C语言 从 Infix 转换为 Postfix 并评估 Postfix 表示法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24579424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:12:07  来源:igfitidea点击:

Converting from Infix to Postfix and evaluating Postfix notation

cstackpostfix-notationinfix-notation

提问by user3096716

I'm writing a program that reads an Infix notation, converts it to Postfix and then evaluate that Postfix. Here's my program:

我正在编写一个读取中缀符号的程序,将其转换为 Postfix,然后评估该 Postfix。这是我的程序:

#include<stdio.h> 
#include <ctype.h>
#define SIZE 50            /* Size of Stack */

char s[SIZE];
int top = -1; /* Global declarations */

push(char elem) { /* Function for PUSH operation */
 s[++top] = elem;
}

char pop() { /* Function for POP operation */
 return (s[top--]);
}

int pr(char elem) { /* Function for precedence */
 switch (elem) {
 case '#':
  return 0;
 case '(':
   return 1;
 case '+':
 case '-':
  return 2;
 case '*':
 case '/':
  return 3;
 }
}
pushit(int ele){                       /* Function for PUSH operation */
 s[++top]=ele;
}

int popit(){                      /* Function for POP operation */
 return(s[top--]);
}

 main() { /* Main Program */
  char infx[50], pofx[50], ch, elem;
 int i = 0, k = 0, op1, op2,ele;
 printf("\n\nRead the Infix Expression   ");
 scanf("%s", infx);
 push('#');
 while ((ch = infx[i++]) != '
pushit (int ele) {      /* Function for PUSH operation */
    s[++top] = ele;
}
') { if (ch == '(') push(ch); else if (isalnum(ch)) pofx[k++] = ch; else if (ch == ')') { while (s[top] != '(') pofx[k++] = pop(); elem = pop(); /* Remove ( */ } else { /* Operator */ while (pr(s[top]) >= pr(ch)) pofx[k++] = pop(); push(ch); } } while (s[top] != '#') /* Pop from stack till empty */ pofx[k++] = pop(); pofx[k] = '
op2=popit();
op1=popit();
'; /* Make pofx as valid string */ printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx); while( (ch=pofx[i++]) != '
int popit(){                      /* Function for POP operation */
return(s[top--]);
}
') { if(isdigit(ch)) pushit(ch-'0'); /* Push the operand */ else { /* Operator,pop two operands */ op2=popit(); op1=popit(); switch(ch) { case '+':pushit(op1+op2);break; case '-':pushit(op1-op2);break; case '*':pushit(op1*op2);break; case '/':pushit(op1/op2);break; } } } printf("\n Given Postfix Expn: %s\n",pofx); printf("\n Result after Evaluation: %d\n",s[top]); }

The program converts my Infix to a Postfix notation correctly. However, for the evaluation part, it always returns 0 as a result.

该程序正确地将我的中缀转换为后缀表示法。但是,对于求值部分,它总是返回 0 作为结果。

Also, when converting from Infix to Postfix , I would like to print the result in each step, how can I do that?

另外,当从 Infix 转换为 Postfix 时,我想在每个步骤中打印结果,我该怎么做?

回答by David C. Rankin

One problem is your are storing values in sas a char with storage of 1 byte per element and then attempt to push integers into swith:

一个问题是您将值存储s为一个字符,每个元素存储 1 个字节,然后尝试将整数推入s

popit.c:8:1: warning: return type defaults to ‘int'
popit.c:32:1: warning: return type defaults to ‘int'
popit.c:41:1: warning: return type defaults to ‘int'

After mixing int/char in s, you attempt to read:

在将 int/char 混合后s,您尝试阅读:

char push(char elem)

which attempts to create an intfrom popit(). popit()is simply a 1 byte char. So op1and op2are not getting the values you want:

它试图创建一个intfrom popit()popit()只是一个 1 字节char。所以op1op2没有得到你想要的值:

int pushit(int ele)

You need to look at how your are storing integers if you expect to get integers back. Lastly, look at your warnings. At a minimum, build with the -Walloption. It reveals:

如果您希望取回整数,则需要查看如何存储整数。最后,看看你的警告。至少,使用该-Wall选项进行构建。它揭示了:

##代码##

Which may be what you intended. However, your code should build without warning to help insure it is doing what you think it is doing.

这可能是你想要的。但是,您的代码应该在没有警告的情况下构建,以帮助确保它正在做您认为正在做的事情。

回答by Swaraj

In line no. 9 Type:

行号 9 类型:

##代码##

& Line no. 32 Type:

& 行号 32 类型:

##代码##