表达式计算模板(+-*/^)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int number[101],i=0,p=1;
char symbol[101],s[256],t[256];
void push(){symbol[++p]=s[i];}
int Pow(int a,int b){
    int res=1;
    while(b){
        if(b&1)res=res*a;
        a=a*a;
        b>>=1;
    }
    return res;
}
void pop(){
    switch(symbol[p--]){
        case'+':number[p]+=number[p+1];break;
        case'-':number[p]-=number[p+1];break;
        case'*':number[p]*=number[p+1];break;
        case'/':number[p]/=number[p+1];break;
        case'^':number[p]=Pow(number[p],number[p+1]);break;
    }
}
bool can(){
    if((s[i]=='+'||s[i]=='-')&&symbol[p]!='(')return 1;
    if((s[i]=='*'||s[i]=='/')&&(symbol[p]=='*'||symbol[p]=='/'))return 1;
    if(s[i]=='^'&&symbol[p]=='^')return 1;
    return 0;
}
int main(){
    //scanf("%s",s);
    freopen("Soda.txt","r",stdin);
    gets(s);
    s[strlen(s)]=')';symbol[p]='(';
    while(i<strlen(s)){
        while(s[i]==' ')i++;
        while(s[i]=='('){
            push();i++;
            while(s[i]==' ')i++;
        }
        int x=0;
        while(s[i]>='0'&&s[i]<='9'){
            x=x*10+s[i]-'0';
            i++;
            while(s[i]==' ')i++;
        }
        number[p]=x;
        int j=0;
        do{
            if(s[i]==')'){
                while(symbol[p]!='(')pop();
                number[--p]=number[p+1];
            }
            else{
                while(can())pop();
                push();
            }
            j=0;
            i++;j++;
            while(s[i]==' ')i++,j++;
        }while(i<strlen(s)&&s[i-j]==')');
    }
    printf("%d",number[0]);
    return 0;
}
/*
    by林永迪
    读入一个字符串,里面有+-*/()
*/
#include <cstdio>
#include <cstring>
const int strsize = 1024;

int n, ops, nms;
char str[strsize], opt[strsize];
int num[strsize];
void pop() {
    char op = opt[--ops];
    int x = num[--nms], y = num[--nms], z;
    switch (op) {
        case '+': z = y + x; break;
        case '-': z = y - x; break;
        case '*': z = y * x; break;
        case '/': z = y / x; break;
    }
    num[nms++] = z;
}

int main() {
    int n;
    for (scanf("%d", &n); n; n--) {
        scanf("%s", str);
        int len = strlen(str);
        ops = nms = 0;
        int x = 0;
        for (int i = 0; i < len; i++)
            if (str[i] >= '0' && str[i] <= '9')
                x = x * 10 + str[i] - '0';
            else {
                if (x > 0) num[nms++] = x, x = 0;
                if (str[i] == ')') {
                    while (opt[ops - 1] != '(') pop();
                    --ops;
                    continue;
                }
                if (str[i] == '+' || str[i] == '-')
                    while (ops > 0 && opt[ops - 1] != '(') pop();
                else if (str[i] == '*' || str[i] == '/')
                    while (ops > 0 && opt[ops - 1] != '(' && (opt[ops - 1] == '*' || opt[ops - 1] == '/')) pop();
                opt[ops++] = str[i];
            }
        if (x > 0) num[nms++] = x, x = 0;
        while (ops > 0) pop();
        printf("%d
", num[0]);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/thmyl/p/7667642.html