问题描述
我正在学习波兰语符号,我尝试了一个用于后缀评估的程序。
我的程序执行适用于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]);
}
推荐答案
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
if(isdigit(ch)) push(ch-'0');
每个数字都是单独处理的。
你的我需要处理一个前面有另一个数字的数字不是新数字,而是前一个数字的一部分。
你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。
[]
[]
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个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.
这篇关于如何使用多个数字操作数执行后缀操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!