我的程式碼不管是自己執行還是題目的「測試執行」都是沒有問題的,但是最後出來的結果都是WA,Help!!!
#include <bits/stdc++.h>
using namespace std;
int main() {
string q;
stringstream ss;
while(getline(cin, q)){
vector<string> a;
int c1 = 0 , c2 = 0;
stringstream sss(q);
string token;
while(sss>>token) {
a.push_back(token);
}
for (int i = 0; i < a.size(); i ++) {
if(a[i]=="("){
c1 = i;
}
else if(a[i]==")"){
c2 = i;
}
}
////////////////////
if(c1!=0 && c2!=0){
int l = c2 - c1 - 1;
vector<string> b;
for(int i = 0 ; i<l ; i++){
a.erase(a.begin() + c1);
string s = a[c1];
b.push_back(s);
}
a.erase(a.begin() + c1);
////
for (int i = 0; i < b.size(); i++) {
if(b[i] == "*"){
int x,y,z;
ss.str("");
ss.clear();
ss<<b[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<b[i+1];
ss>>y;
z = x*y;
b[i-1] = to_string(z);
b.erase(b.begin() + i);
b.erase(b.begin() + i);
i--;
}
else if(b[i] == "/"){
int x,y,z;
ss.str("");
ss.clear();
ss<<b[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<b[i+1];
ss>>y;
z = x/y;
b[i-1] = to_string(z);
b.erase(b.begin() + i);
b.erase(b.begin() + i);
i--;
}
else if(b[i] == "%"){
int x,y,z;
ss.str("");
ss.clear();
ss<<b[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<b[i+1];
ss>>y;
z = x%y;
b[i-1] = to_string(z);
b.erase(b.begin() + i);
b.erase(b.begin() + i);
i--;
}
}
///+&-
for (int i = 0; i < b.size(); i++) {
if(b[i] == "+"){
int x,y,z;
ss.str("");
ss.clear();
ss<<b[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<b[i+1];
ss>>y;
z = x+y;
b[i-1] = to_string(z);
b.erase(b.begin() + i);
b.erase(b.begin() + i);
i--;
}
else if(b[i] == "-"){
int x,y,z;
ss.str("");
ss.clear();
ss<<b[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<b[i+1];
ss>>y;
z = x-y;
b[i-1] = to_string(z);
b.erase(b.begin() + i);
b.erase(b.begin() + i);
i--;
}
}
a[c1] = b[0];
}
for (int i = 0; i < a.size(); i++) {
if(a[i] == "*"){
int x,y,z;
ss.str("");
ss.clear();
ss<<a[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<a[i+1];
ss>>y;
z = x*y;
a[i-1] = to_string(z);
a.erase(a.begin() + i);
a.erase(a.begin() + i);
i--;
}
else if(a[i] == "/"){
int x,y,z;
ss.str("");
ss.clear();
ss<<a[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<a[i+1];
ss>>y;
z = x/y;
a[i-1] = to_string(z);
a.erase(a.begin() + i);
a.erase(a.begin() + i);
i--;
}
else if(a[i] == "%"){
int x,y,z;
ss.str("");
ss.clear();
ss<<a[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<a[i+1];
ss>>y;
z = x%y;
a[i-1] = to_string(z);
a.erase(a.begin() + i);
a.erase(a.begin() + i);
i--;
}
}
///+&-
for (int i = 0; i < a.size(); i++) {
if(a[i] == "+"){
int x,y,z;
ss.str("");
ss.clear();
ss<<a[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<a[i+1];
ss>>y;
z = x+y;
a[i-1] = to_string(z);
a.erase(a.begin() + i);
a.erase(a.begin() + i);
i--;
}
else if(a[i] == "-"){
int x,y,z;
ss.str("");
ss.clear();
ss<<a[i-1];
ss>>x;
ss.str("");
ss.clear();
ss<<a[i+1];
ss>>y;
z = x-y;
a[i-1] = to_string(z);
a.erase(a.begin() + i);
a.erase(a.begin() + i);
i--;
}
}
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout<<endl;
}
return 0;
}