爱问知识人 爱问教育 医院库

用栈实现复杂四则运算的C语言代码

首页

用栈实现复杂四则运算的C语言代码


        

提交回答
好评回答
  • 2008-11-05 21:59:00
      #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; }。
      

    z***

    2008-11-05 21:59:00

类似问题

换一换
  • 互联网 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):