用栈实现复杂四则运算的C语言代码
#include <cstdio> #include <cstdlib> #include <cstring> class CalcInfixExp { public: CalcInfixExp(); char calc(double &result, const char *s); void error(char n); char GetOrder(char a, char b); bool NumPush(const char &a); bool NumPop(char &a); bool OpePush(const char &a); bool OpePop(char &a); private: int p, q; char ope[100]; char num[100]; }; CalcInfixExp::CalcInfixExp() { p=q=0; }; bool CalcInfixExp::NumPush(const char &a) { if (q>=100) return false; num[q++]=a; return true; } char CalcInfixExp::calc(double &result, const char *s) { bool PopRight; char f, szA[20], order; int i; double a, b, c; p=q=0; //清空ope栈和num栈 OpePush('\0'); if (*s=='+') s++; else if (*s=='-') { s++; OpePush('-'); NumPush('0'); NumPush(','); } do { if (*s>='0' && *s<='9' || *s=='。
') { do NumPush(*s++); while (*s>='0' && *s<='9' || *s=='。'); NumPush(','); } else { OpePop(f); switch (order=GetOrder(f, *s)) { case '<': OpePush(f); OpePush(*s++); if (*s=='+') s++; else if (*s=='-') { s++; OpePush('-'); NumPush('0'); NumPush(','); } break; case '>': //从num栈中获取第一个操作数 i=0; if (q==0) { error('\4'); //num栈已空 return false; } NumPop(szA[i]); do NumPop(szA[i]); while (szA[i++]!=','); szA[--i]='\0'; strrev(szA); b=atof(szA); //从num栈中获取第二个操作数 i=0; if (q==0) { error('\4'); //num栈已空 return false; } do PopRight=NumPop(szA[i]); while (szA[i++]!=',' && PopRight); if (PopRight) NumPush(','); szA[--i]='\0'; strrev(szA); a=atof(szA); //计算 switch (f) { case '+': c=a+b; break; case '-': c=a-b; break; case '*': c=a*b; break; case '/': c=a/b; break; } gcvt(c, 16, szA); i=strlen(szA)-1; if (szA[i]=='。
') szA[i]='\0'; //把计算结果压入栈 i=0; do NumPush(szA[i]); while (szA[++i]!='\0'); NumPush(','); break; case '=': s++; break; default: error(order); return false; } } }while (p!=0); result=atof(num); return true; } void CalcInfixExp::error(char n) { switch (n) { case '\1': printf("括号不匹配\n"); break; case '\2': printf("右括号与左括号间没有运算符\n"); break; case '\3': printf("低级括号内包含高级括号\n"); break; case '\4': printf("运算符过多\n"); } } char CalcInfixExp::GetOrder(char a, char b) { static char order[11][11]={ '>','>','<','<','<','>','<','>','<','>','>', '>','>','<','<','<','>','<','>','<','>','>', '>','>','>','>','<','>','<','>','<','>','>', '>','>','>','>','<','>','<','>','<','>','>', '<','<','<','<','<','=','\3','\1','\3','\1','\1', '>','>','>','>','\2','>','>','>','>','>','>', '<','<','<','<','<','\1','<','=','\3','\1','\1', '>','>','>','>','\2','\3','=','>','>','>','>', '<','<','<','<','<','\1','<','\1','<','=','\1', '>','>','>','>','\2','\3','\2','\3','\2','>','>', '<','<','<','<','<','\1','<','\1','<','\1','=' }; int i[2], j=0, TreatB=0; do { switch (a) { case '+': i[j]=0; break; case '-': i[j]=1; break; case '*': i[j]=2; break; case '/': i[j]=3; break; case '(': i[j]=4; break; case ')': i[j]=5; break; case '[': i[j]=6; break; case ']': i[j]=7; break; case '{': i[j]=8; break; case '}': i[j]=9; break; case '\0': i[j]=10; break; default: return '\0'; } j++; a=b; }while (TreatB^=1); return order[i[0]][i[1]]; } bool CalcInfixExp::NumPop(char &a) { if (q==0) return false; a=num[--q]; return true; } bool CalcInfixExp::OpePush(const char &a) { if (p>=100) return false; ope[p++]=a; return true; } bool CalcInfixExp::OpePop(char &a) { if (p==0) return false; a=ope[--p]; return true; } int main() { CalcInfixExp s; double result; char a[100]; while (gets(a)) if ( lc(result, a)) printf("%lg\n", result); return 0; }。
答:我是软件品质管理的,我就北方IT这个圈子里给你一些建议 首先你要考虑好你对计算机哪方面的技术感兴趣 要编程SUN认证不错 要做软件测试、品质管理,微软、IBM认...详情>>
答:同学习的目的是什么? 1、为了一技之长,养家糊口 2、为了一个兴趣爱好 3、为了考试及格 4、为了.... 总之,不管是学什么,目的都是为了提高在某个方面的知识...详情>>