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

来源:互联网 发布:看表情猜网络用语 编辑:程序博客网 时间:2024/05/16 06:22


若已经给出了后缀式,求值,其规则为,建立一个栈,从左到右计算。若为数值,入栈,若为运算符,让栈顶两个数值运算,且运算顺序为从左到右,并把新的元素重新入栈。此题用数组也可解决,

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

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

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

Input

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

Output

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

Example Input

59*684/-3*+#

Example Output

57

Hint

基本操作数都是一位正整数!
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACKINCREMENT 10000     //存储空间分配增量
#define STACKINITSIZE 10000    //存储空间初始分配量
typedef int SElemType;
typedef struct
{
    SElemType *base;//定义栈底指针;
    SElemType *top;//定义暂定指针,
    int stacksize;//当前已分配的存储空间
}SqStack;
int InitStack(SqStack *S)  //初始化栈
{
    S->base = (SElemType *)malloc(STACKINITSIZE*sizeof(SElemType));//为栈底开辟空间
    if(!S->base)
        return 0;//如果开辟空间失败,返回为0;
    S->top= S->base;
    S->stacksize = STACKINITSIZE;//S->stacksize为开辟的空间
    return 1;
}
int Push(SqStack *S,int e)//入栈函数
{
    if(S->top - S->base>=S->stacksize)//先判断空间是否足够
    {
        S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT)*sizeof(SElemType));//新开的空间,用realloc函数
        if(!S->base)
            return 0;//如果创建失败,返回0
        S->top = S->base + S->stacksize;//再次确定S->top的地址,防止S->base改变。而丢失了S->top的地址
        S->stacksize = S->stacksize + STACKINCREMENT;//更新S->stacksize
    }
    *(S->top) = e;//进行进栈操作
    S->top++;
    return 1;
}




int f(char a,int b,int c)
{
    if(a=='+')
        return b+c;
    if(a=='-')
        return b-c;
    if(a=='*')
        return b*c;
    if(a=='/')
        return b/c;
    return 1;
}
int Judge(char s,SqStack *S)
{
    int t;
    t = f(s,*(S->top-2),*(S->top-1));//前一个与后一个运算,一定要搞清楚运算顺序,搞明白哪两个进行运算。
    S->top--;
    *(S->top-1) = t;
    return 1;
}
int main()
{
   SqStack S;
   InitStack(&S);
   char s[10000];
   gets(s);
   int n = strlen(s);
   int i;
   for(i = 0;i<=n-1;i++)
   {
       if(s[i]=='#')
       break;
       if(s[i]>='0'&&s[i]<='9')
       {
           Push(&S,(int)(s[i]-48));//将char型转成数值型,直接相减也行,直接为数值型,‘s[i] ’-'0',也可以


       }
       else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
       {
           Judge(s[i],&S);
        }
   }
   printf("%d",*(S.top-1));//最后输出栈顶元素


   printf("\n");


    return 0;
}

法二:数组。
view source
print?
01#include <stdio.h>
02#include <stdlib.h>
03#include <string.h>
04int f(char a,int b,int c)
05{
06    if(a=='+')
07        return b+c;
08    if(a=='-')
09        return b-c;
10    if(a=='*')
11        return b*c;
12    if(a=='/')
13        return b/c;
14    return 1;
15}
16int Judge(char s,int a[],int n)
17{
18    int t;
19    t = f(s,a[n-1],a[n]);
20    a[n-1] = t;
21    return a[n-1];
22}
23int main()
24{
25   char s[10000];
26   int a[10000];
27   gets(s);
28   int n = strlen(s);
29   int i,j = 0;
30   for(i = 0;i<=n-1;i++)
31   {
32       if(s[i]=='#')
33       break;
34       if(s[i]>='0'&&s[i]<='9')
35       {
36           a[j] = (int)(s[i]-48);
37           j++;
38 
39       }
40       else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
41       {
42           Judge(s[i],a,j-1);
43           j--;
44        }
45   }
46   printf("%d",a[0]);
47 
48   printf("\n");
49 
50    return 0;
51}
52     


原创粉丝点击