后缀表达式的求值

 

#include 
#include 
#include 
struct node{
    int top;
    double a[100];
}stack;
int main()
{
    stack.top=-1;
    char s[100];
    scanf("%s",s);
    int i=0;
    while(i='0'&&s[i]<='9')
            stack.a[++(stack.top)]=s[i]-'0';
        else {
            double temp;
            switch(s[i]){
                case '+':
                    temp=stack.a[stack.top-1]+stack.a[stack.top];
                    break;
                case '-':
                    temp=stack.a[stack.top-1]-stack.a[stack.top];
                    break;
                case '*':
                    temp=stack.a[stack.top-1]*stack.a[stack.top];
                    break;
                case '/':
                    temp=stack.a[stack.top-1]/stack.a[stack.top];
                    break;
            }
            printf("**%.1f**",temp);
            stack.a[--(stack.top)]=temp;
        }
        printf("%d %.1f\n",stack.top,stack.a[stack.top]);
        i++;
    }
    printf("%.1f\n",stack.a[stack.top]);
    return 0;
}

 

你可能感兴趣的:(数据结构)