C ++后缀到Postfix转换和计算器

时间:2018-12-12 18:30:24

标签: c++ calculator postfix-notation infix-notation

我是stackoverflow的新手,并在大学里完成了我的数据结构课程的最终项目。对于我的项目,我正在创建一个计算器,该计算器从输入中获取数学表达式,例如(11-2)/ 3 *(15 + 2/1)-6,并做两件事:

1)将表达式从中缀表示法转换为后缀表示法 2)使用后缀符号来评估表达式的值

我已经非常接近最终产品了,但是我的问题是程序只能采用数字0-9,因为我目前拥有代码的方式是转换函数将输入评估为字符串,因此大于9的数字(即双/三位数)将使评估功能失效。我如何才能将这些数字检测为整数(或浮点数/双精度数)并将其以评估函数可以处理的格式(带有运算符)放置?

此外,我需要创建异常类(括号不正确,被零除,字母/非操作数/非运算符等)。我什至还没有学到什么异常类,所以我不确定如何编写这些类。我知道它与throw / catch函数有关,但是任何输入都值得赞赏(或向我可以找到简单解释的方向)。我在下面粘贴了我的代码:

#include <iostream>
#include <ctype.h>
#include <stack>
#include <ItemType.h>
using namespace std;

string getPostfix( string str );

double evaluate( string str );

bool hasPrecedence( char a, char b );

int main()
{
    cout << "Welcome to the calculator!" << endl;
    string infixString; 
    string postFixString;
    bool valid = false;
    cout << "Enter the expression you would like to evaluate: ";
    getline( cin, infixString );    // get expression as string
    postFixString = getPostfix( infixString ); // convert to postfix
    cout << postFixString << endl;
    cout << "The value of the given expression is: " << evaluate( postFixString ) << endl; // output evaluated expression

    return 0;
}

string getPostfix( string str ) // function to convert infix to postfix
{
    string postFixStr;  // converted string
    stack<char> stack;  // stack for operators
    for ( int i = 0; i < str.length(); i++ )
    {
        if ( isdigit( str[i] ) )    // if str[i] is a digit (0-9), add to postfix string
            postFixStr += str[i];

        else if ( str[i] == '(' )   // if str[i] is an opening parenthesis, push to stack
            stack.push( str[i] );

        else if ( str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' )    // if an operator...
        {
            if ( stack.empty() || stack.top() == '(' ) // ... push to stack if stack is empty or top of stack is opening parenthesis
            {
                stack.push( str[i] );
            }

            else if ( hasPrecedence( str[i], stack.top() ) ) // ... if the operator has higher precedence, push to stack
            {
                stack.push( str[i] );
            }

            else // ... if neither of the above cases, add operator already in stack to string, pop it, and push the new operator to stack
            {
                postFixStr += stack.top();
                stack.pop();
                stack.push( str[i] );
            }
        }

        else if ( str[i] == ')' ) // ... if str[i] is closing parenthesis, add to string and pop until opening parenthesis is reached
        {
            while ( stack.top() != '(' )
            {
                postFixStr += stack.top();
                stack.pop();
            }
            if ( stack.top() == '(' )
                stack.pop();
        }
    }
    while ( ! stack.empty() ) // after string has been iterated, add the remainder of the stack to the string
    {
        postFixStr += stack.top();
        stack.pop();
    }
    return postFixStr;
} 

double evaluate( string str ) // function to evaluate postfix expression
{
    int op1, op2;
    stack<int> stack;
    for ( int i = 0; i < str.length(); i++ ) // iterate through postfix string
    {
        if ( isdigit( str[i] ) ) // if a digit, push to stack as int value
            stack.push( str[i] - 48 );
        else if ( str[i] == '+' ) // cases for different operators
        {
            op1 = stack.top(); // assign top int to op1
            stack.pop();       // pop top
            op2 = stack.top(); // assign next into to op2
            stack.pop();       // pop top
            stack.push( op1 + op2 ); // add op1 and op2, push result to stack
        }
        else if ( str[i] == '-' )
        {
            op1 = stack.top();
            stack.pop();
            op2 = stack.top();
            stack.pop();
            stack.push( op1 - op2 );
        }
        else if ( str[i] == '*' )
        {
            op1 = stack.top();
            stack.pop();
            op2 = stack.top();
            stack.pop();
            stack.push( op1 * op2 );
        }
        else if ( str[i] == '-' )
        {
            op1 = stack.top();
            stack.pop();
            op2 = stack.top();
            stack.pop();
            stack.push( op1 / op2 );
        }
    }
    return stack.top(); // return final value
}

bool hasPrecedence( char a, char b ) // function to determine operator precedence
{
    int aWeight, bWeight = 2;
    if ( a == '*' || a == '/' )
        aWeight = 2;
    else
        aWeight = 1;
    return aWeight >= bWeight;
}

1 个答案:

答案 0 :(得分:2)

感谢大家的答复。在仔细研究了大家发布的解决方案并收集了他们的想法之后,我找到了针对我的具体案例的非常简单的解决方案。

在我的getPostFix函数中,我在将每个字符插入postFixStr之后添加了空格,然后使用这些空格作为分隔符,可以将其添加到我的评估函数中的第一个“ if”语句中:

if ( isdigit( str[i] ) )
{
    double value = 0;
    while ( i < str.length() && isdigit( str[i] ) ) // if a digit, push to stack as double value
    {
        value = ( value * 10 ) + ( str[i] - '0' );
        i++;    
    }
    stack.push(value);
}

请注意,* 10是将先前的值移到10的位置(或100's +的位置),而-'0'是我之前更改的-49的清晰版本ASCII值到算术值。

我希望这对以后的人有帮助!