计算表达式

#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
#define MAXSIZE 100
struct seq {
    char st[MAXSIZE];
    int top;
};
struct op {
    int data[MAXSIZE];
    int top;
};
bool isEmpty(seq *s) {
    return (!(~(s -> top)));
}
bool push(seq *s, char c) {
    if(s -> top == MAXSIZE) return false;
    s -> st[++s -> top] = c;
    return true;
}
bool pop(seq *s, char *c) {
    if(isEmpty(s)) return false;
    *c = s -> st[s -> top];
    s -> top--;
    return true;
}
bool getTop(seq *s, char *c) {
    if(isEmpty(s)) return false;
    *c = s -> st[s -> top];
    return true;
}
void trans(char s1[], char s2[]) {
    seq s;
    char ch, e = '\0';
    int i = 0, j = 0;
    s.top = -1;
    ch = s1[i++];
    while(ch != '\0') {
        switch(ch) {
            case '(':
                push(&s, ch);
                break;
            case ')':
                while(getTop(&s, &e) && e != '(') {
                    pop(&s, &e);
                    s2[j++] = e;
                }
                pop(&s, &e);
                break;
            case '+':
            case '-':
                while(!isEmpty(&s) && getTop(&s, &e) && e != '(') {
                    pop(&s, &e);
                    s2[j++] = e;
                }
                push(&s, ch);
                break;
            case '*':
            case '/':
                while(!isEmpty(&s) && getTop(&s, &e) && (e == '/' || e == '*')) {
                    pop(&s, &e);
                    s2[j++] = e;
                }
                push(&s, ch);
                break;
            case ' ':
                break;
            default:
                while(isdigit(ch)) {
                    s2[j++] = ch;
                    ch = s1[i++];
                }
                i--;
                s2[j++] = ' ';
        }
        ch = s1[i++];
    }
    while(!isEmpty(&s)) {
        pop(&s, &e);
        if(e != ' ') s2[j++] = e;
    }
    s2[j] = '\0';
}
int cal(char a[]) {
    op s;
    int i = 0, data, res = 0;
    int x1, x2;
    s.top = -1;
    while(a[i] != '\0') {
        if(isdigit(a[i])) {
            data = 0;
            while(a[i] != ' ') {
                data = data * 10 + a[i] - '0';
                i++;
            }
            s.data[++s.top] = data;
        }
        else {
            switch(a[i]) {
                case '+':
                    x1 = s.data[s.top--];
                    x2 = s.data[s.top--];
                    res = x1 + x2;
                    s.data[++s.top] = res;
                    break;
                case '-':
                    x1 = s.data[s.top--];
                    x2 = s.data[s.top--];
                    res = x2 - x1;
                    s.data[++s.top] = res;
                    break;
                case '*':
                    x1 = s.data[s.top--];
                    x2 = s.data[s.top--];
                    res = x1 * x2;
                    s.data[++s.top] = res;
                    break;
                case '/':
                    x1 = s.data[s.top--];
                    x2 = s.data[s.top--];
                    res = x2 / x1;
                    s.data[++s.top] = res;
                    break;
            }
        }
        i++;
    }
    if(s.top != -1) res = s.data[s.top--];
    return res;
}
int main() {
    char str1[MAXSIZE], str2[MAXSIZE];
    fgets(str1, 100, stdin);
    trans(str1, str2);
    printf("%d\n", cal(str2));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值