Skip to content

abhi25072002/Binary_Calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binary Calculator - arbitrary-precision math in pure C

Language: C Inspired by Dependencies License: MIT

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.


What is this?

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.

Demo

Real session, exact answers:

Binary Calculator session: exact results for 2^100, 2^256, an 18x18 digit multiplication, decimals and negative exponents

Features

  • Arbitrary precision: numbers stored as doubly linked lists of digits, 10,000+ digits without overflow
  • Five operators with correct precedence: + - * / ^
  • Negative exponents: 2 ^ -2 gives 0.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

How it works

1. Tokenizing: the finite state machine

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
Loading

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.

2. Evaluation: two stacks

  • an operator stack (chars) for + - * / ^ (
  • an operand stack of NUMBER ADTs

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.

3. The NUMBER ADT

The original design sketch:

ADTs for Binary Calculator

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.

Build and run

make
./calc

Then type expressions at the prompt; q quits. Every session is appended to History1.txt:

>>>>123456789123456789 * 987654321987654321
121932631356500531347203169112635269
>>>>q

Project structure

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

License

MIT

About

A bc-style arbitrary-precision calculator in pure C. Numbers stored as linked lists of digits (10,000+ digits), FSM expression parser, two-stack evaluation. No bignum libraries.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages