对于基本的算术表达式,以字符序列的形式从终端进行输入,要求语法正确的,不含变量,按照算术运算优先级顺序,实现基本算术表达式的运算过程。 (1) 输入:输入一个算术表达式,以#结束 (2) 输出:输出数据栈栈顶元素和最后表达式的运算结果 (3) 程序功能:完成包含加、减、乘、除运算,包含括号的基本整数表达式的运算,可实现动态的输入,实时的输出。 (4) 测试数据:3*(7-2)和自选数据
1
#include"stdio.h" char A[7]={'+','-','*','/','(',')','#'}; char B[7][7]={{'>','>','<','<','','>'}, {'>','>','<','<','','>'}, {'>','>','>','>','','>'}, {'>','>','>','>','','>'}, {'<','<','<','<','','>','>','>','0','>','>'}, {'<','<','<','<','top=S->base=0; } void Initstack2(stack2 *S) { S->top=S->base=0; } int push1(stack1 *S,char ch) { S->s[S->top]=ch; S->top++; } int push2(stack2 *S,int ch) { S->s[S->top]=ch; S->top++; } int search(char ch) { int i=0; while(ch!=A[i]) { i++; } return i; } char precede(char c1,char c2) { int i,j; i=search(c1); j=search(c2); return B[i][j]; } char gettop1(stack1 S) { char e; if(S.top==S.base) printf("!!!"); e=S.s[S.top-1]; return e; } int gettop2(stack2 S) { int e; if(S.top==S.base) printf("!!!"); e=S.s[S.top-1]; return e; } char pop1(stack1 *S) { if(S->top==S->base) return('!'); else { S->top--; return(S->s[S->top]); } } int pop2(stack2 *S) { if(S->top==S->base) return('!'); else { S->top--; return(S->s[S->top]); } } int operate(int a,char op,int b) { switch(op) { case '+':return(a+b);break; case '-':return(a-b);break; case '*':return(a*b);break; case '/':return(a/b);break; } } int main() { struct stack1 OPTR; struct stack2 OPND; char c,op; int a,b,an; Initstack1(&OPTR); push1(&OPTR,'#'); Initstack2(&OPND); c=getchar(); while(c!='#'||gettop1(OPTR)!='#') { if(c>='0'&&c='0'&&c='0'&&c<='9'); } else { switch(precede(gettop1(OPTR),c)) { case '': op=pop1(&OPTR);a=pop2(&OPND);b=pop2(&OPND); push2(&OPND,operate(b,op,a));break; }//switch } //else } //while an=pop2(&OPND); printf("\nyour answer is:\n=%d",an); }
2022-03-07 21:01:18 3KB 堆栈表达式求值
1
51计算器优化版(可计算运算优先级).zip
2021-10-13 11:02:51 48KB 单片机 C语言
1
c语言运算优先级.docx
2021-03-15 13:05:22 175KB c
1
1)有运算优先级(括号最先,乘方次之,乘除再次之,加减最后等) 2)思路:按运算优先级将数学式子步步化简 3)数学式子用字符串存储,负数用中括号[]括起来表示,如:[-93]
2019-12-21 20:08:13 5KB 计算器代码 运算优先级 括号运算
1
将公式解析成语法树再进行处理,支持自定义函数,支持扩展函数,支持自定义常量。
2019-12-21 19:24:25 52KB C# 公式编辑 自定义 可扩展
1