C语言实现算术表达式求值

编程入门 行业动态 更新时间:2024-10-19 14:32:53

C语言实现<a href=https://www.elefans.com/category/jswz/34/1747979.html style=算术表达式求值"/>

C语言实现算术表达式求值

话不多数代码如下,看不懂的同学请评论区留言哦

#include<stdio.h>
#include <string.h>
#include <conio.h>
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
char a[]= {'+','-','*','/','(',')','#'};
int PriorityTable[7][7]= {{ 1, 1,-1,-1,-1, 1, 1},{ 1, 1,-1,-1,-1, 1, 1},{ 1, 1, 1, 1,-1, 1, 1},{ 1, 1, 1, 1,-1, 1, 1},{-1,-1,-1,-1,-1, 0, NO},{ 1, 1, 1, 1,NO, 1, 1},{-1,-1,-1,-1,-1,NO, 0}
};
int menu(void);
void InputExpression(char str[])
{int len;printf("请输入算术表达式:\n");scanf("%s",str);len=strlen(str);str[len]='#';str[len+1]='\0';
}
int GetCharType(char ch)
{int i;for(i=0; i<NUM; i++) if(ch==a[i]) return(i);if(ch>='0' && ch<='9') return(DIGIT);if(ch=='.') return(POINT);return(-1);
}
double Operate(double a,int theta,double b)
{double x;switch(theta){case 0:x=a+b;break;case 1:x=a-b;break;case 2:x=a*b;break;case 3:x=a/b;break;}return (x);
}
int EXCUTE(char *str,double *Result)
{int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE];double number,temp,OPND[STACKSIZE];OPTR[0]=STARTEND;topTr=1;topNd=0;pp=0;while((str[pp])){CharType=GetCharType(str[pp]);switch(CharType){case -1:return(0);case DIGIT:number=0;while(str[pp]>='0' && str[pp]<='9'){number=number*10+(str[pp]-48);pp++;}if(str[pp]=='.'){temp=10.0;pp++;while(str[pp]>='0' && str[pp]<='9'){number=number+(str[pp]-48)/temp;temp=temp*10;pp++;}}OPND[topNd]=number;topNd++;break;case POINT:number=0;temp=10.0;pp++;while(str[pp]>='0' && str[pp]<='9'){number=number+(str[pp]-48)/temp;temp=temp*10;pp++;}OPND[topNd]=number;topNd++;break;case PLUS:case MINUS:case POWER:case DIVIDE:if(PriorityTable[OPTR[topTr-1]][CharType]==-1){OPTR[topTr]=CharType;topTr++;pp++;}else{OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);topNd--;topTr--;}break;case LEFTP:OPTR[topTr]=CharType;topTr++;pp++;break;case RIGHP:while(OPTR[topTr-1]!=LEFTP){if(OPTR[topTr-1]==STARTEND)return(0);if(PriorityTable[OPTR[topTr-1]][CharType]==1){OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);topNd--;topTr--;}elsebreak;}topTr--;pp++;break;case STARTEND:while(OPTR[topTr-1]!=STARTEND){OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);topNd--;topTr--;}if(topNd==1){*Result=OPND[0];return(1);}elsereturn(0);}}return(1);
}
void main()
{int num,flag;double result;char str[256];str[0]='0';while(1){num=menu();switch(num){case 1:InputExpression(str);flag=0;printf("%s\n",str);getchar();break;case 2:if(str[0]=='0'){printf("表达式为空!");getchar();break;}if(!EXCUTE(str,&result)){printf("表达式有错!\n");getchar();}else{printf("计算结束!\n");getchar();flag=1;}break;case 3:if(flag){printf("#%s=%lf\n",str,result);getchar();}break;case 4:break;}if(num==4) break;}
}
int menu(void)
{int num;printf("*----------1--输入表达式------------*\n",' ');printf("*----------2--计算表达式------------*\n",' ');printf("*----------3--输出结果--------------*\n",' ');printf("*----------4--退出------------------*\n",' ');printf("*----------请选择操作1,2,3,4--------:");do{scanf("%d",&num);}while(num<1 || num>4);return(num);
}


 

更多推荐

C语言实现算术表达式求值

本文发布于:2024-02-27 05:14:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1705309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算术   表达式   语言   求值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!