后缀表达式

or:rgb(0,0,0);font-style:normal;font-v ★实验任务热爱数学的小明在学习算式, 他发现算式的表达不止一种, 有一种叫做后缀表达式, 而我们平常使用的是中缀表达式(a+b 是中缀表达式, 而 ab+是后缀表达式),现在小明想对一些中缀表达式计算出他们的后缀表达式, 并通过后缀表达式计算出值,你能帮助他吗?
★数据输入读入一个只包含 +, -, *, / , (, ) 的非负整数计算表达式,该表达式的后缀表达式。每个输入为一行字符串, 字符串长度不超过 100000,整数和运算符之间用一个空格分隔。没有非法表达式。
★数据输出对于每个输入, 输出 2 行, 第一行即该表达式的后缀表达式, 数与数之间、 符号与数之间、符号与符号之间都应有一个空格隔开(注意行末无空格),第二行是该算式的值, 精确到小数点后 2 位。

输入示例         输出示例

681 + 62 - 83     681 62 + 83-
                  660.00

 
 
             #include <stdio.h>   
#include <stdlib.h>   
#include <malloc.h>   
#include <ctype.h>   
 
#define STACK_INIT_SIZE 100000   
#define STACKINCREMENT  10   
#define MAXBUFFER       20   
 
typedef char BaseType,BaseType1;   
typedef double ElemType;   
typedef struct  
{   
    BaseType *base;   
    BaseType *top;   
    int stackSize;   
}aStack;   
 
typedef struct  
{   
    BaseType1 *base;   
    BaseType1 *top;   
    int stackSize;   
}aStack1;   
 
typedef struct  
{   
    ElemType *base;   
    ElemType *top;   
    int stackSize;   
}sqStack;   
 
void StackInit(aStack *s);   
void Push(aStack *s,BaseType b);   
void Pop(aStack *s,BaseType *b);   
int StackLen(aStack s);   
 
void StackInit1(aStack1 *s);   
void Push2(aStack1 *s,BaseType1 b);   
void Pop2(aStack1 *s,BaseType1 *b);   
int StackLen2(aStack1 s);   
 
void InitStack(sqStack *s);   
void Push1(sqStack *s,ElemType b);   
void Pop1(sqStack *s,ElemType *b);   
int StackLen1(sqStack s);   
 
int main()   
{   
    aStack s;   
    sqStack q;   
    aStack1 r;   
    char c,e;   
    char str[MAXBUFFER];   
    double d,f;   
    int i=0;   
    StackInit(&s);   
    InitStack(&q);   
    StackInit1(&r);   
 
    scanf("%c",&c);   
    while(c!='\n')   
    {   
        while(c>='0'&&c<='9')   
        {   
            Push2(&r,c);   
            printf("%c",c);   
            scanf("%c",&c);   
            if(c<'0'||c>'9')   
            {   
                Push2(&r,' ');   
                printf(" ");   
            }   
        }   
        if(c==')')   
        {   
            Pop(&s,&e);   
            while(e!='(')   
            {   
                Push2(&r,e);   
                Push2(&r,' ');   
                printf("%c ",e);   
                Pop(&s,&e);   
            }   
        }   
        else if(c=='+'||c=='-')   
        {   
            if(!StackLen(s))   
            {   
                Push(&s,c);   
            }   
            else  
            {   
                do  
                {   
                    Pop(&s,&e);   
                    if(e=='(')   
                    {   
                        Push(&s,e);   
                    }   
                    else  
                    {   
                        Push2(&r,e);   
                        Push2(&r,' ');   
                        printf("%c ",e);   
                    }   
                }while(StackLen(s)&&e!='(');   
                    Push(&s,c);   
            }   
        }   
        else if(c=='(')   
        {   
            Push(&s,c);   
        }   
        else if(c=='*'||c=='/')   
        {   
            if(!StackLen(s))   
            {   
                Push(&s,c);   
            }   
            else  
            {   
                Pop(&s,&e);   
                if(e=='*'||e=='/')   
                {   
                    Push2(&r,e);   
                    Push2(&r,' ');   
                    printf("%c ",e);   
                }   
                else  
                {   
                    Push(&s,e);   
                }   
                Push(&s,c);   
            }   
        }   
        else if(c=='\n')   
            break;   
        scanf("%c",&c);   
    }   
    while(StackLen(s))   
    {   
        Pop(&s,&e);   
        Push2(&r,e);   
        Push2(&r,' ');   
        printf("%c ",e);   
    }   
    e='#';   
    Push2(&r,e);   
    while(StackLen2(r))   
    {   
        Pop2(&r,&c);   
        Push(&s,c);   
    }   
    Pop(&s,&c);   
    while(c!='#')   
    {   
        while(isdigit(c))   
        {   
            str[i++]=c;   
            str[i]='\0';   
            Pop(&s,&c);   
            if(c==' ')   
            {   
                d=atof(str);   
                Push1(&q,d);   
                i=0;   
                break;   
            }   
        }   
        switch(c)   
        {   
        case'+':   
            Pop1(&q,&f);   
            Pop1(&q,&d);   
            Push1(&q,d+f);   
            break;   
        case'-':   
            Pop1(&q,&f);   
            Pop1(&q,&d);   
            Push1(&q,d-f);   
            break;   
        case'*':   
            Pop1(&q,&f);   
            Pop1(&q,&d);   
            Push1(&q,d*f);   
            break;   
        case'/':   
            Pop1(&q,&f);   
            Pop1(&q,&d);   
            Push1(&q,d/f);   
            break;   
        }   
        Pop(&s,&c);   
    }   
    Pop1(&q,&d);   
    printf("\n%.2f",d);   
    return 0;   
}   
 
void StackInit(aStack *s)   
{   
    s->base=(BaseType *)malloc(STACK_INIT_SIZE*sizeof(BaseType));   
    if(!s->base)   
        exit(0);   
    s->top=s->base;   
    s->stackSize=STACK_INIT_SIZE;   
}   
 
void Push(aStack *s,BaseType b)   
{   
    if(s->top-s->base>=s->stackSize)   
    {   
        s->base=(BaseType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(BaseType));   
        if(!s->base)   
            exit(0);   
    }   
    *(s->top)=b;   
    s->top++;   
}   
 
void Pop(aStack *s,BaseType *b)   
{   
    if(s->top==s->base)   
        return;   
    *b=*--(s->top);   
}   
 
int StackLen(aStack s)   
{   
    return(s.top-s.base);   
}   
 
void StackInit1(aStack1 *s)   
{   
    s->base=(BaseType1 *)malloc(STACK_INIT_SIZE*sizeof(BaseType1));   
    if(!s->base)   
        exit(0);   
    s->top=s->base;   
    s->stackSize=STACK_INIT_SIZE;   
}   
 
void Push2(aStack1 *s,BaseType1 b)   
{   
    if(s->top-s->base>=s->stackSize)   
    {   
        s->base=(BaseType1 *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(BaseType1));   
        if(!s->base)   
            exit(0);   
    }   
    *(s->top)=b;   
    s->top++;   
}   
 
void Pop2(aStack1 *s,BaseType1 *b)   
{   
    if(s->top==s->base)   
        return;   
    *b=*--(s->top);   
}   
 
int StackLen2(aStack1 s)   
{   
    return(s.top-s.base);   
}   
 
void InitStack(sqStack *s)   
{   
    s->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));   
    if(!s->base)   
        exit(0);   
    s->top=s->base;   
    s->stackSize=STACK_INIT_SIZE;   
}   
 
void Push1(sqStack *s,ElemType b)   
{   
    if(s->top-s->base>=s->stackSize)   
    {   
        s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(ElemType));   
        if(!s->base)   
            exit(0);   
    }   
    *(s->top)=b;   
    s->top++;   
}   
 
void Pop1(sqStack *s,ElemType *b)   
{   
    if(s->top==s->base)   
        return;   
    *b=*--(s->top);   
}   
 
int StackLen1(sqStack s)   
{   
    return(s.top-s.base);   
}
       

   



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值