1 #include<iostream>
  2 #include<stack>
  3 #include<string>
  4 #include<cmath>
  5 using namespace std;
  6 int GetNum(string s){ 
  7  string ss;
  8  ss = s;
  9  int len = ss.length();
 10  int sum = 0;
 11  int count=0;  
 12  for(int i=0;i<len;i++){
 13   if(ss[i]>='0'&& ss[i]<='9'){
 14               sum = (ss[i]-'0')+ sum*10; 
 15                 count++;
 16   }
 17   else if(count!=0) break;  
 18  }
 19  return sum;
 20 }
 21 string CutNum(string s){
 22  string ss,s1;
 23  ss = s;
 24  s1 = "";
 25     int t;
 26  int len = ss.length();
 27     for(int i=0;i<len;i++){
 28      if(ss[i]<'0'||ss[i]>'9'){
 29         t=i;
 30      break; 
 31   }
 32  }
 33  
 34  if(t==0) return s1;
 35  
 36  for(int j=t;j<len;j++){
 37   s1+=ss[j];
 38  }
 39  
 40  return s1; 
 41 } 
 42 string CutOpra(string s){
 43  string ss,s1;
 44  ss = s;
 45  s1="";
 46     int t=0;
 47  int len = ss.length();
 48  
 49  if(len==1) return s1;
 50  
 51     for(int i=0;i<len;i++){
 52      if(ss[i]<'0'|| ss[i]>'9'){
 53         t=i;
 54      break; 
 55   }
 56  }
 57  
 58  for(int j=t+1;j<len;j++){
 59   s1+=ss[j];
 60  }
 61  
 62  return s1; 
 63 } 
 64 char GetOpra(string s){
 65  string ss,s1;
 66  ss = s;
 67     int t;
 68  int len = ss.length();
 69     for(int i=0;i<len;i++){
 70      if(ss[i]<'0'||ss[i]>'9'){
 71         t=i;
 72      break; 
 73   }
 74  }
 75  char ts;
 76  ts = ss[t];
 77  return  ts;
 78 } 
 79  
 80 //a 栈顶元素 
 81 int Level(char a,char b){         // 优先级比较   大于return1   等于return0   小于return-1 
 82  
 83  if(a=='+'&&(b=='*'||b=='/'||b=='('))                                                 return 1;
 84  else if(a=='+'&&(b=='+'||b=='-'||b=='#'||b==')'))                                    return -1;
 85  
 86  else if(a=='-'&&(b=='*'||b=='/'||b=='('))                                            return 1;
 87  else if(a=='-'&&(b=='+'||b=='-'||b=='#'||b==')'))                                    return -1;
 88  
 89  else if(a=='*'&& b=='(')                                                             return 1;
 90  else if(a=='*'&&(b=='*'||b=='/'||b=='+'||b=='-'||b=='#'||b==')'))                    return -1;
 91  
 92  else if(a=='/'&&(b=='('))                                                            return 1;
 93  else if(a=='/'&&(b=='*'||b=='/'||b=='+'||b=='-'||b=='#'||b==')'))                    return -1;
 94  
 95  else if(a=='('&&(b=='+'||b=='-'||b=='*'||b=='/'||b=='('))                            return 1;
 96  else if(a=='('&& b==')')                                                             return 2;
 97  
 98  else if(a==')'&&(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#'))                    return 1;
 99     else if(a=='#'&&(b=='+'||b=='-'||b=='*'||b=='/'||b=='('))                            return 1;
100     else if(a=='#'&&b=='#')                                                              return 0;
101  
102  else return 100000;
103 }
104  
105  
106  
107 void run(){
108  string s;
109  getline(cin,s,'\n');
110  stack<int> OPND;
111  stack<char> OPTR;
112     OPTR.push('#');
113     while(s!=""){
114      cout<<s<<endl; 
115      cout<<s[0]<<endl;
116      if(s[0]<'0'||s[0]>'9'){
117         char ts = GetOpra(s);
118         cout<<"ts="<<ts<<endl;
119         s=CutOpra(s);
120         cout<<"CutOpra(s)=="<<s<<endl;
121             cout<<"OPTR.top()="<<OPTR.top()<<endl;
122      while(Level(OPTR.top(),ts)==-1) {
123              int a,b;
124              a = OPND.top();
125              OPND.pop();
126              b =  OPND.top();
127              cout<<"a=="<<a<<"  "<<"b=="<<b<<endl;
128              OPND.pop();
129              if(OPTR.top()=='*')  OPND.push(a*b);
130              if(OPTR.top()=='/')  OPND.push(b/a);
131              if(OPTR.top()=='+')  OPND.push(a+b);
132              if(OPTR.top()=='-')  OPND.push(b-a);
133              cout<<"OPND.top()=="<<OPND.top()<<endl;
134              OPTR.pop();
135        cout<<"OPtR.top()=="<<OPTR.top()<<endl;
136            
137        }
138       while(Level(OPTR.top(),ts)==1) {
139              OPTR.push(ts); 
140              break;
141      } 
142            if(Level(OPTR.top(),ts)==2){
143                 OPTR.pop();
144                 ts = ' '; 
145                 cout<<"OPtR.top()=="<<OPTR.top()<<endl;
146      }
147        if(Level(ts,OPTR.top())==0) {
148          
149            cout<<"zhangding="<<OPTR.top()<<endl;
150            OPTR.pop();
151            break; 
152    }  
153   }
154   
155      else{
156      
157      int a = GetNum(s);
158        OPND.push(a);
159        s=CutNum(s);
160   }
161  }
162  
163  
164  
165  
166     while(!OPTR.empty()){
167      
168          cout<<"zhangding="<<OPTR.top()<<endl;
169          OPTR.pop();
170      
171      int a,b;
172          a = OPND.top();     cout<<"a="<<a<<endl;
173          OPND.pop();
174          b =  OPND.top();    cout<<"b="<<b<<endl;
175          OPND.pop();
176          if(OPTR.top()=='*')  OPND.push(a*b);
177          if(OPTR.top()=='/')  OPND.push(b/a);
178          if(OPTR.top()=='+')  OPND.push(a+b);
179          if(OPTR.top()=='-')  OPND.push(b-a);
180          OPTR.pop();
181   }
182   
183   cout<<"OPND.top()="<<OPND.top()<<endl;
184 }
185 int main(){
186  run();
187  return 0;
188 } 

 

posted on 2016-10-23 14:32  任我主宰  阅读(238)  评论(0编辑  收藏  举报