四则运算表达式求值

来源:互联网 发布:彩虹六号围攻n卡优化 编辑:程序博客网 时间:2024/04/26 16:50

四则运算表达式求值


Time Limit:

2000MS

 
Memory Limit:

65535K

Description

请实现简单四则运算表达式求值程序。表达式中的运算符包含“+”、 “-”、 “*”、 “/”、 “(”和“)”,分别表示加、减、乘、整除、左括号、右括号,表达式的操作数都是整数类型。程序的输入是表达式字符串,输出是运算的结果。

Input

第一行输入字符串的个数n,从第2行到第n+1行每一行对应一个表达式字符串,该字符串的长度不会超过1024。

Output

输出n行,分别对应输入的n个表达式的值。

Sample Input

3

100*3

10+4/2

5*(1+2)

Sample Output

300

12

15

Hint

可以借助栈来实现。

Source



C++解题代码:
#include <iostream>#include<stack> using namespace std;//区分判断表达式中的操作数和操作符bool IsOperator(char ch)  {      if (ch == '+' || ch == '-' ||          ch == '*' || ch == '/' ||          ch == '(' || ch == ')' || ch == '#')          return true;      else          return false;            }  //运算符的优先关系                           //'+', '-', '*', '/', '(', ')', '#'    char OprRelation[7][7] =   {{'>', '>', '<', '<', '<', '>', '>'}, //'+'                             {'>', '>', '<', '<', '<', '>', '>'}, //'-'                             {'>', '>', '>', '>', '<', '>', '>'}, //'*'                             {'>', '>', '>', '>', '<', '>', '>'}, //'/'                             {'<', '<', '<', '<', '<', '=', '>'}, //'('                             {'>', '>', '>', '>', '=', '>', '>'}, //')'                             {'<', '<', '<', '<', '<', ' ', '='}};//'#'    //将运算符转化为数组下标以便进行优先级比较int ConvertToIndex(char opr)  {      int index;        switch (opr)      {      case '+':          index = 0;          break;      case '-':          index = 1;          break;      case '*':          index = 2;          break;      case '/':          index = 3;          break;      case '(':          index = 4;          break;      case ')':          index = 5;          break;      case '#':          index = 6;          break;      }        return index;  }    //运算符优先级比较char Precede(char opr1, char opr2)  {      int index1 = ConvertToIndex(opr1);      int index2 = ConvertToIndex(opr2);        return OprRelation[index1][index2];  }   //进行运算符转化int Operate(int opnd1, char op, int opnd2)  {      int ret;        switch(op)      {      case '+':          ret = opnd1 + opnd2;          break;      case '-':          ret = opnd1 - opnd2;          break;      case '*':          ret = opnd1 * opnd2;          break;      case '/':          ret = opnd1 / opnd2;          break;      }        return ret;  }    //运算符优先算法  int CaculateExpression(string exp)  {      stack<char> optr; //只处理+ - # / ()运算      stack<int> opnd;       char ch;      int i = 0;            exp += "#";      optr.push('#');            ch = exp[i++];            //如果##配对,表达式求值完成      while (ch != '#' || optr.top() != '#')      {          if (!IsOperator(ch))          {              //操作数入栈  int a=0;while(!IsOperator(ch)){ a=a*10+ch-'0';       //将String转化为int ch=exp[i++];}            opnd.push(a);                   }          else          {              //比较栈顶操作符和新取得的操作符的优先关系              switch (Precede(optr.top(), ch))              {              case '<'://栈顶优先权低                  optr.push(ch);                  ch = exp[i++];                  break;              case '='://括号配对,栈顶括号弹出                  optr.pop();                  ch = exp[i++];                  break;              case '>'://栈顶优先权高,先弹出,计算,结果操作数入栈                  char op = optr.top();                  optr.pop();                  int num2 = opnd.top();//第二个操作数在前                  opnd.pop();                  int num1 = opnd.top();                  opnd.pop();                                    int ret = Operate(num1, op, num2);                                    opnd.push(ret);                  break;              }          }      }//end of while         //操作数栈的唯一元素即为计算结果      return opnd.top();  }  int main(){char a[1025];int res;int n;cin>>n;for(int i=0;i<n;i++){cin>>a;string s=string (a);       res=CaculateExpression(s)  ;   cout<<res<<endl;}}

欢迎各位大牛指教!!!

参考资料:
http://blog.csdn.net/lilypp/article/details/6546658#







原创粉丝点击