Advertisement
Guest User

递归下降求解表达式

a guest
Nov 3rd, 2022
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. enum Error_Type{
  6.     NONE_ERROR,
  7.     ERR_DIV_ZERO,
  8.     ERR_DISMATCH_RBRACKET,
  9.     ERR_MULTIPLE_DOT
  10. };
  11. static string SError_Type[]{
  12.     "NONE_ERROR",
  13.     "ERR_DIV_ZERO",
  14.     "ERR_DISMATCH_RBRACKET",
  15.     "ERR_MULTIPLE_DOT"
  16. };
  17.  
  18. class Calculator
  19. {
  20. public:
  21.     Calculator()
  22.     {
  23.         expression = "";
  24.         pos = 0;
  25.         error = NONE_ERROR;
  26.     }
  27.     double operator()(const string &exp)
  28.     {
  29.         expression = removeBlank(exp);
  30.         pos = 0;
  31.         error = NONE_ERROR;
  32.         double value=evalExpression();
  33.         if(error){
  34.             cout<<SError_Type[error]<<endl;
  35.         }
  36.         return value;
  37.     }
  38.  
  39. private:
  40.    
  41.  
  42.     string expression;
  43.     int pos;
  44.     Error_Type error;
  45.  
  46.     string removeBlank(const string &exp)
  47.     {
  48.         string text;
  49.         for (auto c : exp)
  50.         {
  51.             if(isdigit(c)||c=='.'||c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'){//过滤输入,只允许特定的运算符,数字,小数点进入expression
  52.                 text+=c;
  53.             }
  54.         }
  55.         return text;
  56.     }
  57.     bool end()
  58.     {
  59.         return expression[pos] == '\0';
  60.     }
  61.     double evalExpression()
  62.     {
  63.         double term;
  64.         double result = 0;
  65.         char op;
  66.         term = evalTerm();
  67.         if (error)
  68.         {
  69.             return 0;
  70.         }
  71.  
  72.         result = term;
  73.         while (!end())
  74.         {
  75.             op = expression[pos];
  76.             if (op != '+' && op != '-')
  77.             {
  78.                 break;
  79.             }
  80.             ++pos;
  81.             term = evalTerm();
  82.             if (error)
  83.             {
  84.                 return 0;
  85.             }
  86.             switch (op)
  87.             {
  88.             case '+':
  89.                 result += term;
  90.                 break;
  91.             case '-':
  92.                 result -= term;
  93.                 break;
  94.             }
  95.         }
  96.         return result;
  97.     }
  98.     double evalTerm()
  99.     {
  100.         double factor;
  101.         double result = 1;
  102.         char op;
  103.         factor = evalFactor();
  104.         if (error)
  105.         {
  106.             return 0;
  107.         }
  108.         result = factor;
  109.         while (!end())
  110.         {
  111.             op = expression[pos];
  112.             if (op != '*' && op != '/')
  113.             {
  114.                 break;
  115.             }
  116.             ++pos;
  117.             factor = evalFactor();
  118.             switch (op)
  119.             {
  120.             case '*':
  121.                 result *= factor;
  122.                 break;
  123.             case '/':
  124.                 if (factor == 0)//避免检查错误
  125.                 {
  126.                     error = ERR_DIV_ZERO;
  127.                     return 0;
  128.                 }
  129.                 result /= factor;
  130.             }
  131.         }
  132.         return result;
  133.     }
  134.     double evalFactor()
  135.     {
  136.         double result;
  137.         if (expression[pos] == '(')
  138.         {
  139.             ++pos;
  140.             result = evalExpression();
  141.             if(error){
  142.                 return 0;
  143.             }
  144.             if (expression[pos]!=')')
  145.             {
  146.                 error=ERR_DISMATCH_RBRACKET;
  147.                 return 0;
  148.             }
  149.  
  150.             ++pos;
  151.         }
  152.  
  153.         else if (expression[pos] == '-')
  154.         {
  155.             ++pos;
  156.             result=evalFactor();
  157.             if(error){
  158.                 return 0;
  159.             }
  160.             else  result*=-1;
  161.         }
  162.         else if (isdigit(expression[pos]))
  163.         {
  164.             result=evalID();
  165.             if(error){
  166.                 return 0;
  167.             }
  168.         }
  169.         return result;
  170.     }
  171.  
  172.  
  173.     double evalID()
  174.     {
  175.         bool dot_flag=false;//一个分数中最多只能有一个小数点
  176.         string buffer;
  177.         while (!end())
  178.         {
  179.             if(expression[pos]!='.'&&!isdigit(expression[pos])){
  180.                 break;
  181.             }
  182.             else if(expression[pos]=='.'){
  183.                 if(dot_flag==false){
  184.                     dot_flag=true;
  185.                 }
  186.                 else{
  187.                     error=ERR_MULTIPLE_DOT;
  188.                     return 0;
  189.                 }
  190.                 buffer+=expression[pos];
  191.             }
  192.            
  193.             buffer+=expression[pos];
  194.             ++pos;
  195.         }
  196.  
  197.         return stof(buffer);
  198.     }
  199. }calc;
  200.  
  201. int main()
  202. {
  203.     string expression;
  204.     while(cin>>expression){
  205.         cout<<calc(expression)<<endl;
  206.     }
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement