简体   繁体   English

在 c++ 中使用堆栈评估中缀表达式

[英]Evaluating an infix expression using stack in c++

This is a c++ code to evaluate an infix expression.My code gives the correct output when I put an infix expression directly in the code but when I take an expression from the user and then pass that expression, the code does not give a correct answer, I am not being able to understand why.I have used my custom stack file.这是用于评估中缀表达式的 c++ 代码。当我将中缀表达式直接放入代码中时,我的代码给出了正确的 output 但是当我从用户那里获取表达式然后传递该表达式时,代码没有给出正确的答案,我无法理解为什么。我使用了我的自定义堆栈文件。 If you could kindly find my mistake and fix my code.如果您能找到我的错误并修复我的代码。 Thank you.谢谢你。


#include <iostream>
#include"stacklink.h"
#include"stacklink.cpp"
#include<bits/stdc++.h>

using namespace std;

int precedence(char op){
    if(op == '+'||op == '-')
    return 1;
    if(op == '*'||op == '/')
    return 2;
    return 0;
}
int applyOp(int a, int b, char op){
    switch(op){
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return a / b;
    }
}
int evaluate(string tokens){
    int i;
    StackType <int> values;
    StackType <char> ops;

    for(i = 0; i < tokens.length(); i++){

        if(tokens[i] == ' ')
            continue;

        else if(tokens[i] == '('){
            ops.Push(tokens[i]);
        }

        else if(isdigit(tokens[i])){
            int val = 0;

            while(i < tokens.length() &&
                        isdigit(tokens[i]))
            {
                val = (val*10) + (tokens[i]-'0');
                i++;
            }

            values.Push(val);
        }

        else if(tokens[i] == ')')
        {
            while(!ops.IsEmpty() && ops.Top() != '(')
            {
                int val2 = values.Top();
                values.Pop();

                int val1 = values.Top();
                values.Pop();

                char op = ops.Top();
                ops.Pop();

                values.Push(applyOp(val1, val2, op));
            }

            if(!ops.IsEmpty())
               ops.Pop();
        }

        else
        {
            while((!ops.IsEmpty()) && precedence(ops.Top())
                                >= precedence(tokens[i]))  {
                int val2 = values.Top();
                values.Pop();

                int val1 = values.Top();
                values.Pop();

                char op = ops.Top();
                ops.Pop();

                values.Push(applyOp(val1, val2, op));
            }

            ops.Push(tokens[i]);
        }
    }

    while(!ops.IsEmpty()){
        int val2 = values.Top();
        values.Pop();

        int val1 = values.Top();
        values.Pop();

        char op = ops.Top();
        ops.Pop();

        values.Push(applyOp(val1, val2, op));
    }

    return values.Top();
}

int main() {
    //cout << evaluate("10 + 2 * 6") << "\n";
    //cout << evaluate("10 + 3 * 5 / ( 16 - 4 )") << "\n";
    //cout << evaluate("100 * ( 2 + 12 )") << "\n";
    //cout << evaluate("100 * ( 2 + 12 ) / 14");

    string infix;

    cin>>infix;
    cout<< evaluate(infix);
    return 0;
}

Instead of using cin>>infix use getline(cin,infix) Because in case of cin It will take input till a ' ' or '\n' or '\t' and your string contains blank spaces so it is reading till black space Lets say your expression is 10 + 2 but it is reading expression as only 10 .而不是使用cin>>infix使用getline(cin,infix)因为在cin的情况下,它将输入直到 ' ' 或 '\n' 或 '\t' 并且您的字符串包含空格,因此它正在读取直到黑色空间假设您的表达式是10 + 2 ,但它仅将表达式读取为10 But if you will use getline then it will read whole line till "\n".但是如果你将使用getline那么它将读取整行直到“\n”。

Hope It may Help....!希望它可以帮助......!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM