RE求助
  • 板块学术版
  • 楼主shipeiqian
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/8/19 13:18
  • 上次更新2023/10/27 14:37:26
查看原帖
RE求助
632063
shipeiqian楼主2022/8/19 13:18

以下代码为什么会出现除零错误啊。(题目是p1449)

#include <bits/stdc++.h>
//#include <windows.h>
using namespace std;
typedef long long ll;
ll toint(string s){
    ll tot=0;
    for(int i=0;i<s.size();i++){
        tot=tot*10+s[i]-'0';
    }
    return tot;
}
int main(){
    string s;
    stack<ll> st;
    cin >>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='@')break;
        if(s[i]=='.')continue;
        if(s[i]<'0'||s[i]>'9'){
            int a,b,cnt;
            a=st.top();
            st.pop();
            b=st.top();
            st.pop();
            if(s[i]=='+')cnt=b+a;
            else if(s[i]=='-')cnt=b-a;
            else if(s[i]=='*')cnt=b*a;
            else if(s[i]=='/')cnt=b/a;
            st.push(cnt);
            continue;
        }
        string n="";
        int j=i;
        while(s[j]!='.'){
            n+=s[j];
            j++;
        }
        st.push(toint(n));
    }
    cout <<st.top();
    //system("pause");
    return 0;
}

别问为什么要用pause,问就是VS Code的尿性

2022/8/19 13:18
加载中...