gpt4 book ai didi

c - 中缀到后缀计算器没有给出所需的结果

转载 作者:行者123 更新时间:2023-11-30 21:03:05 25 4
gpt4 key购买 nike

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#define BLANK ' '
#define TAB '\t'
#define MAX 50
#define is_number(chr) ((chr>='0')&&(chr<='9'))
void push(long int symbol);
long int pop();
void infix_to_postfix();
int priority(char symbol);
int isEmpty();
int white_space(char);
long int eval_post();
char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;
char operator_set[]="+-*/^()%";
int i=0;
int main()
{
top=-1;
printf("Enter infix Exp. : ");
gets(infix);
//checking for invalid infix arithmatic expression
while(infix[i]!='\0')
{
if(!white_space(infix[i]) && !strchr(operator_set,infix[i]) && !is_number(infix[i]))
{
printf("\nInvalid arithmatic expression");
return -1;
}
i++;
}

infix_to_postfix();
printf("\n==>Postfix Exp.: %s\n",postfix);
printf("\nThe postfix Exp. Evaluated to %d",eval_post());
getch();
return 0;
}

void infix_to_postfix()
{
unsigned 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 priority(char symbol)
{
switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default :
return 0;
}
}

void push(long int symbol)
{
if(top>MAX)
{
printf("Stack overflow\n");
exit(1);
}
stack[++top]=symbol;
}

long int pop()
{
if( isEmpty() )
{
printf("Stack underflow\n");
exit(1);
}
return (stack[top--]);
}
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}

int white_space(char symbol)
{
if( symbol == BLANK || symbol == TAB )
return 1;
else
return 0;
}

long int eval_post()
{
long int a,b,temp,result;
unsigned int i;

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;
}

这是我输入简单表达式(如1+2+3)时的代码它给出了正确的输出,但是当我输入 234+234 时它没有给出想要的结果我不知道我错在哪里?

任何专家都可以帮助我,我花了几个小时但无法弄清楚吗?

最佳答案

代码失败,因为您只考虑代码中一位数的情况。例如,12+34 被翻译为 1234+,后缀解释器将其解析为 1 2 3 4+,从而生成结果7 是 3 + 4。您可能需要更改代码,以便在后缀代码中的连续数字之间添加空格,并更新后缀解释器以识别多位数字。

也就是说,您的代码中还存在其他三个问题。打开警告进行编译,编译器会告诉您以下内容:

  • 您使用的 gets 函数无法以安全方式使用。每个使用 gets 的程序都是有缺陷的。

  • 您使用了不可移植的 conio.h header ,导致您的代码只能在 DOS 上运行,并且在某种程度上可以在 Microsoft Windows 上运行。考虑不使用 conio.h

  • 在其中一个 printf 调用中,您为 long int 类型的参数指定格式 %d。这是未定义的行为,请使用 %ld 来实现此目的。

关于c - 中缀到后缀计算器没有给出所需的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com