使用堆栈的计算器程序

标签 c stack infix-notation postfix-operator

我正在用 c 使用堆栈编写一个计算器程序,在下面的程序中,我使用了中缀到后缀转换和下一个后缀评估的概念。 我得到 1+2 的正确答案是 3,但对于 11+1 或任何两位数或更多数字,我得到错误的答案。

Can anyone help me what I will include in my code so that it work for more than two digit like 28+25 or any?

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

int top = -1;
char pofx[50];
char s[SIZE];
int infix_to_postfix() {

  char infx[50], ch;
  int i = 0, k = 0;

  void 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;
    }
    return -1;
  }
  printf("\n\nEnter a Value to calculate : ");
  gets(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();
      char 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);

  return (int) pofx[k];
}

void postfix_evaluate() {

  char ch;
  int i = 0, op1, op2;
  void pushit(int elem) { /* Function for PUSH operation */
    s[++top] = elem;
  }

  int popit() { /* Function for POP operation */
    return (s[top--]);
  }
  infix_to_postfix();
  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]);
}

int main() {
  postfix_evaluate();
  return 0;
}

最佳答案

可能有用的我自己的部分代码:

if (isdigit(gi.n.nch))
{
gi.x = chr2num(gi.n.nch);
gi.n= nextchar( gi.n, len, instr);
while(isdigit(gi.n.nch))
{
    gi.x *= 10;
    gi.x += chr2num(gi.n.nch);
    gi.n= nextchar( gi.n, len, instr);
}
}

关于使用堆栈的计算器程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314064/

相关文章:

python - 创建一个堆栈和使用 for 循环遍历堆栈的迭代器

c++ - 用动态数组实现的堆栈

java - StringIndexOutOfBoundsException while 循环与charat()

.net - F# 多种类型的通用中缀运算符(fmap、applicative、bind 等)

c++ - OpenCL get_global_id

c - 什么触发了 0x08 中断?

c++ - 如何设置c控制台窗口标题

function - 如何在 OCaml 中定义中缀(非符号,也不是运算符)函数?

function - Haskell 中缀函数应用优先级

c - 解析一个巨大的文本文件给我一个段错误