三重雪关扉

#include <stdio.h>
#include <stdlib.h>

typedef struct Stack{
    char data[10];
    int top;
}stack, *sp;

sp createStack(){
    sp ptr = malloc(sizeof(stack));
    ptr->top = -1;
    return ptr;
}

void Push(sp ptr, char c){
    ptr->data[++ptr->top] = c;
}

char Pop(sp ptr){
    if(ptr->top == -1) return 'w';
    return ptr->data[ptr->top--];
}
char Top(sp ptr){
    if(ptr->top == -1) return 'w';
    return ptr->data[ptr->top];
}
int main(){
    char str[50], c, result[50]={'\0'};
    scanf("%s",str);
    sp ptr1 = createStack(), ptr2 = createStack();
    int i=0, j=0;
    while(str[i] != '\0'){
        c = str[i];
        if(c >= '0' && c <= '9') {
            result[j] = c;
            j++;
            }
        else{
            char temp=Top(ptr2);
            if(temp=='w' || c=='(') Push(ptr2, c);
            else if((c=='*' || c=='/') && (temp=='+' || temp=='-')) Push(ptr2, c);
            else if((c=='*' || c=='/') && (temp=='*' || temp=='/')) {result[j]=Pop(ptr2);Push(ptr2, c);j++;}
            else if(c==')'){temp=Pop(ptr2);while(temp!='('){result[j]=temp;j++;temp=Pop(ptr2);}}
            else {while(ptr2->top!=-1 && Top(ptr2)!='('){result[j]=Pop(ptr2);j++;}Push(ptr2, c);}
        }
        i++;
    }
    while(ptr2->top!=-1){
        result[j] = Pop(ptr2);
        j++;
    }
    int h=0, top=-1;
    float s[20]={0};
    while(result[h] != '\0'){
        if(result[h] >= '0' && result[h] <= '9') {s[++top]=result[h]-48;h++;continue;}
        if(result[h] == '+'){s[top-1] += s[top];top--;}
        else if(result[h] == '-'){s[top-1] -= s[top];top--;}
        else if(result[h] == '*'){s[top-1] *= s[top];top--;}
        else if(result[h] == '/'){s[top-1] /= s[top];top--;}
        h++;
    }
    printf("%.2f\n",s[0]);
    i=0;
    while(result[i]!='\0'){
        printf("%c ",result[i]);
        i++;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值