A sophisticated calculator implementation with string manipulation and expression evaluation capabilities
๐ Final Project for Data Structures and Algorithms Course
๐จโ๐ซ Under The Supervision of Prof. Hamid Hoorfar
๐ Fall 2020 Semester
This project implements an advanced calculator machine that processes mathematical expressions through efficient string manipulation and evaluation. The calculator maintains an input string with a movable cursor and supports various operations for building and evaluating mathematical expressions.
- ๐ String Manipulation: Dynamic cursor movement, character insertion, and deletion capabilities
- ๐ง Expression Evaluation: Robust evaluation of mathematical expressions with operator precedence
- โก Postfix Conversion: Internal conversion to Reverse Polish Notation for efficient computation
- ๐พ Result Caching: Smart caching mechanism for previously computed expressions
- ๐๏ธ Optimized Data Structures: Efficient implementation using linked lists and stacks
- โฑ๏ธ Performance Optimized: Meets strict time (1s) and memory (256MB) constraints
The calculator supports the following interactive commands:
Command | Description | Example |
---|---|---|
โ (< ) |
Move cursor one position left | "12|3" โ "1|23" |
โ (> ) |
Move cursor one position right | "1|23" โ "12|3" |
c + |
Insert character c at cursor position |
"12|3" + 4 โ "124|3" |
- |
Delete character before cursor | "12|3" โ "1|3" |
? |
Display current string with cursor position | Shows "12|3" |
! |
Evaluate expression and display result | "12+3" โ 15 |
Input String โ [Parser] โ [Postfix Converter] โ [Evaluator] โ Result
โ โ โ
[Cursor Manager] [Operator Stack] [Operand Stack]
- ๐ Linked List: For efficient string manipulation and cursor operations
- ๐ Stack: For expression parsing and evaluation (both operator and operand stacks)
- ๐๏ธ Cache: For storing previously computed results
- String Representation: Uses a doubly linked list for O(1) insertion/deletion at cursor position
- Expression Parsing: Implements Shunting-yard algorithm for infix-to-postfix conversion
- Evaluation: Uses stack-based postfix evaluation with proper operator precedence
- Caching: Memoization technique to avoid redundant computations
- โฐ Time Limit: 1 second per operation
- ๐พ Memory Limit: 256 MB total usage
- ๐ Complexity: O(n) for most operations, optimized for large inputs
>> 1+2+3?
>> 4+<!
>> <5+!
>> 1+2+3|
>> 6
>> 65
>> 1+2+35|
- C++ Compiler (GCC, Clang, or MSVC)
- C++11 or higher support
g++ -std=c++11 -O2 main.cpp -o calculator
./calculator
Calculator/
โโโ Calculator.cpp # Main program entry point
โโโ README.md # This file
๐ This project is licensed under the MIT License - see the LICENSE file for details.