数据结构之栈:算数表达式的转换 (sdut oj2484)

本文介绍了一种将算术表达式转换为前缀、中缀和后缀表示的方法,并提供了详细的算法实现步骤及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算术表达式的转换

Time Limit: 1000MS Memory limit: 65536K

题目描述

小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。
   因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。

输入

 输入一算术表达式,以\'#\'字符作为结束标志。(数据保证无空格,只有一组输入)

输出

 输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。

示例输入

a*b+(c-d/e)*f#

示例输出

+*ab*-c/def
a*b+c-d/e*f
ab*cde/-f*+

提示

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

int cmp(char c)
{
    if(c == '+'||c == '-') return 1 ;
    if(c == '*'||c == '/') return 2 ;
    if(c == '(')  return 3 ;
    if(c == ')')  return 4 ;
    return 0 ;
}


void Qianzhui(char a[])
{
    int top2 = -1, top3=-1;
    int l = strlen(a);
    char stack2[100], stack3[100];
    for(int i = l-2; i >= 0; i--)
    {
        if(a[i] >= 'a'&&a[i]<= 'z')
        {
            stack3[++top3] = a[i];
        }
        else
        {
            if(top2 == -1||stack2[top2] == ')'|| a[i] == ')')
            {
                stack2[++top2] = a[i];
            }
            else if(a[i] == '(')
            {
                while(stack2[top2] != ')')
                {
                    stack3[++top3] = stack2[top2--];
                }
                top2--;
            }
            else
            {
                if(cmp(a[i]) < cmp(stack2[top2]))
                {
                    stack3[++top3]= stack2[top2--];
                    i++;
                }
                else
                {
                    stack2[++top2] = a[i];
                }

            }
        }
    }
    while(top2 != -1)
    {
        stack3[++top3] = stack2[top2--];
    }
    for(int i = top3; i >= 0; i--)
        printf("%c", stack3[i]);
    printf("\n");
}

void Zhongzhui(char a[])
{
    for(int i = 0; a[i] != '#'; i++)
    {
        if(a[i] !='(' &&a[i] != ')')
            printf("%c", a[i]);
    }
    printf("\n");
}

void Houzhui(char a[])
{
    int top1 = 0;
    char stack1[100] ;
    for(int i = 0; a[i] != '#'; i++)
    {
        if(a[i] >= 'a'&&a[i] <= 'z')
        {
            printf("%c", a[i]) ;
        }
        else
        {
            if(top1 == 0)
            {
                top1++ ;
                stack1[top1] = a[i] ;
            }
            else if(cmp(a[i]) > cmp(stack1[top1]))
            {
                if(cmp(a[i]) == 4)
                {
                    while(stack1[top1] != '(')
                    {
                        printf("%c", stack1[top1--]);
                    }
                    top1-- ;
                }
                else
                {
                    top1++ ;
                    stack1[top1] = a[i];
                }
            }
            else
            {
                if(stack1[top1] != '(')
                {
                    printf("%c", stack1[top1]) ;
                    stack1[top1] = a[i] ;
                }
                else
                {
                    top1++ ;
                    stack1[top1] = a[i] ;
                }
            }
        }
    }
    while(top1 != 0)
    {
        printf("%c", stack1[top1]) ;
        top1-- ;
    }
    printf("\n") ;
}

int main()
{
    char a[1005];
    scanf("%s",a);
    Qianzhui(a);
    Zhongzhui(a);
    Houzhui(a);
    return 0;
}

2:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int judge(char a,char b)
{
    if((a=='*'||a=='/')&&(b=='+'||b=='-'))
        return 1;
    else if((a=='+'||a=='-')&&(b=='*'||b=='/'))
        return -1;
    else if((a=='+'||a=='-')&&(b=='+'||b=='-'))
        return 0;
    else if((a=='*'||a=='/')&&(b=='*'||b=='/'))
        return 0;
}
void houzhui(char a[])
{
    char c2[1005],c1[1005];
    int t1,t2,i;
    t1=t2=-1;
    for(i=0; a[i]!='#'; i++)
    {
        if(a[i]!='*'&&a[i]!='-'&&a[i]!='+'&&a[i]!='/'&&a[i]!='('&&a[i]!=')')
        {
            c2[++t2]=a[i];
        }
        else
        {
            if(t1==-1||c1[t1]=='('||a[i]=='(')
            {
                c1[++t1]=a[i];
                //  printf("%c",c1[t1]);
            }
            else if(a[i]==')')
            {
                //t1--;
                while(c1[t1]!='(')
                {
                    c2[++t2]=c1[t1--];
                }
                t1--;
            }
            else
            {
                if(judge(a[i],c1[t1])==1)
                    c1[++t1]=a[i];
                else
                {
                    c2[++t2]=c1[t1--];
                    i--;
                }
            }
        }
    }
    while(t1!=-1)
    {
        c2[++t2]=c1[t1--];
    }
    for(int i=0; i<=t2; i++)
        printf("%c",c2[i]);
        printf("\n");

}
void zhongzhui(char a[])
{
    char c2[1005],c1[1005];
    int t1,t2,i;
    t1=t2=-1;
    for(i=0; a[i]!='#'; i++)
    {
        if(a[i]!='('&&a[i]!=')')
        {
            c2[++t2]=a[i];
        }
    }
    for(int i=0; i<=t2; i++)
        printf("%c",c2[i]);
        printf("\n");

}
void qianzhui(char a[])
{
    char c2[1005],c1[1005];//c2��c1���符
    int t1,t2,i;
    t1=t2=-1;
    int l=strlen(a);
    for(i=l-2; i>=0; i--)
    {
        if(a[i]!='*'&&a[i]!='-'&&a[i]!='+'&&a[i]!='/'&&a[i]!='('&&a[i]!=')')
        {
            c2[++t2]=a[i];
        }
        else
        {
            if(t1==-1||c1[t1]==')'||a[i]==')')
            {
                c1[++t1]=a[i];
                //  printf("%c",c1[t1]);
            }
            else if(a[i]=='(')
            {
                //t1--;
                while(c1[t1]!=')')
                {
                    c2[++t2]=c1[t1--];
                }
                t1--;
            }
            else
            {
                if(judge(a[i],c1[t1])==-1)
                 {
                    c2[++t2]=c1[t1--];
                    i++;
                }
                else
                c1[++t1]=a[i];
            }
        }
    }
    while(t1!=-1)
    {
        c2[++t2]=c1[t1--];
    }
    for(int i=t2; i>=0; i--)
        printf("%c",c2[i]);
        printf("\n");

}
int main()
{
    char a[1005];
    scanf("%s",a);
    qianzhui(a);
    zhongzhui(a);
    houzhui(a);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值