算术表达式的转换 2484 SDUT

算术表达式的转换

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。

   因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。

Input

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

Output

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

Sample Input

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

Sample Output

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

Hint

Source

方法一:

#include<stdio.h>
#include<string.h>
 
char a[1000],ls[1000], la[1000];
int cmp(char z)
{
    if(z == '+' || z == '-') return 1;
    if(z == '*' || z == '/') return 2;
    if(z == ')') return 3;
    if(z == '(') return 4;
}
 
int main()
{
    scanf("%s",a);
 
    int i = 0,k = 0, c = 0;
    int str  = strlen(a);
    str = str - 2;
    while(str >= 0)
    {
        if(a[str] >= 'a' && a[str] <= 'z')
        {
            la[c++] = a[str];
 
        }
        else
        {
            if(k == 0 )
            {
                ls[k++] = a[str];
            }
            else
            {
                if(cmp(ls[k-1]) <= cmp(a[str]))
                {
                    if(a[str] == '(')
                    {
                        k = k - 1;
                        for(; ls[k] != ')'; k--)
                        {
                            la[c++] = ls[k];
                        }
                    }
                    else
                    {
                        ls[k++] = a[str];
                    }
                }
                else
                {
                    if(ls[k-1] != ')')
                    {
                        la[c++] = ls[k-1];
                        ls[k-1] = a[str];
                    }
                    else
                        ls[k++] = a[str];
                }
            }
        }
        str--;
    }
    c = c- 1;
    for(i = 0; i < k; i++)
    {
        printf("%c",ls[i]);
    }
    for(; c >= 0; c--)
        printf("%c",la[c]);
    printf("\n");
    for(i = 0; a[i] != '#'; i++)
    {
        if(a[i] != '(' && a[i] != ')')
        {
            printf("%c",a[i]);
        }
    }
    printf("\n");
    k = 0;
    memset(ls,0,sizeof(ls));
    for(i = 0; a[i] != '#'; i++)
    {
        if(a[i] >= 'a' && a[i] <= 'z')
        {
            printf("%c",a[i]);
        }
        else
        {
            if(k == 0)
            {
                ls[k++] = a[i];
            }
            else
            {
                if(cmp(ls[k-1]) >= cmp(a[i]))
                {
                    if(ls[k-1]!= '(')
                    {
                        printf("%c",ls[k-1]);
                        ls[k-1] = a[i];
                    }
                    else
                    {
                        ls[k++] = a[i];
                    }
                }
                else
                {
                    if(a[i] == ')')
                    {
                        k = k-1;
                        for(; ls[k]!='('; k--)
                        {
                            printf("%c",ls[k]);
                        }
                    }
                    else
                    {
                        ls[k++] = a[i];
                    }
                }
            }
 
        }
    }
    k  = k - 1;
    while(k>=0)
    {
        printf("%c",ls[k]);
        k--;
    }
    printf("\n");
 
    return 0;
}

方法二:

#include <stdio.h>
#include <malloc.h>
struct node
{
    char s;
    struct node *l,*r;
};
char sa[100],sb[100],sc[100];
int p;
void first(struct node *q)
{
    if(q==NULL)
    {
        return ;
    }
    printf("%c",q->s);
    first(q->l);
    first(q->r);
}
void infix(struct node *q)
{
    if(q==NULL)
    {
        return ;
    }
    infix(q->l);
    printf("%c",q->s);
    infix(q->r);
}
void postfix(struct node *q)
{
    if(q==NULL)
    {
        return ;
    }
    postfix(q->l);
    postfix(q->r);
    printf("%c",q->s);
}
void h()
{
    int x=0,y=0;
    for(p=0; sa[p]!='#'; p++)
    {
        if(sa[p]>='a'&&sa[p]<='z')///如果是数字
        {
            sb[x]=sa[p];
            x++;
        }
        else if(sa[p]=='+'||sa[p]=='-')
        {
            while(y!=0&&sc[y-1]!='(')
            {
                sb[x]=sc[y-1];
                x++;
                y--;
            }
            sc[y]=sa[p];
            y++;
        }
        else if(sa[p]=='*'||sa[p]=='/')
        {
            while(y!=0&&(sc[y-1]=='*'||sc[y-1]=='/'))
            {
                sb[x]=sc[y-1];
                x++;
                y--;
            }
            sc[y]=sa[p];
            y++;
        }
        else if(sa[p]=='(')
        {
            sc[y]=sa[p];
            y++;
        }
        else if(sa[p]==')')
        {
            while(sc[y-1]!='(')
            {
                sb[x]=sc[y-1];
                x++;
                y--;
            }
            y--;
        }
    }
    while(y!=0)
    {
        sb[x]=sc[y-1];
        x++;
        y--;
    }
    sb[x]='\0';
}
int main()
{
    int d=0,i;
    scanf("%s",sa);
    h();
    struct node *po[100]={NULL},*pi;
    for(i=0;i<p;i++)
    {
        if(sb[i]>='a'&&sb[i]<='z')
        {
            pi=(struct node *)malloc(sizeof(struct node));
            pi->s=sb[i];
            pi->l=NULL;
            pi->r=NULL;
            po[d]=pi;
            d++;
        }
        else
        {
            pi=(struct node *)malloc(sizeof(struct node));
            pi->s=sb[i];
            pi->r=po[d-1];
            d--;
            pi->l=po[d-1];
            d--;
            po[d]=pi;
            d++;
        }
    }
    first(po[0]);
    printf("\n");
    infix(po[0]);
    printf("\n");
    postfix(po[0]);
    printf("\n");
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/Allinone99/article/details/81358665