注册 登录
编程论坛 C语言论坛

为什么会报错?求大神解决!

Henry1559 发布于 2023-05-13 20:52, 57 次点击
程序代码:
#include <bits/stdc++.h>
using namespace std;
int priority(char op){
    if(op=='+'||op=='-') return 1;
    else if(op=='*'||op =='/') return 2;
    else return 0;
}
float count(int a,int b,char op){
    if(op=='+') return a+b;
    else if(op=='-') return a-b;
    else if(op=='*') return a*b;
    else if(op=='^') return pow(a,b);
    else return a/b;
}
int main() {
    string s;
    getline(cin,s);
    stack<float> num_;//数字
    stack<char> op_;//运算符
    for (int i=0;i<s.length();i++){
        if (s[i]>='0'&&s[i]<='9'){
            int num=0;
            while(i<s.length()&&isdigit(s[i])){
                num=num*10+(s[i]-'0');
                i++;
            }
            i--; //
            num_.push(num);
        }
        else if(s[i]=='(')
            op_.push(s[i]);
        else if(s[i] == ')'){
            while(!op_.empty()&&op_.top()!='(') {
                char op=op_.top();
                op_.pop();
                float b=num_.top();
                num_.pop();
                float a=num_.top();
                num_.pop();
                num_.push(count(a, b, op));
            }
            op_.pop();
        }
        else{
            while(!op_.empty()&&priority(op_.top())>=priority(s[i])) {
                char op = op_.top();
                op_.pop();
                float b=num_.top();
                num_.pop();
                float a=num_.top();
                num_.pop();
                num_.push(count(a, b, op));
            }
            op_.push(s[i]);
        }
    }
    while(!op_.empty()) {
        char op=op_.top();
        op_.pop();
        float b=num_.top();
        num_.pop();
        float a=num_.top();
        num_.pop();
        num_.push(count(a,b,op));
    }
    printf("%f\n",&num_.top());
    return 0;
}
1 回复
#2
东海ECS2023-05-13 20:58
倒数第3行:
printf("%f\n",&num_.top());
这里有冗余的&
1