首页 > 解决方案 > 逆波兰符号计算器

问题描述

我目前正在研究 RPN 计算器,它需要一个中缀表达式将其转换为后缀并显示答案。我基本上是对的,但是当我从堆栈中弹出答案时,如果只显示结果的最后一位数字

Enter infix: (1+1)*13+10/2
Postfix: 11+13*102/+
Result is: 1
Enter infix: 2*13+10/2
Postfix: 213*102/+
Result is:1

它适合这种输入 输入中缀:3*2+5 后缀:32*5+ 结果是:11

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

using namespace std;

class infix2postfix
{
public:
    void push(int symbol);
    int pop();
    void infix_to_postfix();
    int priority(char symbol);
    int isEmpty();
    int white_space(char);
    int eval_post();
};

char infix[100], postfix[100];
int stack[100];
int top;

int main()
{
    infix2postfix ip;
    top=-1;
    cout<<"Enter infix : ";
    gets(infix);
    ip.infix_to_postfix();
    cout<<"Postfix : "<<postfix<<endl;
    cout<<"Result is : "<<ip.eval_post()<<endl;
    return 1;
}

void infix2postfix :: infix_to_postfix()
{
    int i,p=0;
    char next;
    char symbol;
    for(i=0; i<strlen(infix); i++)
    {
        symbol=infix[i];
        if(!white_space(symbol))
        {
            switch(symbol)
            {
            case '(':
                push(symbol);
                break;
            case ')':
                while((next=pop())!='(')
                    postfix[p++] = next;
                break;
            case '+':
            case '-':
            case '*':
            case '/':
            case '%':
            case '^':
                while( !isEmpty( ) &&  priority(stack[top])>= priority(symbol) )
                    postfix[p++]=pop();
                push(symbol);
                break;
            default: /*if an operand comes*/
                postfix[p++]=symbol;
            }
        }
    }
    while(!isEmpty( ))
        postfix[p++]=pop();
    postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}

/*This function returns the priority of the operator*/
int infix2postfix :: priority(char symbol)
{
    switch(symbol)
    {
    case '(':
        return 0;
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
    case '%':
        return 2;
    case '^':
        return 3;
    default :
        return 0;
    }
}

void infix2postfix :: push(int symbol)
{
    if(top>100)
    {
        cout<<"Stack overflow\n";
        exit(1);
    }
    stack[++top]=symbol;
}

int infix2postfix :: pop()
{
    if( isEmpty() )
    {
        cout<<"Stack underflow\n";
        exit(1);
    }
    return (stack[top--]);
}


int infix2postfix :: isEmpty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

int infix2postfix :: white_space(char symbol)
{
    if( symbol == ' ' || symbol == '\t' )
        return 1;
    else
        return 0;
}

int infix2postfix :: eval_post()
{
    int a,b,i,temp,result;
    for(i=0; i<strlen(postfix); i++)
    {
        if(postfix[i]<='9' && postfix[i]>='0')
            push(postfix[i]-'0');
        else
        {
            a=pop();
            b=pop();
            switch(postfix[i])
            {
            case '+':
                temp=b+a;
                break;
            case '-':
                temp=b-a;
                break;
            case '*':
                temp=b*a;
                break;
            case '/':
                temp=b/a;
                break;
            case '%':
                temp=b%a;
                break;
            case '^':
                temp=pow(b,a);
            }
            push(temp);
        }
    }
    result=pop();
    return result;
}

标签: c++postfix-notation

解决方案


考虑当eval_post()被给予使用时会发生什么213*102/+。让我们从中间开始,星号后面的“1”。'1' 是一个数字,所以推它 [堆栈以:1 结尾]。类似地,0 和 2 被推入 [堆栈以:1、0、2 结尾]。然后遇到除法符号,所以弹出 2 和 0,然后 push 0/2 = 0 [堆栈以:1, 0 结尾]。最后遇到加法符号,所以弹出 0 和 1,然后 push 1+0=1,然后弹出作为你的答案。

您的问题的一个症状是,如果一切正常,eval_post()返回时堆栈应该是空的。但是,当您的中缀包含多于一位的数字时,它不为空。请注意,“10”作为两个数字被压入堆栈:“1”后跟“0”。您希望推送值 10。

代码也存在一些样式问题,但这似乎是主要的功能问题。


推荐阅读