C-堆栈中的简单表达式计算器'应用

发布于 2025-01-29 12:07:10 字数 2003 浏览 2 评论 0原文

编写一个求解算术表达式,以字符串并打印其值的程序。使用两个堆栈,一个用于操作数,另一个用于操作员。仅考虑 +和 *操作员。还考虑1位操作数。考虑操作员在解决表达式方面的优先级。

我的代码无法正常工作,但是即使我一直在寻找几天,我也看不到问题。非常感谢您的帮助!

# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# define MAX 40

int *p1, *p2; // pointers rows operands, operators
int *to1, *to2; // tops rows operands, operators
int *bo1, *bo2; // bases rows operands, operators

void push1(int i), push2(char j); //operating, operator - stack
int pop1(void), pop2(void); //operand, operator - unstack

void push1 (int i)
{
    if (p1>bo1)
    {
        printf("Stack of operands full \n");
    }
    *p1=i;
    p1++;
}
void push2 (char j)
{
    if (p2>bo2)
    {
        printf("Operator stack full \n");
    }
    *p2=j;
    p2++;
}

int pop1()
{
    p1--;
    if (p1<to1)
    {
        printf("Empty operand stack \n");
        return 0;
    }
    return *p1;
}

int pop2()
{
    p2--;
    if (p2<to2)
    {
        printf("Empty operator stack \n");
        return 0;
    }
    return *p2;
}

void main()
{
    int a,b, c, result, i, st;
    char o, s[MAX];
    p1= malloc(MAX*sizeof(int)); //allocate memory stack 1
    p2= malloc(MAX*sizeof(char)); //allocate memory stack 2

    to1=p1;
    to2=p2;
    bo1=p1+MAX-1;
    bo2=p2+MAX-1;

    printf("Enter the expression\n");
    gets(s);
    st=strlen(s);

    push1(s[0]); //put the first digit
    for (i=0; i<(st-1)/2+1; i++)
    {push1(s[2*(i+1)]); //stack the next digit
     push2(s[2*i+1]); //stack the next operator
         if (s[2*i+1]=='*') //if it's multiplication...
         {
             a=pop1(); // get the last two digits
             b=pop1();
             o=pop2(); //delete the operator
             push1(a*b); // replace with product
         }
    }
        
    while(p1>to1)
         {
             a=pop1();
             b=pop1();
             c=a+b;
             push1(c);
         }

    result=pop1();
    printf("Result=%d", result);
}

Write a program that solves arithmetic expressions, passed as a string, printing its value. Use two stacks, one for operands and one for operators. Consider only + and * operators. Also consider 1-digit operands. Consider operator precedence in solving the expression.

My code is not working, but I cannot see the problem, even though I am looking for it for days. I really appreciate some help!

# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# define MAX 40

int *p1, *p2; // pointers rows operands, operators
int *to1, *to2; // tops rows operands, operators
int *bo1, *bo2; // bases rows operands, operators

void push1(int i), push2(char j); //operating, operator - stack
int pop1(void), pop2(void); //operand, operator - unstack

void push1 (int i)
{
    if (p1>bo1)
    {
        printf("Stack of operands full \n");
    }
    *p1=i;
    p1++;
}
void push2 (char j)
{
    if (p2>bo2)
    {
        printf("Operator stack full \n");
    }
    *p2=j;
    p2++;
}

int pop1()
{
    p1--;
    if (p1<to1)
    {
        printf("Empty operand stack \n");
        return 0;
    }
    return *p1;
}

int pop2()
{
    p2--;
    if (p2<to2)
    {
        printf("Empty operator stack \n");
        return 0;
    }
    return *p2;
}

void main()
{
    int a,b, c, result, i, st;
    char o, s[MAX];
    p1= malloc(MAX*sizeof(int)); //allocate memory stack 1
    p2= malloc(MAX*sizeof(char)); //allocate memory stack 2

    to1=p1;
    to2=p2;
    bo1=p1+MAX-1;
    bo2=p2+MAX-1;

    printf("Enter the expression\n");
    gets(s);
    st=strlen(s);

    push1(s[0]); //put the first digit
    for (i=0; i<(st-1)/2+1; i++)
    {push1(s[2*(i+1)]); //stack the next digit
     push2(s[2*i+1]); //stack the next operator
         if (s[2*i+1]=='*') //if it's multiplication...
         {
             a=pop1(); // get the last two digits
             b=pop1();
             o=pop2(); //delete the operator
             push1(a*b); // replace with product
         }
    }
        
    while(p1>to1)
         {
             a=pop1();
             b=pop1();
             c=a+b;
             push1(c);
         }

    result=pop1();
    printf("Result=%d", result);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2025-02-05 12:07:10

走下输入字符串应该很简单,

  for(int i = 0; i < st; i++){
      char next = s[i];
       ....
  }

为什么所有复杂的2*,-1, /2+1 ....?

walking down the input string should be simple

  for(int i = 0; i < st; i++){
      char next = s[i];
       ....
  }

why all that complicated 2* , -1, /2+1....?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文