从中缀转换为后缀并评估后缀表示法 [英] Converting from Infix to Postfix and evaluating Postfix notation

查看:22
本文介绍了从中缀转换为后缀并评估后缀表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

#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("

Read the Infix Expression   ");
 scanf("%s", infx);
 push('#');
 while ((ch = infx[i++]) != '') {
  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] = ''; /* Make pofx as valid string */
 printf("

Given Infix Expn: %s  Postfix Expn: %s
", infx, pofx);

 while( (ch=pofx[i++]) != '')
 {
  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("
 Given Postfix Expn: %s
",pofx);
 printf("
 Result after Evaluation: %d
",s[top]);
}

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

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

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

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

推荐答案

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

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

pushit (int ele) {      /* Function for PUSH operation */
    s[++top] = ele;
}

s 中混合 int/char 后,您尝试读取:

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

op2=popit();
op1=popit();

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

which attempts to create an int from popit(). popit() is simply a 1 byte char. So op1 and op2 are not getting the values you want:

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

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

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 -Wall option. It reveals:

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’

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

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.

这篇关于从中缀转换为后缀并评估后缀表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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