A bc-style arbitrary-precision calculator written in pure C. Every digit lives in a linked-list node, so numbers can grow to 10,000+ digits with exact results.
Standard C integers overflow at 19 digits. This calculator does not care: it stores each number as a doubly linked list of digits (the NUMBER ADT) and implements addition, subtraction, multiplication, division, and exponentiation digit by digit, the way you would on paper. Expressions are tokenized by a finite state machine and evaluated with the classic two-stack (shunting-yard) algorithm, like bc does.
Real session, exact answers:
- Arbitrary precision: numbers stored as doubly linked lists of digits, 10,000+ digits without overflow
- Five operators with correct precedence:
+ - * / ^ - Negative exponents:
2 ^ -2gives0.25 - Signed decimal arithmetic: sign flag + decimal-point position tracked per number, with a padding step that aligns decimal places before add/subtract
- Parentheses with balance checking
- Interactive REPL that logs every session to
History1.txt - Error detection: unbalanced parentheses, invalid characters, malformed expressions
The parser walks the input one character at a time. What it does next depends on the state it is in, which makes it a small FSM:
stateDiagram-v2
[*] --> Scan
Scan --> Integer : digit, or unary '-' before a digit
Integer --> Integer : digit
Integer --> Fraction : '.'
Fraction --> Fraction : digit
Integer --> Scan : end of number, push NUMBER onto operand stack
Fraction --> Scan : end of number, push NUMBER onto operand stack
Scan --> Operator : + - * / ^
Operator --> Scan : pop higher-precedence ops and evaluate,<br/>then push this one
Scan --> Scan : '(' pushed / space skipped
Scan --> Reduce : ')'
Reduce --> Scan : pop and evaluate until '('
Scan --> Error : unknown character
Error --> [*]
Scan --> Drain : end of input
Drain --> [*] : evaluate remaining operators,<br/>result is on top of the operand stack
While a number is being scanned, each digit is appended to a fresh NUMBER; hitting a . records the decimal position, and a leading - sets the sign flag.
- an operator stack (
chars) for+ - * / ^ ( - an operand stack of
NUMBERADTs
Operators are pushed and popped by precedence (^ above * / above + -), exactly like infix-to-postfix conversion. Each pop applies the operator to the top two NUMBERs using linked-list arithmetic and pushes the result back.
The original design sketch:
Each NUMBER carries the digit list, a decimal-point position, a sign flag, and a decimal flag. Before add/subtract, a padding function aligns both operands to the same decimal length (1.5 and 12.25 become 01.50 and 12.25), so digit-wise arithmetic stays exact.
make
./calcThen type expressions at the prompt; q quits. Every session is appended to History1.txt:
>>>>123456789123456789 * 987654321987654321
121932631356500531347203169112635269
>>>>q
Binary_Calculator/
├── main.c # REPL loop + history logging
├── eval.c/.h # FSM tokenizer + two-stack expression evaluation
├── numbers.c/.h # NUMBER ADT: add, subtract, multiply, divide, power, padding
├── stack.c/.h # Operator stack and operand (NUMBER) stack
├── dlist.c/.h # Doubly linked list of digits
└── docs/ # Logo, demo session, ADT design sketch

