1 //============================================================================
2 // Name : calculator.cpp
3 // Author :
4 // Version :
5 // Copyright : Your copyright notice
6 // Description : Hello World in C++, Ansi-style
7 //============================================================================
8
9 #include <string>
10 #include <sstream>
11 #include <stack>
12 #include <vector>
13 #include <iostream>
14 #include <iterator>
15 #include <stdexcept>
16 #include <algorithm>
17
18 using namespace std;
19
20 bool CheckBlank(const char c)
21 {
22 return c == ' ';
23 }
24
25 bool CheckInvalid(const char c)
26 {
27 if(c >= '0' && c <= '9')
28 return false;
29 else if(c == '+' || c == '-' || c == '*' || c == '/' ||
30 c == '(' || c == ')')
31 return false;
32 else
33 return true;
34 }
35
36 bool CheckOperator(const char c)
37 {
38 if(c == '+' || c == '-' || c == '*' || c == '/')
39 return true;
40 else
41 return false;
42 }
43
44 bool CheckNotNumber(const char c)
45 {
46 if(c < '0' || c > '9')
47 return true;
48 else
49 return false;
50 }
51
52 int main() {
53 string str_in, str;
54 getline(cin, str_in);
55 remove_copy_if(str_in.begin(), str_in.end(), back_inserter(str), CheckBlank);
56 try{
57 if(str.end() != find_if(str.begin(), str.end(), CheckInvalid))
58 throw runtime_error("Expression Invalid(0)!");
59 if(0 == str.size() || CheckOperator(str[0]) || str[0] == ')')
60 throw runtime_error("Expression Invalid(1)!");
61 if(count(str.begin(), str.end(), '(') != count(str.begin(), str.end(), ')'))
62 throw runtime_error("Expression Invalid(2)!");
63 }catch(runtime_error &err){
64 cout<<err.what();
65 exit(0);
66 }
67
68 vector<string> vect;
69 stack<char> ops;
70 string::iterator i = str.begin(), j;
71 while(i != str.end())
72 {
73 j = find_if(i, str.end(), CheckNotNumber);
74 if(i == j)
75 {
76 if(ops.empty() || '(' == *i)
77 ops.push(*i);
78 else if(')' == *i)
79 {
80 while(0 != ops.size())
81 {
82 if(ops.top() != '(')
83 {
84 string in(" ");
85 in[0] = ops.top();
86 vect.push_back(in);
87 }
88 ops.pop();
89 }
90 }
91 else if('+' == *i || '-' == *i)
92 {
93 if('(' == ops.top())
94 ops.push(*i);
95 else if(CheckNotNumber(ops.top()))
96 {
97 string in(" ");
98 in[0] = ops.top();
99 vect.push_back(in);
100 ops.pop();
101 ops.push(*i);
102 }
103 }
104 else if('*' == *i || '/' == *i)
105 {
106 if('*' == ops.top() || '/' == ops.top())
107 {
108 string in(" ");
109 in[0] = ops.top();
110 vect.push_back(in);
111 ops.pop();
112 }
113 ops.push(*i);
114 }
115 j++;
116 }
117 else
118 {
119 string in(i, j);
120 vect.push_back(in);
121 }
122 i = j;
123 }
124 while(ops.size())
125 {
126 string in(" ");
127 in[0] = ops.top();
128 vect.push_back(in);
129 ops.pop();
130 }
131
132 stack<int> stk;
133 for(vector<string>::size_type i = 0; i < vect.size(); i++)
134 {
135 if(CheckNotNumber(vect[i][0]))
136 {
137 int l, r, result;
138 r = stk.top();
139 stk.pop();
140 l = stk.top();
141 stk.pop();
142 switch(vect[i][0])
143 {
144 case '+':
145 result = l + r;
146 break;
147 case '-':
148 result = l - r;
149 break;
150 case '*':
151 result = l * r;
152 break;
153 case '/':
154 result = l / r;
155 break;
156 }
157 stk.push(result);
158 }
159 else
160 {
161 int value;
162 stringstream ss(vect[i]);
163 ss >> value;
164 stk.push(value);
165 }
166 }
167
168 cout<<"result:"<<stk.top()<<endl;
169 return 0;
170 }