王道数据结构之中缀转后缀并计算(栈)——考研复习笔记

所实现的算法:

  • 括号匹配检查
  • 中缀表达式转后缀表达式
  • 计算转乘后缀后的表达式

 数据类型

//用来转后缀表达式 
typedef struct{
	char data[MaxSize];
	int top;
}SqStack,SNode;

  

//用来计算后缀表达式 
typedef struct{
	float Num[MaxSize];
	int top;
}NuStack,NNode;

  

 

完整代码:

  1 /*    中缀转后缀并计算 
  2 */
  3 #include <iostream>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 
  7 using namespace std;
  8 #define MaxSize 100
  9 
 10 typedef struct{
 11     char data[MaxSize];
 12     int top;
 13 }SqStack,SNode;
 14 
 15 
 16 typedef struct{
 17     float Num[MaxSize];
 18     int top;
 19 }NuStack,NNode;
 20 
 21 
 22 //运算符优先级 
 23 int Priority(char op){
 24     switch(op){
 25         case '+':
 26             return 1;
 27         case '-':
 28             return 1;
 29         case '*':
 30             return 2;
 31         case '/':
 32             return 2;
 33         default:
 34             return 0;
 35     }
 36 }
 37 
 38 void Init(SqStack &P){
 39     P.top=-1;
 40 } 
 41 
 42 void Init_Nu(NuStack &S)
 43 {
 44     S.top=-1;
 45 }
 46 
 47 bool Empty(SqStack P){
 48     if (P.top==-1) return true;
 49     return false;
 50 }
 51 
 52 bool Empty_Num(NuStack S){
 53     if (S.top==-1) return true;
 54     return false;
 55 }
 56 
 57 bool Push(SqStack &P, char ch){
 58     P.data[++P.top]=ch;
 59     return true;
 60 } 
 61 
 62 bool Push_Nu(NuStack &S, float ch){
 63     //cout<<ch<<endl;
 64     S.Num[++S.top]=ch;
 65     return true;
 66 } 
 67 
 68 bool Pop_1(SqStack &P){
 69     P.top--;
 70     return true;
 71 }
 72 
 73 bool Pop_2(NuStack &S,float &x){
 74     x=S.Num[S.top--];
 75     return true;
 76 }
 77 
 78 char GetTop(SqStack S){
 79     return S.data[S.top];
 80 }
 81 
 82 float GetNum(NuStack S){
 83     return S.Num[S.top];
 84 }
 85 
 86 //转后缀并计算 
 87 void Convert(NuStack &S,SqStack &P,char ch[],int len){
 88     char Cov_ch[MaxSize];
 89     int cnt =0;
 90     for(int i=0;i<len;i++){
 91         if(ch[i]<='9'&&ch[i]>='0'){
 92             Cov_ch[cnt++]=ch[i];
 93         }else{
 94             if(Empty(P)) Push(P,ch[i]);
 95             else if(ch[i]=='(') Push(P,ch[i]);
 96             else if(ch[i]==')'){
 97                 while(GetTop(P)!='('){
 98                     Cov_ch[cnt++]=GetTop(P);
 99                     Pop_1(P);
100                 }
101             Pop_1(P);
102             }
103             else{
104                 while(Priority(ch[i])<=Priority(GetTop(P))){
105                     Cov_ch[cnt++]=GetTop(P);
106                     Pop_1(P);
107                     if(Empty(P)) break;
108                 }
109                 Push(P,ch[i]);
110             }            
111         }
112     }
113     while(!Empty(P)){
114         Cov_ch[cnt++]=GetTop(P);
115         Pop_1(P);
116     }
117     
118     for(int i=0;i<cnt;i++){
119         cout<<Cov_ch[i]; 
120     }
121     puts("");
122     float a,b;
123     for(int i=0;i<cnt;i++){
124         if(Cov_ch[i]>='0'&&Cov_ch[i]<='9'){
125             Push_Nu(S,Cov_ch[i]-'0');
126         }else{
127             switch(Cov_ch[i]){
128                 case '+':
129                     Pop_2(S,a);
130                     Pop_2(S,b);
131                     Push_Nu(S,b+a);
132                     break;
133                 case '-':
134                     Pop_2(S,a);
135                     Pop_2(S,b);
136                     Push_Nu(S,b-a);
137                     break;
138                 case '*':
139                     Pop_2(S,a);
140                     Pop_2(S,b);
141                     Push_Nu(S,a*b);
142                     break;
143                 case '/':
144                     Pop_2(S,a);
145                     Pop_2(S,b);
146                     Push_Nu(S,b/a);
147                     break;
148                 default:
149                     break; 
150             }    
151         }
152     }
153     cout<<GetNum(S);
154 }
155 //括号检查 
156 bool bracketCheck(char str[],int ll){  
157     SqStack Q;
158     Q.top=-1;
159     char x;
160     char bracket[MaxSize];
161     int cnt=0;
162     for(int i=0;i<ll;i++){
163         if(str[i]=='('||str[i]==')') bracket[cnt++]=str[i];
164     }
165     for(int i=0;i<cnt;i++){
166         if(bracket[i]=='('){
167             Push(Q,bracket[i]);
168         }
169         else{
170             if(Empty(Q)) return false;
171             x=GetTop(Q);
172             Pop_1(Q);
173             if((bracket[i]==')'&&x!='('))
174                  return false;
175         }
176     }
177     return Empty(Q);
178 }
179 
180 int main(){
181     SqStack P;
182     NuStack S;
183     Init(P);
184     Init_Nu(S);
185     char ch[MaxSize];
186     cin>>ch;
187     int len = strlen(ch);
188     if(!bracketCheck(ch,len)){
189         cout<<"It's a wrong expression"<<endl;
190     }else{
191         Convert(S,P,ch,len);
192     }
193     return 0;
194 } 

 

posted @ 2020-07-21 21:27  Drake丶  阅读(361)  评论(0编辑  收藏  举报