#include <calculator.h>
Inheritance diagram for Calculator:
It supports only four types of actions with integer numbers, so it's quite unuseful. I wrote it only to demonstrate logic simplify with usage of finite state machine drawn with FSME.
Definition at line 18 of file calculator.h.
Public Slots | |
void | slotNumber (int) |
A number have veen entered. | |
void | slotPlus () |
'+' have been pressed | |
void | slotMinus () |
'-' have been pressed | |
void | slotMul () |
'*' have been pressed | |
void | slotDiv () |
'/' have been pressed | |
void | slotClear () |
'C' have been pressed | |
void | slotEqual () |
'=' have been pressed | |
Public Member Functions | |
Calculator (QWidget *parent=0, const char *name=0, WFlags fl=0) | |
int | popOperand () |
Pop an operand from operands stack. It's used by Calculator::Operation. | |
void | pushOperand (int n) |
Push result to the operands stack. It's used by Calculator::Operation::operator()(). | |
Protected Member Functions | |
virtual void | saveNumber () |
virtual void | addNumber () |
virtual void | clear () |
virtual void | saveOp () |
virtual void | showResult () |
Private Member Functions | |
void | showStackTop () |
Display a top of stack on a screen. This is our result. | |
Private Attributes | |
QValueStack< int > | operands |
Operands stack. | |
QPtrStack< Operation > | operations |
Operations stack. | |
int | enteredOperand |
Entered digit. | |
Operation * | enteredOperation |
Entered operation. |
|
Entered operation. This is an operation entered by user. For example, when user clicks "1+2", first, enteredOperand will be "1", and enteredOperation will be Calculator::OpPlus Definition at line 112 of file calculator.h. Referenced by saveOp(), slotDiv(), slotMinus(), slotMul(), and slotPlus(). |
|
Operands stack. There are 2 stacks: operands and operations. This one holds operands being entered. In our simple case this stack will never have more than 1 element. But if parentheses are introduced, deep will grow.
Referenced by addNumber(), clear(), popOperand(), pushOperand(), saveNumber(), and showStackTop(). |
|
Operations stack.
After '=' is pressed or ')' is entered, operation will be popped from this stack. Then it will be executed (taking appropriate number of arguments from operands stack) and the result will be pushed to operands stack back.
Referenced by clear(), saveOp(), and showResult(). |