expresscalculate
![](http://images0.cnblogs.com/blog/528013/201310/29212247-b88bc2c000a3494486a85b25d881f8ff.png)
//本程序接受任意含有+、-、*、/和()的数值表达式,数值允许带小数,输入以#结束。如:((2.4+3.5)*2-(2+3.5)/2)*(2+3)# #include <stdio.h> #include "G:\express\mystack.h" char piror[7][7]={'>','>','<','<','<','>','>', '>','>','<','<','<','>','>', '>','>','>','>','<','>','>', '>','>','>','>','<','>','>', '<','<','<','<','<','=','f', '>','>','>','>','f','>','>', '<','<','<','<','<','f','='}; //保存算符优先矩阵 int chartoint(char ch)//将运算符变成数组(算符优先矩阵)下标 { switch(ch) { case '+':return 0; case '-':return 1; case '*':return 2; case '/':return 3; case '(':return 4; case ')':return 5; case '#':return 6; } } int numchar(char ch)//判断是否为数字符号 { if (ch>='0' && ch<='9') return 1; else return 0; } int isoptr(char ch)//判断是否为合法的运算符号 { switch(ch) { case '+': case '-': case '*': case '/': case '(': case ')': case '#':return 1; default :return 0; } } float operate(float n1,char ch,float n2)//对操作数按运算符进行运算 { switch(ch) { case '+':return n1+n2; case '-':return n1-n2; case '*':return n1*n2; case '/':return n1/n2; } } main() { char str[80]; gets(str); int i=0; int number1,exp10; char ch1,ch2,curroptr1; float number,number2,n1,n2,n3,curroptr,ch3; sqstack optr,opnd; initstack(optr);push(optr,'#'); initstack(opnd); while(str[i]!='\0') { if(numchar(str[i])) { number1=0; while(numchar(str[i])) { number1=number1*10+(str[i]-'0'); i++; } //已经读取了一个整数 if (str[i]=='.') { i++; number2=0.0;exp10=10; while(numchar(str[i])) { number2=number2+((str[i]-'0')*1.0)/exp10; exp10=exp10*10; i++; printf("number2_____decimal part=%f\n",number2); } number=number1+number2; printf("number_float=%f\n",number);// 将实数压入堆栈 push(opnd,number); } else { push(opnd,number1);// 将整数压入堆栈 printf("number_____int=%d\n",number1); } } else if(!isoptr(str[i])) { printf("express error---1!\n"); exit(0); } else { ch1=(char)gettop(optr); ch2=str[i]; printf("ch1=%c,ch2=%c\n",ch1,ch2); if(piror[chartoint(ch1)][chartoint(ch2)]=='>') { while(piror[chartoint(ch1)][chartoint(ch2)]=='>') { pop(opnd,n2); pop(opnd,n1); pop(optr,curroptr); curroptr1=(char)curroptr; printf("\n---%f %c %f=====%f\n",n1,curroptr1,n2,operate(n1,curroptr1,n2)); push(opnd,operate(n1,curroptr1,n2)); ch1=(char)gettop(optr); } } if(piror[chartoint(ch1)][chartoint(ch2)]=='<') push(optr,ch2); if(piror[chartoint(ch1)][chartoint(ch2)]=='=') pop(optr,ch3); if(piror[chartoint(ch1)][chartoint(ch2)]=='f') { printf("express error---2!\n"); exit(0); } i++; } } pop(opnd,n3); printf("\nresult=%f\n",n3); }
#include <stdlib.h> #include <stdio.h> #define stackinitsize 50 #define stackincrement 8 typedef float elemtype; typedef struct{ elemtype *base; elemtype *top; int stacksize; }sqstack; int initstack(sqstack &s) {s.base=(elemtype * ) malloc(stackinitsize*sizeof(int)); s.top=s.base; s.stacksize=stackinitsize; return 1; } int push(sqstack &s,elemtype e) { *(s.top)=e; s.top++; return 1; } elemtype gettop(sqstack s) { return *(s.top-1); } int emptystack(sqstack s) {if (s.top==s.base) return 1; else return 0; } int pop(sqstack &s,elemtype &e) { if (emptystack(s)) return 0; --s.top; e=*(s.top); return 1; }