正文

模拟计算器(C++)2006-08-22 12:46:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lost/17749.html

分享到:

  Tag:C++ C 算法   //author: baker//email:baker1203@sina.com//course designing for data structures and program design in c++//time:16/5/2006/*                         a simulant calculatordescription: requiring design a simulant calculator ,which could take operations such as +,-,*,/ ,( ),or functon SQR ,ABScondition:inputted expression can limit into int type.therefore ,you must check the expression for exactness.if the errorstake place ,the program would give an alarm. */ #include<iostream>   #include<conio.h>#include<stack>   #include<string>   #include<cstdlib>#include<math.h>   using namespace std;   void  init(string& s)     //³õʼ»¯ÊäÈë±í´ïʽ.   {  cout<<"enter the expression:";     cin>>s;     s+="#";                      //SÒÔ#½áÊø   }   void error(int tag)    //±¨´íº¯Êý {     switch(tag)     {                case 1:                     cout<<"wrong operator!"<<endl;                     break;                case 2:                     cout<<"wrong operand!"<<endl;                     break;                           }     }      bool is_optr(char c)     //¼ì²éÊäÈëÊÇ·ñΪ ÔËËã·û   {     string   optr_string("+-*/()AS#");     for(int   i=0;i<optr_string.size();i++)     if(c==optr_string[i])     return   true;     return   false;     }       char  precede(char   op1,   char   op2)     {     string   tab[9];     tab[0]=">><<<><<>";     tab[1]=">><<<><<>";     tab[2]=">>>><><<>";     tab[3]=">>>><><<>";     tab[4]="<<<<<=<<E";     tab[5]=">>>>E>>>>";     tab[6]=">>>><>>>>";  tab[7]=">>>><>>>>";   tab[8]="<<<<<E<<=";  string optr_string("+-*/()AS#");         int op1_loc,op2_loc;         for(op1_loc=0;op1_loc<optr_string.size();op1_loc++)           if(optr_string[op1_loc]==op1)break;     for(op2_loc=0;op2_loc<optr_string.size();op2_loc++)        if(optr_string[op2_loc]==op2)break;         return   tab[op1_loc][op2_loc];     }       double operate(double x,char op,double y)     {     switch(op)     {     case   '+':   return   x+y;   break;     case   '-':   return   x-y;   break;     case   '*':   return   x*y;   break;     case   '/':   return   x/y;   break;    case   'A':   return  fabs(y);break;  case   'S':   return  sqrt(y);break;   }     return   -1;     }                       double   cal(string&   s)     {     stack<char>optr;     optr.push('#');     stack<double>opnd;         char   c=s[0];     s.erase(0,1);         while(c!='#'||optr.top()!='#')     {     if(!is_optr(c))     {        if(c>='0'&&c<='9')      {       string   num;             num.insert(num.begin(),c);         int loc=0;         while(!is_optr(s[loc]))         loc++;             string num2(s,0,loc);         num+=num2;         s.erase(0,loc);            double   x=atof(num.c_str());        opnd.push(x);        c=s[0];        s.erase(0,1);     }     else { error(2); return -1;}      }     else    {          switch(precede(optr.top(),c))          {       case   '<':   optr.push(c);   c=s[0];   s.erase(0,1);    break;       case   '=':   optr.pop();    c=s[0];    s.erase(0,1);   break;       case   '>':              char   op;              op=optr.top();             optr.pop();              double   a,b;              a=opnd.top();              opnd.pop();             if(op!='S'&&op!='A')          {           b=opnd.top();              opnd.pop();           }                  double   res;              res=operate(b,op,a);              opnd.push(res);              break;       case  'E':           error(1);           return -1;         }      }     }         return   opnd.top();   }       int main()     {     string expression;     double value;         init(expression);     value=cal(expression);     cout<<value<<endl;  getch();         return   0;     } 曾做过三次修改,主要是运算符优先级. 现已满足要求(胡学刚版课程设计)

阅读(3366) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册