【问题标题】:Converting from Infix to Postfix and evaluating Postfix notation从中缀转换为后缀并评估后缀表示法
【发布时间】:2014-08-26 02:17:50
【问题描述】:

我正在编写一个程序,它读取中缀符号,将其转换为 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++]) != '\0') {
  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] = '\0'; /* Make pofx as valid string */
 printf("\n\nGiven Infix Expn: %s  Postfix Expn: %s\n", infx, pofx);

 while( (ch=pofx[i++]) != '\0')
 {
  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]);
}

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

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

【问题讨论】:

  • 小注:有趣的名字:pushit。它确实 p.u.
  • 注意:如果您的输入包含空格,那么scanf("%s", infx); 将不会扫描整行。
  • 代码太多,调试太少。
  • @chux haha​​hahaha ...当你指出它时我才注意到它。不用担心我更改了函数的名称!

标签: c stack postfix-notation infix-notation


【解决方案1】:

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

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

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

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

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

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

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

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’

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

【讨论】:

  • 是的,你是对的!非常感谢你的帮助!我终于让它工作了!
【解决方案2】:

行号9 类型:

char push(char elem)

& 行号32 类型:

int pushit(int ele)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多