中缀表达式转换成逆波兰式C语言,数据结构,栈的应用

题目:将任意一个合法的中缀表达式转换成逆波兰式

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void PushOperation(char *opera, char *string, int *op, int *s);
void PexpretoSexpre(char *string);
void PexpretoSexpre(char *string)
{
    char num[100] = "0";    /* 存储后缀表达式 */
    char opera[100] = "0";    /* 存储运算符 */
    /*
    num----j
    opera----op
    string----i
    */
    int i, j, op;

    op = i = j = 0;

    while (string[i] != '\0')
    {
        if (isdigit(string[i]))    /* 如果是数字 */
        {
            num[j] = string[i];    /* 数字直接入后缀表达式栈 */
            j++;
            i++;
        }
        else
        {
            switch (string[i])    /* 如果是操作数 */
            {
            case '+':
                {
                    if (op == 0)    /* 如果是空栈 */
                    {
                        PushOperation(opera, string, &op, &i);    /* 入运算符栈 */
                        break;
                    }
                    if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
                    {
                        switch (opera[op-1])
                        {
                        case '+':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '-':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '*':
                            {    /* 加法优先级低于乘法 */
                                num[j] = opera[op-1];    /* 将操作数出栈 */
                                opera[op-1] = string[i];        /* 将新的操作数压入栈中 */
                                j++;
                                i++;
                                break;
                            }
                        case '/':
                            {
                                num[j] = opera[op-1];
                                opera[op-1] = string[i];
                                j++;
                                i++;
                                break;
                            }
                        case '(':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        }
                    }
                    break;
                }
            case '-':
                {
                    if (op == 0)
                    {
                        PushOperation(opera, string, &op, &i);
                        break;
                    }
                    if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
                    {
                        switch (opera[op-1])
                        {
                        case '+':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '-':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '*':
                            {
                                num[j] = opera[op-1];
                                opera[op-1] = string[i];
                                j++;
                                i++;
                                break;
                            }
                        case '/':
                            {
                                num[j] = opera[op-1];
                                opera[op-1] = string[i];
                                j++;
                                i++;
                                break;
                            }
                        case '(':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        }
                    }
                    break;
                }
            case '*':
                {
                    if (op == 0)
                    {
                        PushOperation(opera, string, &op, &i);
                        break;
                    }
                    if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
                    {
                        switch (opera[op-1])
                        {
                        case '+':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '-':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '*':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '/':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '(':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        }
                    }
                    break;
                }
            case '/':
                {
                    if (op == 0)
                    {
                        PushOperation(opera, string, &op, &i);
                        break;
                    }
                    if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
                    {
                        switch (opera[op-1])
                        {
                        case '+':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '-':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '*':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '/':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        case '(':
                            {
                                PushOperation(opera, string, &op, &i);
                                break;
                            }
                        }
                    }
                    break;
                }
            case '(':
                {
                    PushOperation(opera, string, &op, &i);
                    break;
                }
            case ')':    /* 如果遇到右括号 */
                {
                    while (opera[op-1] != '(')
                    {
                        num[j] = opera[op-1];    /* 将运算符栈中的元素依次入栈到后缀表达式栈中,直到遇到左括号为止 */
                        j++;
                        op--;
                    }
                    op--;
                    i++;
                    break;
                }
            default:
                {
                    printf("传入表达式不符合要求\n");
                    exit(0);
                }

            }
        }
    }
    while (op != 0)
    {
        num[j] = opera[op-1];    /* 将运算符栈中的元素依次入栈到后缀表达式栈中 */
        j++;
        op--;
    }
    num[j] = '\0';
    i = 0;
    while (num[i] != '\0')    /* 将后缀表达式存储到传入的形参string中 */
    {
        string[i] = num[i];
        i++;
    }
    string[i] = '\0';
    for(i=0;i<sizeof(string);i++)
    printf("%c",string[i]);
}

/* Function: 入运算符栈*/
void PushOperation(char *opera, char *string, int *op, int *s)
{
    opera[*op] = string[*s];
    (*op)++;
    (*s)++;
}

void main()
{
	char string[100];
	gets(string);
	PexpretoSexpre(string);
	
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值