1、四、 数据结构与算法思想描述顺序读取中缀表达式:1、 当遇到数字时,将数字入数字栈2、 当遇到操作符时,与操作符栈栈顶比较:If(当前操作符优先级大于操作符栈栈顶的优先级) If(非”)”操作符)将当前操作符进操作符栈;ElseWhile(操作符栈栈顶不等于”(“) 取操作符栈栈顶及数字栈的两个数进行运算,并将结果压入数字栈; If(非(“操作符)取操作符栈栈顶及数字栈的两个数进行运算,并将结果压入数字栈;Continue;(直到当前操作符比栈顶操作符优先级大) Else 将当前操作符进操作符栈;3、 While(操作符栈非空)操作符栈栈顶及数字栈的两个数进行运算,并将结果压入数字栈;4、 在
2、数字栈取最后结果并输出。五、 程序清单/10*82+5*5+/4-(-10)+ = /100+(-100)-(-102) = 100/(2016-2017+(2015-2014) = 0/-1+(10)+100%102 = 0#includeiomanipmapusing namespace std;const int MAX = 105;typedef double Type;typedef struct Type TypeStackMAX; char charStackMAX; int TypeTop, charTop;Stack;/初始化栈void InitStack(Stack *S)
3、 S-charTop = S-TypeTop = 0;/判断charStack是否为空bool IsEmpty_Char(Stack S) return = 0;/判断TypeStack是否为空bool IsEmpty_Type(Stack S)/判断charStack是否为满bool IsFull_Char(Stack S) return = MAX;/判断TypeStack是否为满bool IsFull_Type(Stack S)void Push_Char(Stack *S, char ch) /charStack不为满则入栈,否则输出提示 if(!IsFull_Char(*S) S-c
4、harStackS-charTop+ = ch; else cout The CharStack Is Full! TypeTop+ = a; The TypeStack Is Full!char Pop_Char(Stack *S)IsEmpty_Char(*S) charTop-; return S-charTop; The CharStack Is Empty! return -1;Type Pop_Type(Stack *S)IsEmpty_Type(*S)TypeTop-;TypeTop; The TypeStack Is Empty!char Top_Char(Stack S)Is
5、Empty_Char(S) return ;Type Top_Type(Stack S)IsEmpty_Type(S)Type Calculate(Type left, Type right, char op) Type value = 0; switch(op) case +: value = left + right; break;- value = left - right;* value = left * right;/ if(right != 0) value = left / right; else cout while(right-) value *= left; right =
6、 -right; value /= left; */ return value;void Computer(char *mid_equotion, Type len) Type right, left , result; char *p_mid_equotion = mid_equotion; char after_equotion = ; map Oper; Oper# = 1;( = 2; = 3; = 4; = 5;) = 6; Stack MyStack; InitStack(&MyStack); Push_Char(&MyStack,); char top_oper, current
7、_oper; for(;*p_mid_equotion != 0) top_oper = Top_Char(MyStack); current_oper = *p_mid_equotion; if(!Opercurrent_oper) Push_Type(&MyStack,strtod(p_mid_equotion, &p_mid_equotion); continue; /end if else /为操作符 if(Opercurrent_oper Opertop_oper) if(current_oper ! Push_Char(&MyStack,current_oper); while(t
8、op_oper ! right = Pop_Type(& if(!IsEmpty_Type(MyStack) left = Pop_Type(& else left = 0; Push_Type(&MyStack,Calculate(left, right, Top_Char(MyStack); Pop_Char(& top_oper = Top_Char(MyStack); Pop_Char(& /end else /end if else if(current_oper = if(*(p_mid_equotion + 1) = MyStack,0); right = Pop_Type(&
9、if(! left = Pop_Type(& else left = 0; Push_Type(&MyStack,Calculate(left, right, top_oper); continue; /end else /end else p_mid_equotion+; /end for top_oper = Pop_Char(& while(top_oper ! right = Pop_Type(& left = Pop_Type(& else left = 0; Push_Type(& top_oper = Pop_Char(&/ cout setprecision(6) nThe Result = (result = Pop_Type(&MyStack) printf(The Result = %lfnn,(result = Pop_Type(&MyStack);int main() char sMAX = Type i = 0; cout s & strcmp(s,-1) ! Computer(s,strlen(s); return 0;六、 程序执行结果及其分析对 “+” , “-” , “*” , “/” , “%” , “” 运算的实现可运算多位数和小数,求余,求平方,括号里包含负数如(-1),及首个数字为负数如-1+1
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1