数据结构——链表栈实现算术表达式的运算

1、 design a "automatic calculator" as follows:

1The expression that needs to be calculated is stored in the text file in the TXT text;

2Each line in the text is an expression;

3Expressions include operands, operators such as addition, subtraction, multiplication and division, and parentheses;

     For example 34-72.3*54.7-82.4

4"Automatic calculator" calculates each expression in the text file according to the input file name, and writes every expression of the result to the original file name in the _out.txt, you should use the method of covering and when saving the records. The format of each row is:

    expression = result

     For examplethe original file is: A1.txt

           The file for output is A1_out.txt

           The format of the text in A1_out.txt is:

               34-72.3*54.7-82.4 = -2177.41

     For all the calculated results, you'd keep 4 digits after the decimal point if it is decimal.

5Generate a statistical document after the calculation, its content is:

     Execution timexxxx-xx-xx hh:mm:ss

     The total number of expressions isXXX

     The number of correct expressions isXXX

     The number of error expressions isXXX

     Naming rules for filenames:original file name :_log.txtWrite files with append write method。

      For exampleA1.txt corresponding to the statistical fileA1_log.txt

 

  1 #include <iostream>
  2 #include <stack>
  3 #include <string>
  4 #include<sstream>
  5 #include<fstream>
  6 #include<time.h>
  7 #include<Windows.h>
  8 
  9 //栈的实现头插法
 10 using namespace std;
 11 
 12 
 13 struct stacks{
 14     float data;
 15     stacks *next;
 16 };
 17 
 18 struct stacks_op{
 19     char data;
 20     stacks_op *next;
 21 };
 22 
 23 stacks *st;
 24 stacks_op *op;
 25 bool empty(int n);
 26 void push1(float data);
 27 void push2(char data);
 28 float top1();
 29 char top2();
 30 void pop(int n);
 31 
 32 bool isNumber(char s);
 33 float stringToNum(string s);
 34 int priority(char op);
 35 
 36 
 37 int main() {
 38     DWORD fin_start, fin_stop;
 39     fin_start = GetTickCount();
 40     DWORD start, stop;
 41     
 42     int cnt__ = 0;
 43     int cnt_ = 0;
 44     st = new stacks;
 45     op = new stacks_op;
 46     st->next = NULL;
 47     op->next = NULL;
 48 
 49     string s1 = "A1.txt";
 50     ifstream infile(s1.c_str());
 51 
 52     if (infile.fail()) {
 53         cerr << "file empty" << endl;
 54         abort();
 55     }
 56 
 57     string suffix = "suffix.txt";
 58     //ofstream outfile(suffix.c_str());
 59     ofstream out("A1_out.txt");
 60     string str;
 61 
 62     while (!infile.eof()) {
 63         cnt__++;
 64         start = GetTickCount();
 65         getline(infile, str);
 66         cout << str;
 67         int len = str.length();
 68 
 69         for (int i = 0; i < len; i++) {
 70             string tmp = "";
 71             if (isNumber(str.at(i))) {
 72                 tmp.append(1, str.at(i));
 73                 for (i = i + 1; i < len; i++) {
 74                     if (isNumber(str.at(i))) {
 75                         tmp.append(1, str.at(i));
 76                     }
 77                     else break;
 78                 }
 79                 i--;
 80                 float temp = stringToNum(tmp);
 81                 push1(temp);
 82                 //outfile << temp << " ";
 83             }
 84             else {
 85                 char oper = str.at(i);
 86                 switch (oper) {
 87                 case '(':
 88                     push2(oper);  //当前运算符为'('直接压栈
 89                     break;
 90                 case ')':
 91                     //将栈中元素弹出加到后缀表达式尾,直到遇到运算符"("
 92                     while (top2() != '(') {
 93                         char op = top2();
 94                         float a = top1();
 95 
 96                         pop(1);
 97                         float b = top1();
 98                         pop(1);
 99                         float c;
100 
101                         switch (op)
102                         {
103                         case '+':
104                             c = a + b;
105                             break;
106                         case '-':
107                             c = b - a;
108                             break;
109                         case '*':
110                             c = b / a;
111                             break;
112                         case '/':
113                             c = b / a;
114                             break;
115                         default:
116                             break;
117                         }
118                         push1(c);
119                         
120                         //outfile << top2() << " ";
121                         pop(2);
122                     }
123                     pop(2);
124                     break;
125                 case '+':
126                 case '-':
127                 case '*':
128                 case '/':
129                     while (!empty(2) && priority(top2()) >= priority(oper)) {
130                         char op=oper;
131                         if (priority(top2()) == priority(oper)) {
132                             
133                             op = top2();
134                             pop(2);
135                             push2(oper);
136                             //cout << "push " << oper << endl;
137                         }
138                         if (priority(top2()) > priority(oper)) {
139 
140                             op = top2();
141                             pop(2);
142                             push2(oper);
143                         }
144                         float a = top1();
145                         
146                         pop(1);
147                         float b = top1();
148                         pop(1);
149                         float c;
150                         
151                         switch (op)
152                         {
153                         case '+':
154                             c = a + b;
155                             break;
156                         case '-':
157                             c = b - a;
158                             break;
159                         case '*':
160                             c = b*a;
161                             break;
162                         case '/':
163                             c = b / a;
164                             break;
165                         default:
166                             break;
167                         }
168                         push1(c);
169 
170                         //outfile << top2() << " ";
171                         pop(2);
172                     }
173                     push2(oper);
174                 default:
175                     break;
176                 }
177 
178             }
179         }
180         while (op->next != NULL) {
181             char oper = top2();
182 
183             float a = top1();
184             pop(1);
185             float b = top1();
186             pop(1);
187             float c;
188             switch (oper)
189             {
190             case '+':
191                 c = a + b;
192                 break;
193             case '-':
194                 c = b - a;
195                 break;
196             case '*':
197                 c = b*a;
198                 break;
199             case '/':
200                 c = b / a;
201                 break;
202             default:
203                 break;
204             }
205             push1(c);
206             //outfile << top2() << " ";
207             pop(2);
208             
209         }
210         //outfile << endl;
211         stop = GetTickCount();
212         out << str << "=" << top1() << "      ";
213         double t = (stop - start)*1.0 / 1000;
214         out << "执行运算时间为:" << t << endl;
215         cout << "=" << top1() << endl;
216         cnt_++;
217     }
218     fin_stop = GetTickCount();
219     double tt = (fin_stop - fin_start)*1.0 / 1000;
220     out << "执行总的运算时间为:" << tt << endl;
221     out << "总的表达式数量为:" << cnt__ << endl;
222     out << "正确表达式数量为:" << cnt_ << endl;
223     out << "错误表达式数量为:" << (cnt__-cnt_) << endl;
224 
225     out.close();
226     //outfile.close();
227     infile.close();
228     system("pause");
229 }
230 
231 bool empty(int n) {
232     if (n == 1) {
233         if (st->next == NULL) {
234             return true;
235         }
236         return false;
237     }
238     else if(n==2) {
239         if (op->next == NULL) {
240             return true;
241         }
242         return false;
243     }
244 }
245 
246 void push1(float data) {
247         stacks *q = new stacks;
248         q->next = NULL;
249         q->data = data;
250         q->next = st->next;
251         st->next = q;
252 }
253 
254 void push2(char data) {
255     stacks_op *q = new stacks_op;
256     q->data = data;
257     q->next = op->next;
258 
259     op->next = q;
260 }
261 
262 float top1() {
263     stacks* p = st->next;
264     return p->data;
265 }
266 
267 char top2() {
268     if (op->next == NULL) {
269         cout << "top2 empty" << endl;
270     }
271     stacks_op *p = op->next;
272     return op->next->data;
273 }
274 
275 void pop(int n) {
276     if (n == 1) {
277         stacks *p = st->next;
278         st->next = p->next;
279         delete p;
280         
281     }
282     else {
283         stacks_op *p = op->next;
284         op->next = p->next;
285         delete p;
286     }
287 }
288 
289 /**
290 *判断当前字符是否为数字
291 */
292 bool isNumber(char s) {
293     string opers = "+-*/()";
294     for (int i = 0; i < opers.length(); i++) {
295         if (s == opers.at(i)) {
296             return false;
297         }
298     }
299     return true;
300 }
301 
302 /**
303 * 判断当前运算符与栈顶运算符的优先级大小
304 */
305 float stringToNum(string str) {
306     stringstream ss(str);
307     float num;
308     ss >> num;
309     return num;
310 }
311 
312 int priority(char op) {
313     switch (op)
314     {
315     case '(':
316     case ')':
317         return 0;
318     case '+':
319     case '-':
320         return 1;
321     case '*':
322     case '/':
323         return 2;
324     default:
325         return -1;
326     }
327 }
MAIN.cpp
1 (34.78+89.89)*12/67+673-342
2 89+56/32+67-32/4-23
3 67+67-32.43+77*5+78/32
4 89+73.231-(33.32*67-32.647+6723)
5 78+60*(6+73-5)-23+67+23
6 9999.99999+11111.11111
A1.txt
posted @ 2018-11-07 22:45  明楼  阅读(404)  评论(0)    收藏  举报