OJ2--B. Infix to Postfix Conversion

  1. Infix to Postfix Conversion
    时间限制1000 ms 内存限制65536 KB
    题目描述
    Convert the infix expression to postfix expression
    输入格式
    1 line
    A correct expression include +.一.*./ ,(,) and interger. Each integer is less than 10 and more than 0.
    输出格式
    2 line.
    The first line gives the result of expression. Keep 2 digits after the decimal point.
    The second line gives the postfix expression. Use a blank space to divide every number and symbol.Thers is a blank space at the end ofthe line.

输入样例
2*(3+4)/5
输出样例
234+*5/

在这里插入图片描述
在这里插入图片描述
注意:此题在读入时要用EOF

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char stack[100];
    double stack2[100];
    char array[20];
    for (int i = 0; i < 20; i++)
    {
        array[i] = '\0';
    }
    int top = -1;
    int top2 = -1;
    int p = -1;
    char x;
    while ((x = getchar()) != '\n' && x != EOF)
    {
        if (x >= '0' && x <= '9')
        {
            p++;
            array[p] = x;
        }
        else
        {
            if (x == '(')
            {
                top++;
                stack[top] = x;
            }
            if (x == ')')
            {
                while (stack[top] != '(')
                {
                    p++;
                    array[p] = stack[top];
                    top--;
                }
                top--;
            }

            if (x == '*' || x == '/')
            {
                while (stack[top] == '*' || stack[top] == '/')
                {
                    p++;
                    array[p] = stack[top];
                    top--;
                }
                top++;
                stack[top] = x;
            }
            if (x == '+' || x == '-')
            {
                while (top != -1 && stack[top] != '(')
                {
                    p++;
                    array[p] = stack[top];
                    top--;
                }

                top++;
                stack[top] = x;
            }
        }
    }

    while (top != -1)
    {
        p++;
        array[p] = stack[top];
        top--;
    }

    //计算表达式的结果:
    double x1, x2, result;
    for (int i = 0; array[i] != '\0'; i++)
    {
        if (array[i] >= '0' && array[i] <= '9')
        {
            top2++;
            stack2[top2] = (array[i] - '0');
        }
        else if (array[i] == '+')
        {
            x1 = stack2[top2--];
            x2 = stack2[top2--];
            result = x1 + x2;
            stack2[++top2] = result;
        }
        else if (array[i] == '-')
        {

            x2 = stack2[top2--];
            result = x2 - x1;
            stack2[++top2] = result;
        }
        else if (array[i] == '*')
        {
            x1 = stack2[top2--];
            x2 = stack2[top2--];
            result = x1 * x2;
            stack2[++top2] = result;
        }
        else if (array[i] == '/')
        {
            x1 = stack2[top2--];
            x2 = stack2[top2--];
            result = x2 / x1;
            stack2[++top2] = result;
        }
    }
    printf("%.2f", stack2[0]);
    printf("\n");
    for (int i = 0; array[i] != '\0'; i++)
    {
        printf("%c ", array[i]);
    }
    // printf("\n");

    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值