堆栈后缀评估问题 [英] STACK postfix evaluation problem
本文介绍了堆栈后缀评估问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下使用Dev C ++编写的代码用于使用堆栈来评估后缀表达式.
该程序对于这样的字符串"12+
"
可以完美运行
但是对于"14 5+
",它给出了错误的结果,它没有将14作为一个操作数(整数).
请帮助我解决这个问题,谢谢
代码:
Below code written in Dev C++ is used to evaluate Post-fix expression using stack.
This program is running perfectly for such string "12+
"
But for "14 5+
" it gives wrong result, it did not take 14 as one operand(integer).
Please help me in solving this problem Thanks
Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 20
struct stack{
int top;
float str[MAX];
}s;//stack
char postfix[MAX];//postfix
void push(float);
float pop();
int isoperand(char);
float operate(float,float,char);
int main()
{
int i=0;
printf("Enter Expression:");
scanf("%s",postfix);
float ans,op1,op2;
while(postfix[i]!='\0')
{
if(isoperand(postfix[i]))
push(postfix[i]-48);
else
{
op1=pop();
op2=pop();
ans=operate(op1,op2,postfix[i]);
push(ans);
printf("%f %c %f = %f\n",op2,postfix[i],op1,ans);
}
i++;
}
printf("%f",s.str[s.top]);
getch();
}
int isoperand(char x)
{
if(x>='0' && x<='9')
return 1;
else
return 0;
}
void push(float x)
{
if(s.top==MAX-1)
printf("Stack is full\nStack overflow\n");
else
{
s.top++;
s.str[s.top]=x;
}
}
float pop()
{
if(s.top==-1)
{
printf("Stack is emplty\nSTACK UNDERFLOW\n");
getch();
}
else
{
s.top--;
return s.str[s.top+1];
}
}
float operate(float op1,float op2,char a)
{
switch(a)
{
case '+':return op2+op1;
case '-':return op2-op1;
case '*':return op2*op1;
case '/':return op2/op1;
case '^':return pow(op2,op1);
}
}
推荐答案
就目前而言,您的计算器将单个数字识别为操作数,而它需要识别一个数字(多个数字).这意味着您必须在输入表达式中允许使用分隔符(否则它将无法区分两个输入数字),必须对其进行处理,最后,您需要将多个数字作为单个标记(一个数字)进行处理. ,操作数).
我已修改您的代码,以符合此类要求.
您可以输入类似以下内容的表达式:
As it stands, your calculator recognizes a single digit as operand while it would need to recognize a number (multiple digits). That means you must allow a separator in your input expression (otherwise it wouldn''t be able to distinguish between two input numbers), you have to handle it and, finally, you need to handle multiple digits as a single token (a number, the operand).
I have modified you code in order to work with such requirements.
You may input expressions like:
43 18 + 4 *
43 18 + 4 *
#include <stdio.h>
#include <math.h>
#define MAX 20
struct stack
{
int top;
float str[MAX];
} s;//stack
char postfix[MAX];//postfix
int operand;
void push(float);
float pop();
int isoperand(int n);
int get_operand(int n);
int skip_blanks(int n);
float operate(float,float,char);
int main()
{
int i=0;
printf("Enter Expression:");
fgets(postfix, sizeof(postfix), stdin); // get a whole input line
float ans,op1,op2;
while(1)
{
i = skip_blanks(i);
if ( postfix[i] == '\n')
break;
else if(isoperand(i))
{
i = get_operand(i);
push(operand);
}
else
{
op1=pop();
op2=pop();
ans=operate(op1,op2, postfix[i]);
push(ans);
printf("%f %c %f = %f\n",op2,postfix[i],op1,ans);
i++;
}
}
printf("%f",s.str[s.top]);
getchar();
return 0;
}
int skip_blanks(int n)
{
while (postfix[n] == ' ') n++;
return n;
}
int get_operand(int n)
{
operand = 0;
while (postfix[n] >= '0' && postfix[n] <= '9')
{
operand *= 10;
operand += postfix[n]-'0';
n++;
}
return n;
}
int isoperand(int i)
{
char x = postfix[i];
if(x>='0' && x<='9')
return 1;
else
return 0;
}
void push(float x)
{
if(s.top==MAX-1)
printf("Stack is full\nStack overflow\n");
else
{
s.top++;
s.str[s.top]=x;
}
}
float pop()
{
if(s.top==-1)
{
printf("Stack is emplty\nSTACK UNDERFLOW\n");
getchar();
return nan("?");
}
else
{
s.top--;
return s.str[s.top+1];
}
}
float operate(float op1,float op2,char a)
{
switch(a)
{
case '+':return op2+op1;
case '-':return op2-op1;
case '*':return op2*op1;
case '/':return op2/op1;
case '^':return pow(op2,op1);
}
return nan("?");
}
这篇关于堆栈后缀评估问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文