| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1279 人关注过本帖
标题:一个+-*/()的计算程序,没有括号计算正常,但带括号就不能给出结果
只看楼主 加入收藏
EMT114514
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-3-12
收藏
 问题点数:0 回复次数:0 
一个+-*/()的计算程序,没有括号计算正常,但带括号就不能给出结果
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

class Token
{
    private:
    double value;
    char kind;

    public:
    void set(double v)  // set num token
    {
        (*this).value = v;
        this->kind = 'n';
    }
    void set(char c) // set char token
    {
        switch (c)
        {
            case '(': case ')': case '^': case '*': case '/':
            case '+': case '-': case 'q': case ';':
            {
                kind = c;
                value = 0;
                break;
            }
            default:
                cout << "Operator \'" << c << "\' is invalid.\n";

        }
    }
    double getvalue() const   // for functions that do not modify this
    {
        return value;
    }
    char getkind() const
    {
        return kind;
    }
    friend ostream& operator<<(ostream& out, const Token& t)
    {
        if (t.kind == 'n')
            out << t.value;
        else
            out << t.kind;
        return out;
    }

};


Token gettoken()  // this function reads one Token at a time
{
    Token t;
    double v;
    char c;

    cin >> c;
    switch(c)
    {
        case '(': case ')': case '^': case '*': case '/':
        case '+': case '-': case 'q': case ';':
        {
            t.set(c);
            return t;
        }
        case '.': case '0': case '1': case '2': case '3':
        case '4': case '5': case '6': case '7': case '8':
        case '9':
        {
            cin.putback(c);  // put it back into out stream
            cin >> v;
            t.set(v);
            return t;
        }
        default:
            cout << "Invalid character: \'" << c << "\'.\n";
            return t;
    }
}

double evaluate(vector<Token>& expr, size_t beg, size_t end)
{
    // regularization of -
    for (size_t i = beg; i < end; i++)
    {
        if (expr[i].getkind() == '-')
        {
            if ( i == beg )
            {
                expr[i].set(-expr[i+1].getvalue());
                expr.erase(expr.begin()+i+1);
            }
            else if ( expr[i-1].getkind() != 'n')
            {
                expr[i].set(-expr[i+1].getvalue());
                expr.erase(expr.begin()+i+1);
            }
        }
    }

    for (size_t i = end-1 ; i > beg; i--)
    {
        if (expr[i].getkind() == '^')
        {
            expr[i-1].set( pow(expr[i-1].getvalue(),expr[i+1].getvalue())  );
            expr.erase(expr.begin()+i); // removes ^
            expr.erase(expr.begin()+i); // removes right term
            end-=2;
        }
    }
    // handle mul and div
    for (size_t i = beg; i < end; i++)
    {
        if (expr[i].getkind() == '*')
        {
            expr[i-1].set( expr[i-1].getvalue() * expr[i+1].getvalue()  );
            expr.erase(expr.begin()+i); // removes *
            expr.erase(expr.begin()+i); // removes right term
            i--;
            end-=2;
        }
        else if (expr[i].getkind() == '/')
        {
            expr[i-1].set( expr[i-1].getvalue() / expr[i+1].getvalue()  );
            expr.erase(expr.begin()+i); // removes /
            expr.erase(expr.begin()+i); // removes right term
            i--;
            end-=2;
        }
    }

    // handle add and sub
    for (size_t i = beg; i < end; i++)
    {
        if (expr[i].getkind() == '+')
        {
            expr[i-1].set( expr[i-1].getvalue() + expr[i+1].getvalue()  );
            expr.erase(expr.begin()+i); // removes +
            expr.erase(expr.begin()+i); // removes right term
            i--;
            end-=2;
        }
        else if (expr[i].getkind() == '-')
        {
            expr[i-1].set( expr[i-1].getvalue() - expr[i+1].getvalue()  );
            expr.erase(expr.begin()+i); // removes -
            expr.erase(expr.begin()+i); // removes right term
            i--;
            end-=2;

        }
    }

    return expr[beg].getvalue();

}
double evaluate( vector<Token>& expr )
{
  while(expr.size() > 1)
  {
    for (size_t i = expr.size()-1; i > 1; i--)
    {
        if (expr[i].getkind() == ')')
        {
          size_t rparenpos = i;
          for (size_t i = 0; i < rparenpos; i++)
          {
              if (expr[i].getkind() == '(')
              {
                size_t lparenpos = i;
                evaluate(expr, lparenpos+1, rparenpos);
                expr.erase(expr.begin()+lparenpos);
                expr.erase(expr.begin()+lparenpos+1);
                i--;
              }
              else
              {
                  cout << "Invalid expression: parentheses not matched";
                  return 0;
              }
          }
        }
          else
          {
            evaluate(expr, 0, expr.size());
          }
      }
  }
  return expr[0].getvalue();
}
int main()
{
    vector<Token> expr;
    Token t;
    double exprval;

    cout << "Enter an algebraic expression such as (1+12.5)*4^2\n"
        << "For now we can handle only: +,-,*,/.\n"
        << "Finish expression with character \';\'"
        << endl;
    while (true)
    {
        t = gettoken();
        if (t.getkind()=='q')
            break;

        if ( t.getkind() == ';')
        {
            for(size_t i = 0; i < expr.size(); i++)
                cout << expr[i];
            exprval = evaluate(expr);
            cout << " = " << exprval << endl;
            expr.clear();
        }
        else
        {
            expr.push_back(t);
        }

    }

    cout << endl;
    return 0;
}
搜索更多相关主题的帖子: begin case set erase Token 
2020-03-12 01:46
快速回复:一个+-*/()的计算程序,没有括号计算正常,但带括号就不能给出结果
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037045 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved