数据结构实验课程----实验三(算术表达式的计算)

题目

利用堆栈实现算术表达式的计算

实验内容:

必做内容:运算对象均为整数

选作内容:运算对象扩充为可以是带小数位的浮点数

PS:这是直接由中缀计算。通过操作符的优先级别来判断什么时候将操作数栈里面的两个数分别弹出来,和将操作符栈的操作符弹出来进行操作。因为当一个操作符准备放入栈的时候,其实前一个操作符所左右的两个操作数已经存在操作数栈,只要判断是否要进行操作就可以了

PS:这是整数的,小数的只是判断小数点而已,稍微修改一下就可以。

 

  1 #include<iostream>
  2 #include<string>
  3 
  4 using namespace std;
  5 
  6 template <typename Object>
  7 class Stack
  8 {
  9 private:
 10     struct Node
 11     {
 12         Object data;
 13         Node * next;
 14 
 15         Node( const Object & d = Object( ), Node * n = NULL ) : data( d ), next( n ) { }
 16     };
 17 
 18 public:
 19     Stack( )
 20     {
 21         init( );
 22     }
 23 
 24     ~Stack( )
 25     {
 26         clear( );
 27         delete Top;
 28     }
 29 
 30     void push( const Object & x )
 31     {
 32         Top = new Node( x, Top );
 33         theSize++;
 34     }
 35 
 36     const Object top( ) const
 37     {
 38         return Top->data;
 39     }
 40 
 41     void pop( )
 42     {
 43         Node *p = Top;
 44         Top = Top->next;
 45         theSize--;
 46         delete p;
 47     }
 48 
 49     int size( ) const
 50     {
 51         return theSize;
 52     }
 53 
 54     bool empty( )
 55     {
 56         return size() == 0;
 57     }
 58 
 59     void clear( )
 60     {
 61         if( size( ) != 0 )
 62         {
 63             pop( );
 64         }
 65     }
 66 
 67 private:
 68     Node *Top;
 69     int theSize;
 70 
 71     void init()
 72     {
 73         theSize = 0;
 74         Top = new Node;
 75     }
 76 };
 77 
 78 //用单链表实现栈。也可以用数组实现。
 79 
 80 int main()
 81 {
 82     string s;
 83     int i, n, s_size;
 84     char c;
 85 
 86     while(getline( cin , s ))
 87     {
 88         n = 0;
 89         s_size = s.size();
 90         Stack<int> s_num;   //存操作数的栈
 91         Stack<char> s_ope;  //存操作符的栈
 92 
 93         /*cout << s_size << endl;
 94         cout << s << endl;*/
 95 
 96         for( i = 0; i != s_size; ++i )
 97         {
 98             if( s[i] >= '0' && s[i] <= '9' )
 99             {
100                 n *= 10;
101                 n += ( s[i] - '0' );
102             }
103             //将字符串里面连续数字取出来
104 
105             else
106             {
107                 if( n != 0 )
108                 {
109                     s_num.push( n );
110                     n = 0;
111                     //cout << s_num.top() << endl;
112                 }
113                 //表达式中如果不是连续数字,那么就将之前取出来的数字存入栈里面
114 
115                 if( s[i] != ' ' )
116                 {
117                     c = s[i];
118                     //将不是数字和空格的字符逐个判断
119                     if( s_ope.size() == 0  || c == '(' )
120                     {
121                         s_ope.push( c );
122                     }
123                     //如果栈是空的,或者是'('就直接存入操作符栈
124 
125                     else
126                     {
127                         if( s_ope.top() == '*' || s_ope.top() == '/' )
128                         {
129                             if( s_ope.top() == '*' )
130                             {
131                                 int b = s_num.top();
132                                 s_num.pop();
133                                 int a = s_num.top();
134                                 s_num.pop();
135                                 int result;
136                                 result = a * b;
137                                 s_num.push( result );
138                                 s_ope.pop();
139                             }
140                             else
141                             {
142                                 int b = s_num.top();
143                                 s_num.pop();
144                                 int a = s_num.top();
145                                 s_num.pop();
146                                 int result;
147                                 result = a / b;
148                                 s_num.push( result );
149                                 s_ope.pop();
150                             }
151 
152                             if(  c == '+' || c == '-'   )
153                             {
154                                 if( s_ope.top() == '+' || s_ope.top() == '-' )
155                                 {
156                                     int b = s_num.top();
157                                     s_num.pop();
158                                     int a = s_num.top();
159                                     s_num.pop();
160                                     int result;
161                                     if( s_ope.top() == '+' )
162                                         result = a + b;
163                                     else
164                                         result = a - b;
165 
166                                     s_num.push( result );
167                                     s_ope.pop();
168                                 }
169                             }
170                             //s_ope.push( c );
171                         }
172                         //如果存操作符的栈的栈顶元素是'*'或'/',那么就一定要先计算,再存入该操作符。根据运算规则。
173                         /*
174                         PS:如果进行完'*'或'/'操作后,操作符c和操作符栈的栈顶元素同样是'+'/'-',那么就要进行操作符栈的栈顶元素的操作
175                         根据同级运算符从左往右,但是不用考虑'*'/'/'。因为根据这种方法,'*'/'/',不会连续出现在栈里面。
176                         */
177 
178                         else if( s_ope.top() == '+' || s_ope.top() == '-' )
179                         {
180                             if( c == '+' || c == '-' )
181                             {
182                                 int b = s_num.top();
183                                 s_num.pop();
184                                 int a = s_num.top();
185                                 s_num.pop();
186                                 int result;
187                                 if( s_ope.top() == '+' )
188                                     result = a + b;
189                                 else
190                                     result = a - b;
191 
192                                 s_num.push( result );
193                                 s_ope.pop();
194                             }
195                             //s_ope.push( c );
196                         }
197 
198 
199                         if( c == ')' )
200                         {
201                             if( s_ope.top() == '*' || s_ope.top() == '/' )
202                             {
203                                 int b = s_num.top();
204                                 s_num.pop();
205                                 int a = s_num.top();
206                                 s_num.pop();
207                                 int result;
208 
209                                 if( s_ope.top() == '*' )
210                                     result = a * b;
211 
212                                 else
213                                     result = a / b;
214 
215                                 s_num.push( result );
216                                 s_ope.pop();
217                             }
218 
219                             while( s_ope.top() != '(' )
220                             {
221                                 if( s_ope.top() == '*' || s_ope.top() == '/' )
222                                 {
223                                     int b = s_num.top();
224                                     s_num.pop();
225                                     int a = s_num.top();
226                                     s_num.pop();
227                                     int result;
228                                     if( s_ope.top() == '*' )
229                                         result = a * b;
230                                     else
231                                         result = a / b;
232 
233                                     s_num.push( result );
234                                     s_ope.pop();
235                                 }
236 
237                                 else
238                                 {
239                                     int b = s_num.top();
240                                     s_num.pop();
241                                     int a = s_num.top();
242                                     s_num.pop();
243                                     int result;
244                                     if( s_ope.top() == '+' )
245                                         result = a + b;
246                                     else
247                                         result = a - b;
248 
249                                     s_num.push( result );
250                                     s_ope.pop();
251                                 }
252                             }
253 
254                             s_ope.pop();
255                         }
256                         /*
257                         遇到')',就将操作符栈里面的元素弹出,直到栈顶元素为'(',因为没有遇到')'的时候,一切都是按照基本的方法进行
258                         相当于'()'里面是一条另外的算式。
259                         */
260 
261                         else
262                         {
263                             s_ope.push( c );
264                         }
265                         //只有在c不是')'才,将其存入操作符栈
266                     }
267                 }
268             }
269         }
270 
271         //cout << s_num.top() << endl;
272         if( n != 0 )
273             s_num.push( n );
274         //有可能最后的一个数是没有存入操作数栈,因为回车的时候停止了
275 
276         /*int b = s_num.top();
277         s_num.pop();
278         int a = s_num.top();
279         s_num.pop();
280         cout << a << " " << b << endl;*/
281 
282         /*while( s_num.size() != 0 )
283         {
284             cout << s_num.top() << endl;
285             s_num.pop();
286         }*/
287 
288         /*while( s_ope.size() != 0 )
289         {
290             cout << s_ope.top() << endl;
291             s_ope.pop();
292         }*/
293 
294 
295         while( s_ope.size() != 0 )
296         {
297             if( s_ope.top() == '*' || s_ope.top() == '/' )
298             {
299                 if( s_ope.top() == '*' )
300                 {
301                     int b = s_num.top();
302                     s_num.pop();
303                     int a = s_num.top();
304                     s_num.pop();
305                     int result;
306                     result = a * b;
307                     s_num.push( result );
308                     s_ope.pop();
309                 }
310                 else
311                 {
312                     int b = s_num.top();
313                     s_num.pop();
314                     int a = s_num.top();
315                     s_num.pop();
316                     int result;
317                     result = a / b;
318                     s_num.push( result );
319                     s_ope.pop();
320                 }
321             }
322 
323             else
324             {
325                 if( s_ope.top() == '+' )
326                 {
327                     int b = s_num.top();
328                     s_num.pop();
329                     int a = s_num.top();
330                     s_num.pop();
331                     int result;
332                     result = a + b;
333                     s_num.push( result );
334                     s_ope.pop();
335                 }
336                 else
337                 {
338                     int b = s_num.top();
339                     s_num.pop();
340                     int a = s_num.top();
341                     s_num.pop();
342                     int result;
343                     result = a - b;
344                     s_num.push( result );
345                     s_ope.pop();
346                 }
347             }
348         }
349         //将操作符栈里面的元素逐个弹出并执行操作,直到栈为空。
350 
351         cout << s_num.top() << endl;
352     }
353 
354     return 0;
355 }
posted @ 2012-10-23 13:36  alan_forever  阅读(706)  评论(0编辑  收藏  举报