文章

34

粉丝

51

获赞

2

访问

6.1k

头像
计算表达式 题解:

#include<bits/stdc++.h>
using namespace std;
string str;
int GetPriority(char ch){
    if(ch == '#') return 0;
    else if(ch == '$') return 1;
    else if(ch == '+' || ch == '-') return 2;
    else return 3;
}
double Calculate(double a,double b,char ch){
    if(ch == '+') return a + b;
    else if(ch == '-') return a - b;
    else if(ch == '*') return a * b;
    else if(ch == '/') return a / b;
    else return 0;
}

double GetNumber(int &i){
    double ans = 0;
    while(isdigit(str[i])){
        ans *= 10;
        ans += str[i] - '0';
        i++;
    }
    return ans;
}
stack<char> oper;
stack<double> data;
int main() {
    while(cin >> str){
        str += "$";
        oper.push('#');
        int len = str.size();
        int i = 0;
        while(i < len){
            if(isdigit(str[i])){
                double num = GetNumber(i);
                data.push(num);
            }
            else if(GetPriority(...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发