如何使用多个数字操作数执行后缀操作 [英] How to execute a postfix operation with multiple digit operands

查看:83
本文介绍了如何使用多个数字操作数执行后缀操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习波兰语符号,我尝试了一个用于后缀评估的程序。



我的程序执行适用于0和9等单位数操作数。但对于多个数字操作数,如10,55,99,它无法正常工作。



例如:我想要的结果是

12 5 * = 60,但它提供了错误的答案。



我在互联网上搜索,找到了很多解决方案,但它们有两个很难理解。

任何人都可以编辑/修改我的这个代码所以我可以使用多位数操作数。



使用的算法如下:



什么我试过了:



I am learning polish notation and i tried a program for postfix evaluation.

My program executed works fine for single digit operands like 0 and 9 . But for multiple digits operands like 10,55,99 its not working correctly.

For eg: The result i want is
12 5 * = 60,but its providing wrong answer.

I searched on internet,found many solutions but they are two complex to understand.
Can anyone edit/modify this code of mine so i can use multiple digit operands.

The Algorithm used is given below:

What I have tried:

#include<stdio.h>
#include<iostream>
using namespace std;
int s[50];
int top=-1;
int space(char ch);

int push(int elem)
{
    s[++top]=elem;
    return 0;
}

int pop()
{
    return(s[top--]);
}

int space(char ch)
{
    if( ch == ' ' || ch == '\t' )
        return 1;
    else
        return 0;
}

main()
{
    char pofx[50],ch;
    int i=0;
    int op1,op2;
    printf("\n\nRead the Postfix Expression ? ");
    gets(pofx);
    while( (ch=pofx[i++]) != '\0')

    {
        if(isdigit(ch)) push(ch-'0');
        else
        {
            op2=pop();
            op1=pop();
            if(!space(ch))
            {
                switch(ch)
                {
                /*case ' ':
                    break;
                case '\t':
                    break;*/
                case '+':
                    push(op1+op2);
                    break;
                case '-':
                    push(op1-op2);
                    break;
                case '*':
                    push(op1*op2);
                    break;
                case '/':
                    push(op1/op2);
                    break;
                case '^':
                    push(op1^op2);
                    break;
                }
            }

        }

    }

    printf("\n Given Postfix Expn: %s\n",pofx);
    printf("\n Result after Evaluation: %d\n",s[top]);
}

推荐答案

您正在推送并弹出单个数字而不是全数字。

Just通过你的代码记住(或打印中间值)。



输入:12 5 *

1:s [0] = 1

2:s [1] = 2

空间:op2 = s [1] = 2,op1 = s [0] = 1

5:s [0] = 5

空格:op2 = s [0] = 5,op1 = s [-1] =未定义

*:



一个简单的解决方案是在空格和操作处分割输入。

要获得数值,我建议使用strtol - C ++参考 [ ^ ](或 strtod - C ++参考 [ ^ ]浮点数值)使用 endptr 参数来检查哪个字符停止转换。



示例:

You are pushing and popping single digits instead of full numbers.
Just step in mind through your code (or let intermediate values be printed out).

Input: "12 5 *"
1: s[0] = 1
2: s[1] = 2
SPACE: op2 = s[1] = 2, op1 = s[0] = 1
5: s[0] = 5
SPACE: op2 = s[0] = 5, op1 = s[-1] = undefined
*:

A simple solution would split the input at spaces and operations.
To get numeric values I suggest to use strtol - C++ Reference[^] (or strtod - C++ Reference[^] for floating point values) using the endptr parameter to check which character stops the conversion.

Example:
char *endptr;
char *parse = pofx;
while (1)
{
    long val = strtol(parse, &endptr, 10);
    // Break when not a number followed by a space
    if (endptr == parse || ' ' != *endptr)
        break;
    push(val);
    // Resume behind space
    parse = endptr + 1;
}
// At this point *endptr should contain the operation character


要查看代码在做什么,调试器是首选工具。

你的问题在这里:

To see what your code is doing, the debugger is the tool of choice.
Your problem is here:
if(isdigit(ch)) push(ch-'0');



每个数字都是单独处理的。

你的我需要处理一个前面有另一个数字的数字不是新数字,而是前一个数字的一​​部分。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个bug。



调试器将帮助你提高对代码的理解,但不是第一人称体验,ni身体可以为你做。


each digit is handled individually.
Your need to handle that a digit preceded by another digit is not a new number, but a part of previous number.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

The debugger will help you to improve your understanding of your code but ot is a first person experience, ni body can do it for you.


这篇关于如何使用多个数字操作数执行后缀操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆