码迷,mamicode.com
首页 > 其他好文 > 详细

个人项目总结

时间:2016-03-19 17:51:19      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

 

括号匹配与括号随机生成不好解决,故使用了读文本文件取算式的方法。

去年数据结构课上实现了中缀转后缀并求值的小程序,在基础上进行了修改。

对于答案正确与否的匹配,用cal函数将原题计算一次,再将答案计算一次,两者误差小于一固定值(十万分之一),即为正确。

 

以下为代码:

 

  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <stdlib.h>
  4 #include <iomanip>
  5 #include <string>
  6 using namespace std;
  7 #define MAXSIZE 99
  8 #define ERROR 0.00001
  9 class node
 10 {
 11 public:
 12     char oper;
 13     double value;
 14     node *next;
 15     node(char c, double v, node *nextval)
 16     {
 17         oper = c;
 18         value = v;
 19         next = nextval;
 20     }
 21     ~node() {};
 22 };
 23 class stack
 24 {
 25 public:
 26     node *top;
 27     stack()
 28     {
 29         node *head = new node(‘, 0, NULL);
 30         top = head;
 31     }
 32     /*~stack()
 33     {
 34         node *tem;
 35         while (top->next != NULL)
 36         {
 37             tem = top;
 38             top = top->next;
 39             delete tem;
 40         }
 41     }*/
 42     void push(char c, double v)
 43     {
 44         node *tem = new node(c, v, top);
 45         top = tem;
 46     }
 47     node* pop()
 48     {
 49         node *tem;
 50         tem = top;
 51         top = top->next;
 52         return tem;
 53     }
 54 };
 55 
 56 double cal(char* s);
 57 
 58 int main()
 59 {
 60     int wrong = 0;
 61     int right = 0;
 62     int i = 0;
 63     double a1, a2;
 64     FILE *fp;
 65     char s[50];
 66     if ((fp = fopen("test.txt", "r")) == NULL)
 67     {
 68         cout << "ERROR" << endl;
 69     }
 70     else
 71     {
 72         for (i = 0; i < 10; i++)
 73         {
 74             fgets(s, MAXSIZE, fp);
 75             cout << s;
 76             strcat(s, "");
 77             a1 = cal(s);
 78             cout << "result:";
 79             cin >> s;
 80             strcat(s, "");
 81             a2 = cal(s);
 82             if (abs(a1 - a2) <= ERROR)
 83             {
 84                 right++;
 85                 cout << "RIGHT!" << endl;
 86             }
 87             else
 88             {
 89                 wrong++;
 90                 cout << "WRONG!" << endl;
 91             }
 92         }
 93     }
 94     cout <<"正确:"<<right <<" "<<"错误:" <<wrong << endl;
 95     cin>> s;
 96 }
 97 
 98 
 99 double cal(char* s2)
100 {
101     stack mystack;
102     node *tem, *tem2;
103     int i, j = 0;
104     int len, count = 0;
105     char s1[50];
106     //cout << "输入中缀表达式:";
107     //cin >> s2;
108     len = strlen(s2);
109     for (i = 0; i<len; i++)
110     {
111         if (s2[i] == ()
112         {
113             mystack.push((‘, 0);
114             //mystack.show();
115         }
116         else if (s2[i] >= 0‘&&s2[i] <= 9)
117         {
118             s1[count] = s2[i];
119             count++;
120         }
121         else if (s2[i] == *‘ || s2[i] == /)
122         {
123             s1[count] = ;
124             count++;
125             if (mystack.top->oper == *‘ || mystack.top->oper == /)
126             {
127                 tem = mystack.pop();
128                 s1[count] = tem->oper;
129                 count++;
130                 delete tem;
131                 //mystack.show();
132             }
133             mystack.push(s2[i], 0);
134             //mystack.show();
135         }
136         else if (s2[i] == +‘ || s2[i] == -)
137         {
138             s1[count] = ;
139             count++;
140             if (mystack.top->oper == *‘ || mystack.top->oper == /‘ || mystack.top->oper == -‘ || mystack.top->oper == +)
141             {
142                 tem = mystack.pop();
143                 s1[count] = tem->oper;
144                 count++;
145                 delete tem;
146                 //mystack.show();
147             }
148             mystack.push(s2[i], 0);
149         }
150         else if (s2[i] == ))
151         {
152             while (mystack.top->oper != ()
153             {
154                 tem = mystack.pop();
155                 s1[count] = tem->oper;
156                 count++;
157                 delete tem;
158             }
159             tem = mystack.pop();
160             delete tem;
161         }
162     }
163     while (mystack.top->next != NULL)
164     {
165         tem = mystack.pop();
166         s1[count] = tem->oper;
167         count++;
168         delete tem;
169         //mystack.show();
170     }
171     s1[count] = \0;
172     //cout << "后缀表达式为:" << s1 << endl;
173     bool change1, change2;
174     change1 = change2 = false;
175     for (i = 0; i<count; i++)
176     {
177         double x;
178         char s[20];
179         if (s1[i] >= 0‘&&s1[i] <= 9)
180         {
181             s[j] = s1[i];
182             j++;
183             change1 = true;
184         }
185         else if (s1[i] != \0)
186         {
187             if (change1)
188             {
189                 change2 = true;
190                 change1 = false;
191                 s[j] = \0;
192                 j = 0;
193             }
194         }
195         if (change2)
196         {
197             x = atoi(s);
198             s[j] = \0;
199             change2 = false;
200             mystack.push(#, x);
201             //mystack.show();
202         }
203 
204         if (s1[i] == +‘ || s1[i] == -‘ || s1[i] == *‘ || s1[i] == /)
205         {
206             double sum = 0;
207             if (s1[i] == +)
208             {
209                 tem = mystack.pop();
210                 // mystack.show();
211                 tem2 = mystack.pop();
212                 //mystack.show();
213                 sum = tem2->value + tem->value;
214                 //cout << tem2->value << "+" << tem->value << "=" << sum << endl;
215             }
216             else if (s1[i] == -)
217             {
218                 tem = mystack.pop();
219                 // mystack.show();
220                 tem2 = mystack.pop();
221                 //mystack.show();
222                 sum = tem2->value - tem->value;
223                 //cout << tem2->value << "-" << tem->value << "=" << sum << endl;
224             }
225             else if (s1[i] == *)
226             {
227                 tem = mystack.pop();
228                 //mystack.show();
229                 tem2 = mystack.pop();
230                 //mystack.show();
231                 sum = tem2->value*tem->value;
232                 //cout << tem2->value << "*" << tem->value << "=" << sum << endl;
233             }
234             else if (s1[i] == /)
235             {
236                 tem = mystack.pop();
237                 //mystack.show();
238                 tem2 = mystack.pop();
239                 //mystack.show();
240                 sum = tem2->value / tem->value;
241                 //cout << tem2->value << "/" << tem->value << "=" << fixed << setprecision(5) << sum << endl;
242             }
243             delete tem;
244             delete tem2;
245             mystack.push(#, sum);
246             //mystack.show();
247         }
248     }
249     tem = mystack.pop();
250     //mystack.show();
251     //cout << "结果为:" << fixed << setprecision(5) << tem->value << endl;
252     //delete tem;
253     return tem->value;
254 }

 

个人项目总结

标签:

原文地址:http://www.cnblogs.com/stormshot/p/5295533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!