1 #include<iostream>
  2 #include<stack>
  3 #include<string>
  4 #include<cmath>
  5 #include<iomanip>
  6 using namespace std;
  7 int GetNum(string s){ 
  8  string ss;
  9  ss = s;
 10  int len = ss.length();
 11  int sum = 0;
 12  int count=0;  
 13  for(int i=0;i<len;i++){
 14   if(ss[i]>='0'&& ss[i]<='9'){
 15               sum = (ss[i]-'0')+ sum*10; 
 16                 count++;
 17   }
 18   else if(count!=0) break;  
 19  }
 20  return sum;
 21 }
 22 string CutNum(string s){
 23  string ss,s1;
 24  ss = s;
 25  s1 = "";
 26     int t;
 27  int len = ss.length();
 28     for(int i=0;i<len;i++){
 29      if(ss[i]<'0'||ss[i]>'9'){
 30         t=i;
 31      break; 
 32   }
 33  }
 34  
 35  if(t==0) return s1;
 36  
 37  for(int j=t;j<len;j++){
 38   s1+=ss[j];
 39  }
 40  
 41  return s1; 
 42 } 
 43 string CutOpra(string s){
 44  string ss,s1;
 45  ss = s;
 46  s1="";
 47     int t=0;
 48  int len = ss.length();
 49  
 50  if(len==1) return s1;
 51  
 52     for(int i=0;i<len;i++){
 53      if(ss[i]<'0'|| ss[i]>'9'){
 54         t=i;
 55      break; 
 56   }
 57  }
 58  
 59  for(int j=t+1;j<len;j++){
 60   s1+=ss[j];
 61  }
 62  
 63  return s1; 
 64 } 
 65 char GetOpra(string s){
 66  string ss,s1;
 67  ss = s;
 68     int t;
 69  int len = ss.length();
 70     for(int i=0;i<len;i++){
 71      if(ss[i]<'0'||ss[i]>'9'){
 72         t=i;
 73      break; 
 74   }
 75  }
 76  char ts;
 77  ts = ss[t];
 78  return  ts;
 79 } 
 80  
 81 //a 栈顶元素 
 82 int Level(char a,char b){         // 优先级比较   大于return1   等于return0   小于return-1 
 83  
 84  if(a=='+'&&(b=='*'||b=='/'||b=='('))                                                 return 1;
 85  else if(a=='+'&&(b=='+'||b=='-'||b=='#'||b==')'))                                    return -1;
 86  
 87  else if(a=='-'&&(b=='*'||b=='/'||b=='('))                                            return 1;
 88  else if(a=='-'&&(b=='+'||b=='-'||b=='#'||b==')'))                                    return -1;
 89  
 90  else if(a=='*'&& b=='(')                                                             return 1;
 91  else if(a=='*'&&(b=='*'||b=='/'||b=='+'||b=='-'||b=='#'||b==')'))                    return -1;
 92  
 93  else if(a=='/'&&(b=='('))                                                            return 1;
 94  else if(a=='/'&&(b=='*'||b=='/'||b=='+'||b=='-'||b=='#'||b==')'))                    return -1;
 95  
 96  else if(a=='('&&(b=='+'||b=='-'||b=='*'||b=='/'||b=='('))                            return 1;
 97  else if(a=='('&& b==')')                                                             return 2;
 98  
 99  else if(a==')'&&(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#'))                    return 1;
100     else if(a=='#'&&(b=='+'||b=='-'||b=='*'||b=='/'||b=='('))                            return 1;
101     else if(a=='#'&&b=='#')                                                              return 0;
102  
103  else return 100000;
104 }
105 
106 int Judge(string s){
107  
108  if(s[0]=='-') {
109     cout<<"由于式子以负号开头,为了方便计算器的运算,所以在整个式子前加一个0"<<endl; 
110     s='0'+s; 
111  }
112  int len = s.length();
113  int c=0;
114  for(int i=0;i<len;i++){
115   if(s[i]=='(') c++;
116   if(s[i]==')') c--;
117   
118   if(s[i]=='.'){
119    cout<<"本计算器不支持小数点的操作"<<endl;
120    return 1; 
121   }
122   
123   if(!((s[i]>='0'&&s[i]<='9')||s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'||s[i]=='#'||s[i]=='('||s[i]==')')){
124    cout<<"对不起,您部分输入字符不符合要求"<<endl;
125    return 2; 
126   }
127   if(s[i]=='0'&&s[i-1]=='/'){
128    cout<<"被除数为0,操作出现错误"<<endl;
129    return 0; 
130   }
131   
132   
133   
134  }
135  if(c!=0) {
136   cout<<"您输入的式子括号不匹配"<<endl; 
137   return 3;
138  }
139  
140  return -1;
141 } 
142  
143 
144 void run(){
145  string s;
146  getline(cin,s,'\n');
147  
148  while(Judge(s)==1||Judge(s)==2||Judge(s)==3||Judge(s)==0){
149   cout<<"请重新输入:";
150   getline(cin,s,'\n');
151  }
152  
153  
154  
155  stack<int> OPND;
156  stack<char> OPTR;
157  char op[1000];
158  int  num[1000];
159     OPTR.push('#');
160     int optop=0;
161     int numtop=0;
162     int step = 0;
163     bool fac = false;
164     
165     cout<<"-------------------------------------------------------------------------------------------------------------------"<<endl;
166     cout<<"步骤         OPTR栈        OPND栈                             输入字符                          主要操作           "<<endl; 
167     cout<<"-------------------------------------------------------------------------------------------------------------------"<<endl;
168      
169     string mainoprt = "";
170     op[optop++]='#';
171     while(s!=""){
172         int e=0;
173         int flag=0;
174      cout<<left<<setw(13)<< ++step;
175      for(int i=0;i<optop;i++){
176       cout<<op[i];
177   }
178      cout<<left<<setw(15-optop)<<" ";
179      for(int i=0;i<numtop;i++){
180       
181       cout<<num[i]<<"'"; 
182             
183             int t = num[i];
184             if(t<0){
185              t = -1*t;
186              flag++;
187    }
188             while(t>10){
189              flag++;
190              t=t/10;  
191    }
192  
193   }
194      cout<<setw(20-2*numtop-flag)<<" ";
195      cout<<right<<setw(23)<<s<<endl; 
196                      
197      
198      
199      
200      
201      if(s[0]<'0'||s[0]>'9'){
202         char ts = GetOpra(s);
203         op[optop++] = ts;
204     //    cout<<"ts="<<ts<<endl;
205         s=CutOpra(s);
206     //    cout<<"CutOpra(s)=="<<s<<endl;
207     //        cout<<"OPTR.top()="<<OPTR.top()<<endl;
208      while(Level(OPTR.top(),ts)==-1) {
209              int a,b;
210              a = OPND.top();
211              OPND.pop();
212              b =  OPND.top();
213              numtop  = numtop-2;
214             
215    //          cout<<"a=="<<a<<"  "<<"b=="<<b<<endl;
216              OPND.pop();
217              if(OPTR.top()=='*')  OPND.push(a*b);
218              if(OPTR.top()=='/')  {
219               if(a==0) cout<<"    程序出现被除数为0的情况,被除数将被随机数取代,此次运算无效"<<endl;
220               fac = true;
221      a = 1;
222               OPND.push(b/a);
223     }
224              if(OPTR.top()=='+')  OPND.push(a+b);
225              if(OPTR.top()=='-')  OPND.push(b-a);
226    //          cout<<"OPND.top()=="<<OPND.top()<<endl;
227                 optop--;
228                 op[optop-1] = ts;
229                 num[numtop++] = OPND.top();
230              OPTR.pop();
231  //      cout<<"OPtR.top()=="<<OPTR.top()<<endl;
232            
233        }
234       while(Level(OPTR.top(),ts)==1) {
235              OPTR.push(ts);   
236              break;
237      } 
238            if(Level(OPTR.top(),ts)==2){
239                 OPTR.pop();
240                 optop = optop-2;
241                 ts = ' '; 
242       //          cout<<"OPtR.top()=="<<OPTR.top()<<endl;
243      }
244        if(Level(ts,OPTR.top())==0) {
245          
246   //         cout<<"zhangding="<<OPTR.top()<<endl;
247            OPTR.pop();
248            optop = optop-1;
249            break; 
250    }  
251   }
252   
253      else{
254      
255      int a = GetNum(s);
256        OPND.push(a);
257        mainoprt +="PUSH(OPND, '')"; 
258        num[numtop++] = a;
259        s=CutNum(s);
260   }
261  }
262  
263  
264  
265  
266     while(!OPTR.empty()){
267         
268         // cout<<"zhangding="<<OPTR.top()<<endl;
269          OPTR.pop();
270      
271      int a,b;
272         // a = OPND.top();     cout<<"a="<<a<<endl;
273          OPND.pop();
274         //b =  OPND.top();    cout<<"b="<<b<<endl;
275          OPND.pop();
276          numtop  = numtop-2;
277          if(OPTR.top()=='*')  OPND.push(a*b);
278          if(OPTR.top()=='/')  {
279               if(a==0) cout<<"    程序出现被除数为0的情况,被除数将被随机数取代,此次运算无效"<<endl;
280               fac = true;
281      a = 1;
282               OPND.push(b/a);
283     }
284          if(OPTR.top()=='+')  OPND.push(a+b);
285          if(OPTR.top()=='-')  OPND.push(b-a);
286          optop--;
287             num[numtop++] = OPND.top();
288          OPTR.pop();
289     
290          
291      int e=0;
292         int flag=0;
293      cout<<left<<setw(13)<< ++step;
294      for(int i=0;i<optop;i++){
295       cout<<op[i];
296   }
297      cout<<left<<setw(15-optop)<<" ";
298      for(int i=0;i<=numtop;i++){
299       
300       cout<<num[i]<<"'"; 
301             
302             int t = num[i];
303             if(t<0){
304              t = -1*t;
305              flag++;
306    }
307             while(t>10){
308              flag++;
309              t=t/10;  
310    }
311  
312   }
313      cout<<setw(26-2*numtop-flag)<<" ";
314      cout<<right<<setw(23)<<s<<endl; 
315    
316          
317   }
318   
319   cout<<left<<setw(13)<<step+1<<left<<setw(15-optop)<<"#"<<left<<setw(15-optop)<<OPND.top()<<right<<setw(26)<<"#"<<endl;
320   cout<<endl;
321   if(fac == false)cout<<"这个式子的结果="<<OPND.top()<<endl;
322   else cout<<"这个式子的结果不存在"<<endl; 
323 }
324 int main(){
325  while(1){
326      run();
327  } 
328  return 0;
329 } 

 

posted on 2016-10-26 00:38  任我主宰  阅读(189)  评论(0编辑  收藏  举报