数学表达式计算器
发布时间:2020-12-16 07:48:19 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #include stdio.h#include stdlib.h#define MAXSIZE32typedef struct{int data[MAXSIZE];//数据段int top;//栈指针}sqstack;sqstack *sqstack_create(
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #include <stdio.h> #include <stdlib.h> #define MAXSIZE 32 typedef struct{ int data[MAXSIZE];//数据段 int top;//栈指针 }sqstack; sqstack *sqstack_create() { sqstack *sq; sq = malloc(sizeof(*sq)); if(sq == NULL ) { return NULL; } sq->top = -1; return sq; } int sqstack_push(sqstack *sq,int *data)//入栈 { if(sq->top == MAXSIZE-1)//full { return -1; } else { sq->data[++sq->top] = *data; } return 0; } int sqstack_top(sqstack *sq,int *data)//取得栈顶数据 { if(sq->top == -1)//empty { return -1; } *data = sq->data[sq->top]; return 0; } int sqstack_pop(sqstack *sq,int *data)//出栈 { if(sq->top == -1)//empty { return -1; } *data = sq->data[sq->top--]; return 0; } int compute(sqstack *snum,int ope) { int n1,n2,n; sqstack_pop(snum,&n1); sqstack_pop(snum,&n2); switch(ope) { case '+': n=n1+n2; printf("%d+%d=%dn",n1,n); break; case '-': n=n2-n1; printf("%d-%d=%dn",n);break; case '*': n=n1*n2; printf("%d*%d=%dn",n);break; case '/': n=n2/n1; printf("%d/%d=%dn",n);break; default: return -1;//break; } sqstack_push(snum,&n);//将运算结果压入数字栈 return 0; } void deal_bracket(sqstack *snum,sqstack *sope)//处理括号 { int old_ope; sqstack_top(sope,&old_ope);//取得栈顶运算符 while(old_ope != '(') { sqstack_pop(sope,&old_ope); compute(snum,old_ope); sqstack_top(sope,&old_ope);//取得栈顶运算符 } sqstack_pop(sope,&old_ope); } int sqstack_is_empty(sqstack *sq) { return (sq->top == -1); } int get_pri(int ope) { switch(ope) { case '(': return 0; case '+': case '-': return 1; case '*': case '/': return 2; } } void deal_ope(sqstack *snum,sqstack *sope,int ope)//处理运算符 { int old_ope; if(ope == '('||sqstack_is_empty(sope)) { sqstack_push(sope,&ope); return ; } sqstack_top(sope,&old_ope); if(get_pri(ope) > get_pri(old_ope)) { sqstack_push(sope,&ope); return ; } while(get_pri(ope) <= get_pri(old_ope))// * + { sqstack_pop(sope,old_ope); if(sqstack_is_empty(sope)) break; sqstack_top(sope,&old_ope); } sqstack_push(sope,&ope); } int main() { sqstack *snum;//运算数字栈 sqstack *sope;//运算符栈 int i = 0,value = 0,flag = 0,old_ope; char str[] = "1+3-(2*5)*(8-6)+5";//所要计算的表达式 //初始化 snum = sqstack_create(); sope = sqstack_create(); while(str[i] != ' ') { if(str[i] >= '0' && str[i] <= '9')//数字 { value = value*10 + str[i]-'0'; flag = 1; } else//运算符 { if(flag == 1)//取得了运算的数字 { sqstack_push(snum,&value);//数字入 运算数字栈 value = 0; flag = 0; } if(str[i] == ')') { deal_bracket(snum,sope); } else//(+-*/ { deal_ope(snum,sope,str[i]); } } i++; } if(flag) { sqstack_push(snum,&value); } while(!sqstack_is_empty(sope)) { sqstack_pop(sope,old_ope); } sqstack_pop(snum,&value); printf("%s = %dn",str,value); return 0; } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- C++标准模板库map的常用操作
- 依赖注入获得一个对象却想返回不同的值(Error)
- 在VSO或VS2015中创建Git分支标记
- ruby-on-rails – 使用select2(和simple_form)对Rails进行可
- open-vm-tools-dkms : 依赖: open-vm-tools (>= 2:9.4.0-12
- C#:检索已安装的屏幕保护程序的名称
- VB阶段学习小结及感想
- c# – 带有ASP.Net MVC RouteConfig的AngularJS Ui-Router.
- fastjson排序问题-如何改变fastjson的排序规则
- 如何在Swift3中获取Json包的内容(unwrap Json package)