数据结构实验之栈三:后缀式求值

数据结构实验之栈三:后缀式求值

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

对于一个基于二元运算符的后缀表示式(基本操作数都是一位正整数),求其代表的算术表达式的值。

输入

输入一个算术表达式的后缀式字符串,以‘#’作为结束标志。

输出

求该后缀式所对应的算术表达式的值,并输出之。

示例输入

59*684/-3*+#

示例输出

57

提示

基本操作数都是一位正整数!


后缀表达式求值:当输入的字符是数字时直接入栈,当输入的字符在是运算符时,每一个操作符用于运算它前面的两个数字,得出的结果也作为一个数字入栈。遍历字符串数组结束后弹出栈顶元素,即为结果。


#include <stdio.h>

#include <stdlib.h>
#include <string.h>
typedef struct {
    int  *base;
    int  *top;
    int   Lsize;
}Sqstack;


Sqstack Create(Sqstack &s)
{
    s.base=(int *)malloc(1010*sizeof(int ));
    s.top=s.base;
    return s;
}


Sqstack push(Sqstack &s,int e){
    *s.top++=e;
}


int  gettop(Sqstack &s)
{
    int e;
    e=*(s.top-1);
    return e;
}


int pop(Sqstack &s){
    int  a;
    if(s.top==s.base)return -1;
    s.top--;
    a=*s.top;
    return a ;
    }


int Empty(Sqstack &s){
    if(s.top==s.base) return 1;
    else return 0;
}


int clearstack(Sqstack &s){
    s.top=s.base;
}
int main()
{
    int i,len,j,x,y;
    char a[1001];
    int b[1001];
    Sqstack S;
    S=Create(S);
    scanf("%s",a);
    len=strlen(a);
    for(i=0;i<len-1;i++){
            if(a[i]<='9'&&a[i]>='0'){
                push(S,a[i]-48);
            }
            else if(a[i]=='+'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x+y);
            }
            else if(a[i]=='-'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x-y);
            }
            else if(a[i]=='*'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x*y);
            }
            else if(a[i]=='/'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x/y);
            }
    }
    j=gettop(S);
    printf("%d",j);

}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
    int *base;
    int *top;
    int  Lsize;
}Sqstack;


Sqstack Create(Sqstack &s)
{
    s.base=(int *)malloc(1010*sizeof(int));
    s.top=s.base;
    return s;
}


Sqstack push(Sqstack &s,int e){
    *s.top++=e;
}


int gettop(Sqstack &s)
{
    int e;
    e=*(s.top-1);
    return e;
}


int  pop(Sqstack &s){
    int a;
    if(s.top==s.base)return -1;
    s.top--;
    a=*s.top;
    return a ;
    }


int Empty(Sqstack &s){
    if(s.top==s.base) return 1;
    else return 0;
}


int clearstack(Sqstack &s){
    s.top=s.base;
}
int main()
{
    int i,j,len,x,y;
    char a[1001];
    int b[1001];
    Sqstack S;
    S=Create(S);
    scanf("%s",&a);
    len=strlen(a);
    for(i=0;i<len-1;i++){
            if(a[i]=='1'){
                b[i]=1;
                push(S,b[i]);
            }else if(a[i]=='2'){
                 b[i]=2;
                push(S,b[i]);
            }
            else if(a[i]=='3'){
                 b[i]=3;
                push(S,b[i]);
            }
            else if(a[i]=='4'){
                 b[i]=4;
                push(S,b[i]);
            }
            else if(a[i]=='5'){
                 b[i]=5;
                push(S,b[i]);
            }
            else if(a[i]=='6'){
                 b[i]=6;
                push(S,b[i]);
            }
            else if(a[i]=='7'){
                 b[i]=7;
                push(S,b[i]);
            }
            else if(a[i]=='8'){
                 b[i]=8;
                push(S,b[i]);
            }
            else if(a[i]=='9'){
                 b[i]=9;
                push(S,b[i]);
            }
            else if(a[i]=='+'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x+y);
            }
            else if(a[i]=='-'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x-y);
            }
            else if(a[i]=='*'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x*y);
            }
            else if(a[i]=='/'){
                y=gettop(S);
                pop(S);
                x=gettop(S);
                pop(S);
                push(S,x/y);
            }
    }
    j=gettop(S);
    printf("%d",j);
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值