Calculator Project

C++ wxWidgets
RoleSolo Developer
Team Size1
Development Time1 month

Contributions

Shunting yard expression parser Postfix solver and tokeniser Trig and modulo support Error handling for bad input
wxWidgets interface and sizers Table driven button factory Keyboard input and font scaling 20 unit tests

About
  • A desktop calculator written in C++ with wxWidgets for a software engineering course.
  •  
  • The full expression is entered first and then solved, rather than one operation at a time.
  •  
  • Button and keyboard events link to functionality to only operate when requested.
The calculator at rest, with a keypad of numbers, orange operator keys and sin, cos and tan along the top
Order of Operations
  • Expressions follow PEMDAS instead of solving left to right as they are typed.
  •  
  • Long equations can be solved and displayed.
  •  
  • The history line shows the previous expression before being solved.
A long chained expression solved to 26.333334, with the original expression shown above the result
Shunting Yard
  • ConvertToPostfix reads each token in order and uses an operator stack to convert the expression into postfix.
  •  
  • Parsing and solving are separate steps. SolvePostfix reads the finished queue and returns a single value.
  •  
  • Unrecognised tokens return undef, which is displayed instead of an answer.
Source code of the ConvertToPostfix function, showing the operator stack and precedence loop
Trig Functions
  • Sine, Cosine and Tangent use radians and are entered as s, c and t in front of a number.
  •  
  • s3.1415 + c3.1415 returns ~0.999907. The tilde is the negative sign, inserted by the plus minus button.
  •  
  • Trig terms are solved before the expression is converted to postfix.
Sine and cosine of 3.1415 added together, returning a negative result written with a tilde
Buttons and Resizing
  • Each button is defined in a single table. An entry holds the id, label, symbol, keyboard keys and button type, and the type sets the colour.
  •  
  • The window can be resized. Fonts scale with it and stop at a minimum size so labels stay inside their buttons.
  •  
  • Divide and mod by zero are checked before solving. Malformed input, such as a trailing operator, returns undef.
The calculator at a larger window size, showing the display text scaled up with it