Skip to content

๐Ÿ–ฉ A high-performance C++ calculator featuring cursor-based string editing, infix-to-postfix conversion (Shunting-yard), and result caching for quick evaluations.

Notifications You must be signed in to change notification settings

Amirbehnam1009/Calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿงฎ Calculator Project

A sophisticated calculator implementation with string manipulation and expression evaluation capabilities

Data Structures License Status

๐Ÿ“– About

๐ŸŽ“ Final Project for Data Structures and Algorithms Course
๐Ÿ‘จโ€๐Ÿซ Under The Supervision of Prof. Hamid Hoorfar
๐Ÿ‚ Fall 2020 Semester

๐Ÿš€ Overview

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.

โœจ Key Features

  • ๐Ÿ“ 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

๐ŸŽฏ Operations

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

๐Ÿ—๏ธ Architecture & Implementation

๐Ÿ”ง Technical Design

Input String โ†’ [Parser] โ†’ [Postfix Converter] โ†’ [Evaluator] โ†’ Result
โ†‘ โ†‘ โ†‘
[Cursor Manager] [Operator Stack] [Operand Stack]

๐Ÿ“ฆ Data Structures Used

  • ๐Ÿ”— 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

โš™๏ธ Algorithmic Approach

  1. String Representation: Uses a doubly linked list for O(1) insertion/deletion at cursor position
  2. Expression Parsing: Implements Shunting-yard algorithm for infix-to-postfix conversion
  3. Evaluation: Uses stack-based postfix evaluation with proper operator precedence
  4. Caching: Memoization technique to avoid redundant computations

๐Ÿšฆ Performance Constraints

  • โฐ Time Limit: 1 second per operation
  • ๐Ÿ’พ Memory Limit: 256 MB total usage
  • ๐Ÿ“Š Complexity: O(n) for most operations, optimized for large inputs

๐Ÿ“ธ Example Usage

๐Ÿ–ฅ๏ธ Input Example

>> 1+2+3?
>> 4+<!
>> <5+!

๐Ÿ“‹ Output Example

>> 1+2+3|
>> 6
>> 65
>> 1+2+35|

๐Ÿ› ๏ธ Installation & Usage

๐Ÿ“‹ Prerequisites

  • C++ Compiler (GCC, Clang, or MSVC)
  • C++11 or higher support

๐Ÿš€ Compilation

g++ -std=c++11 -O2 main.cpp -o calculator

๐ŸŽฎ Running the Program

./calculator

๐Ÿ“ Repository Structure

Calculator/
โ”œโ”€โ”€ Calculator.cpp       # Main program entry point
โ””โ”€โ”€ README.md            # This file

๐Ÿ“œ License

๐Ÿ“„ This project is licensed under the MIT License - see the LICENSE file for details.

About

๐Ÿ–ฉ A high-performance C++ calculator featuring cursor-based string editing, infix-to-postfix conversion (Shunting-yard), and result caching for quick evaluations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages