应用stack 计算表达式(中缀表达式,+ - × / ^)及计算器的实现
Calculator.h
Calculator.cpp
main.cpp
1 #ifndef CALCULATOR_H
2 #define CALCULATOR_H
3
4 #include "Stack.h"
5 #include <math.h>
6 #include <iostream>
7 using namespace std;
8
9 class Calculator
10 {
11 public:
12 Calculator(){ }
13 ~Calculator(){ }
14
15 void run();
16 void clear();
17
18 private:
19 void addOperand(double val){ iStack.push(val); }
20 bool getOperand(double &left, double &right);
21 void doOperator(char op);
22 Stack<double> iStack;
23 };
24
25
26
27 #endif
2 #define CALCULATOR_H
3
4 #include "Stack.h"
5 #include <math.h>
6 #include <iostream>
7 using namespace std;
8
9 class Calculator
10 {
11 public:
12 Calculator(){ }
13 ~Calculator(){ }
14
15 void run();
16 void clear();
17
18 private:
19 void addOperand(double val){ iStack.push(val); }
20 bool getOperand(double &left, double &right);
21 void doOperator(char op);
22 Stack<double> iStack;
23 };
24
25
26
27 #endif
Calculator.cpp
1 #include "Calculator.h"
2
3 bool Calculator::getOperand(double &left, double &right)
4 {
5 if(iStack.empty())
6 {
7 cerr << "Missing Operand!" << endl;
8 return false;
9 }
10
11 right = iStack.pop();
12
13 if(iStack.empty())
14 {
15 cerr << "Missing Operand!" << endl;
16 return false;
17 }
18
19 left = iStack.pop();
20
21 return true;
22 }
23
24 void Calculator::doOperator(char op)
25 {
26 double left, right;
27 if(getOperand(left, right))
28 {
29 switch(op)
30 {
31 case '+': iStack.push(left + right); break;
32 case '-': iStack.push(left - right); break;
33 case '*': iStack.push(left * right); break;
34 case '/': if(right == 0.0)
35 {
36 cerr << "Divide by 0!" << endl;
37 clear();
38 }
39 else
40 {
41 iStack.push(left/right);
42 break;
43 }
44 case '^': iStack.push(pow(left, right)); break;
45 }
46 }
47 else clear();
48
49 }
50
51 void Calculator::run()
52 {
53 char ch;
54 double newop;
55 cout << "Please input expression: (end by =)\n";
56 while(cin >> ch, ch != '=')
57 {
58 switch(ch)
59 {
60 case '+':
61 case '-':
62 case '*':
63 case '/':
64 case '^':
65 doOperator(ch); break;
66
67 default: cin.putback(ch);
68 cin >> newop;
69 addOperand(newop);
70 break;
71 }
72 }
73
74 cout << "Result: " << iStack.pop() << "\n";
75 }
76
77 void Calculator::clear()
78 {
79 iStack.makeEmpty();
80
81 }
2
3 bool Calculator::getOperand(double &left, double &right)
4 {
5 if(iStack.empty())
6 {
7 cerr << "Missing Operand!" << endl;
8 return false;
9 }
10
11 right = iStack.pop();
12
13 if(iStack.empty())
14 {
15 cerr << "Missing Operand!" << endl;
16 return false;
17 }
18
19 left = iStack.pop();
20
21 return true;
22 }
23
24 void Calculator::doOperator(char op)
25 {
26 double left, right;
27 if(getOperand(left, right))
28 {
29 switch(op)
30 {
31 case '+': iStack.push(left + right); break;
32 case '-': iStack.push(left - right); break;
33 case '*': iStack.push(left * right); break;
34 case '/': if(right == 0.0)
35 {
36 cerr << "Divide by 0!" << endl;
37 clear();
38 }
39 else
40 {
41 iStack.push(left/right);
42 break;
43 }
44 case '^': iStack.push(pow(left, right)); break;
45 }
46 }
47 else clear();
48
49 }
50
51 void Calculator::run()
52 {
53 char ch;
54 double newop;
55 cout << "Please input expression: (end by =)\n";
56 while(cin >> ch, ch != '=')
57 {
58 switch(ch)
59 {
60 case '+':
61 case '-':
62 case '*':
63 case '/':
64 case '^':
65 doOperator(ch); break;
66
67 default: cin.putback(ch);
68 cin >> newop;
69 addOperand(newop);
70 break;
71 }
72 }
73
74 cout << "Result: " << iStack.pop() << "\n";
75 }
76
77 void Calculator::clear()
78 {
79 iStack.makeEmpty();
80
81 }
main.cpp
1 #include <iostream>
2 #include "Calculator.h"
3 using namespace std;
4
5
6 int main()
7 {
8 Calculator CAL;
9 CAL.run();
10
11 return 0;
12 }
13
2 #include "Calculator.h"
3 using namespace std;
4
5
6 int main()
7 {
8 Calculator CAL;
9 CAL.run();
10
11 return 0;
12 }
13
输入: 8 5 6 + * 2 / 6 8 * + =
输出: 92
怎样实现一个计算器?所有输入的字符(非中缀)都存在一个字符buffer中