1 #include <iostream> 2 #include<stdlib.h> 3 4 using namespace std; 5 6 #define STACK_INIT_SIZE 100 7 #define STACKINCREASE 10 8 9 //為了簡化函數,數據棧和符號棧用了一種結構體(雖然我知道可以用共用體解決問題,嫌煩), 10 //由此導致的后果是接下來所有的運算過程中不允許出現小數,而且由於是利用ASCII表來儲存整數, 11 //所以要求運算數以及運算過程中數不能超過表中的最大值,暫時沒有考慮負數問題,而且不能在輸入時出現兩位數 12 //以上問題全是由於我把數字當成字符處理的結果,單純圖個方便 13 typedef struct 14 { 15 char *base; 16 char *top; 17 int stacksize; 18 }SqStack; 19 20 21 int InitStack(SqStack &S) 22 { 23 S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char)); 24 if(!S.base) 25 { 26 cout<<"分配空間失敗!"; 27 exit(-1); 28 } 29 S.top=S.base; 30 S.stacksize=STACK_INIT_SIZE; 31 return 0; 32 } 33 34 35 int Push(SqStack &S,char e) 36 { 37 if((S.top-S.base)>=STACK_INIT_SIZE) 38 { 39 S.base=(char *)realloc(S.base,(STACK_INIT_SIZE+STACKINCREASE)*sizeof(char)); 40 if(!S.base) 41 { 42 cout<<"分配空間失敗!"; 43 exit(-1); 44 } 45 S.top=S.base+STACK_INIT_SIZE; 46 S.stacksize=STACK_INIT_SIZE+STACKINCREASE; 47 } 48 *(S.top)=e;//結構體 49 S.top++; 50 return 0; 51 } 52 53 54 int Pop(SqStack &S,char &e) 55 { 56 if(S.base==S.top) 57 { 58 cout<<"棧為空!"; 59 exit(0); 60 } 61 S.top--; 62 e=*(S.top); 63 return 0; 64 } 65 66 int GetTop(SqStack &S,char &e) 67 { 68 if(S.base==S.top) 69 { 70 cout<<"棧為空!"; 71 return 0; 72 } 73 else 74 { 75 e=*(S.top-1); 76 return 1; 77 } 78 } 79 80 81 int EmptyStack(SqStack &S) 82 { 83 if(S.base==S.top) return 1;//stack is empty! 84 else return 0;//stack is not empty! 85 } 86 87 88 int Precede(char a,char b)//a為符號棧棧頂元素,b為待插入的元素 89 { 90 int i;//i=1入棧,i=0彈出操作符以及操作數進行計算 91 if((a=='+'||a=='-')&&(b=='*'||b=='/')) i=1; 92 if((a=='+'||a=='-')&&(b=='+'||b=='-')) i=0; 93 if((a=='*'||a=='/')&&(b=='*'||b=='/')) i=0; 94 if((a=='*'||a=='/')&&(b=='+'||b=='-')) i=0; 95 if(a=='(') i=1; 96 return i; 97 } 98 99 int EvaluateExpression(char *q) 100 { 101 char *p = q; 102 char a,b,c,d,e,f; 103 int i,j; 104 SqStack S1,S2;//S1為操作符棧,S2為操作數棧 105 InitStack(S1); 106 InitStack(S2); 107 c=*p++; 108 while(c!='#') 109 { 110 if(c>=48&&c<=57) Push(S2,c);//輸入為數字 111 if(c=='(') Push(S1,c); //輸入為左括號 112 if(c==')')//輸入為右括號 113 { 114 if(!EmptyStack(S1)) GetTop(S1,e); 115 while(e!='(') 116 { 117 Pop(S2,a); 118 Pop(S2,b); 119 Pop(S1,d); 120 if(d=='+') j=(b-48)+(a-48); 121 if(d=='-') j=(b-48)-(a-48); 122 if(d=='*') j=(b-48)*(a-48); 123 if(d=='/') 124 { 125 if(a-48) j=(b-48)/(a-48); 126 else {cout<<"計算過程出現除數為零的錯誤!"<<endl; return 0;} 127 } 128 f=char(j+48); 129 Push(S2,f); 130 if(!EmptyStack(S1)) GetTop(S1,e);//直到遇到左括號 131 if(e=='(') Pop(S1,e); 132 } 133 } 134 if(c=='+'||c=='-'||c=='*'||c=='/') 135 { 136 if(EmptyStack(S1)) Push(S1,c); 137 else 138 { 139 GetTop(S1,e); 140 i=Precede(e,c); 141 if(i==0) 142 { 143 Pop(S2,a); 144 Pop(S2,b); 145 Pop(S1,d); 146 if(d=='+') j=(b-48)+(a-48); 147 if(d=='-') j=(b-48)-(a-48); 148 if(d=='*') j=(b-48)*(a-48); 149 if(d=='/') 150 { 151 if(a-48) j=(b-48)/(a-48); 152 else {cout<<"計算過程出現除數為零的錯誤!"<<endl; return 0;} 153 } 154 f=char(j+48); 155 Push(S1,c); 156 Push(S2,f); 157 } 158 if(i==1) Push(S1,c); 159 } 160 } 161 c=*p++; 162 } 163 if(!EmptyStack(S1)) 164 { 165 while(!EmptyStack(S1)) 166 { 167 Pop(S2,a); 168 Pop(S2,b); 169 Pop(S1,d); 170 if(d=='+') j=(b-48)+(a-48); 171 if(d=='-') j=(b-48)-(a-48); 172 if(d=='*') j=(b-48)*(a-48); 173 if(d=='/') j=(b-48)/(a-48); 174 f=char(j+48); 175 Push(S2,f); 176 } 177 } 178 //最后輸出結果 179 Pop(S2,a); 180 i=a-48; 181 while(*q!='#') cout << *q++; 182 cout << " 的運算結果為:"<<i<<endl; 183 return 0; 184 } 185 186 187 int main() 188 { 189 //數據測試 190 char *p1="3+2*5#";//=13 191 char *p2="2*5+3#";//=13 192 char *p3="((3+5*2)+2)/5+6/3*2+3#";//=10 193 char *p4="9+(3-1)*3+6/2#";//=18 194 EvaluateExpression(p1); 195 EvaluateExpression(p2); 196 EvaluateExpression(p3); 197 EvaluateExpression(p4); 198 return 0; 199 }
思路參考:http://www.cnblogs.com/dolphin0520/p/3708602.html