#39285: 溢位或者除以0的錯誤!! Floating point exception (core dumped)


cjw931027@gmail.com (JERRY)

學校 : 國立花蓮高級中學
編號 : 150971
來源 : [111.243.107.229]
最後登入時間 :
2023-09-14 21:26:12
a017. 五則運算 | From: [42.72.107.85] | 發表日期 : 2024-02-01 13:32

#include <bits/stdc++.h>
using namespace std;

int main() {
    string ww;
    while(getline(cin,ww)){
        vector<char> s;
        string str;
        for(char c : ww){
            if(c == '('){
                s.push_back(c);
            }else if(c == ')'){
                while (s.size() && s.back() != '('){
                str+=s.back(),s.pop_back();
                }
                s.pop_back();
            }else if(c == '+' || c == '-'){
                while(s.size() && s.back() != '('){
                    str+=s.back(),s.pop_back();
                }
                s.push_back(c);
            }else if(c == '*' || c == '/' || c == '%'){
                while(s.size() && s.back() != '(' && (s.back() == '*' || s.back() == '/' || s.back() == '%')){
                    str+=s.back(),s.pop_back();
                }
                s.push_back(c);
            }else if(c >= '0' && c<= '9'){
                str+=c;
            }
        }
         while(s.size())
            str+=s.back(),s.pop_back();

        stack <int> st;
        string qq;
        for(int i=0;i<str.size();i++){
            qq = str[i];
            if(qq == "+" || qq == "-" || qq == "/" || qq == "*" || qq == "%") {
                signed int sc = st.top(); st.pop();
                signed int ft = st.top(); st.pop();
                if(qq == "+") st.push(ft+sc);
                if(qq == "-") st.push(ft-sc);
                if(qq == "*") st.push(ft*sc);
                if(qq == "/") st.push(ft/sc);
                if(qq == "%") st.push(ft%sc);
            } else {
                st.push(stoi(qq));
            }
        }
        cout<<st.top()<<endl;
    }
    return 0;
}
 
參考解題報告以及網路上的教學,不知道為何會有像主題這樣的錯誤,但是我自己試就能算出來,請問是哪裡出問題?
 
ZeroJudge Forum