#include <iostream>#include <cstring>using namespace std;string splus(string left, string right);string sminus(string left, string right){string ans="";if ((left[0]=='-') && (right[0]=='-')){ans = sminus(right.substr(1, right.length()-1), left.substr(1, left.length()-1));} else if (left[0]=='-'){ans = "-" + splus(left.substr(1, left.length()-1), right);} else if (right[0]=='-'){ans = splus(left, right.substr(1, right.length()-1));} else {int maxLength = (left.length()<right.length())?right.length():left.length();int tmp, carry=0;char cans[maxLength];for (int i=0; i<maxLength; i++){if (i<left.length()){if (i<right.length()) tmp = carry + (left[left.length()-1-i]-'0') - (right[right.length()-1-i]-'0');else tmp = carry + left[left.length()-1-i]-'0';} else tmp = carry - right[right.length()-1-i]-'0';if (tmp<0) {carry = -1;tmp += 10;} else carry = 0;cans[maxLength-1-i] = (char)(tmp+48);}if (carry<0) ans = "-" + sminus(right, left);else {bool zero = true;ans = "";for (int i=0; i<maxLength; i++){if (zero && (cans[i]!='0')) zero = false;if (!zero) ans = ans + cans[i];}if (ans=="") ans = "0";}}return ans;}string splus(string left, string right){string ans="";if ((left[0]=='-') && (right[0]=='-')){ans = "-" + splus(left.substr(1, left.length()-1), right.substr(1, right.length()-1));} else if (left[0]=='-'){ans = sminus(right, left.substr(1, left.length()-1));} else if (right[0]=='-'){ans = sminus(left, right.substr(1, right.length()-1));} else {int maxLength = (left.length()<right.length())?right.length():left.length();int tmp, carry=0;char cans[maxLength];for (int i=0; i<maxLength; i++){tmp = carry;if (i<left.length()) tmp+=left[left.length()-1-i]-'0';if (i<right.length()) tmp+=right[right.length()-1-i]-'0';if (tmp>9) {carry = 1;tmp -= 10;} else carry = 0;cans[maxLength-1-i] = (char)(tmp+48);}ans = "";for (int i=0; i<maxLength; i++) ans = ans + cans[i];if (carry==1){ans = "1" + ans;}}return ans;}string stimes(string left, string right){string ans = "0";int i, j, k;if ((left[0]=='-') && (right[0]=='-')){ans = stimes(left.substr(1, left.length()-1), right.substr(1, right.length()-1));} else if (left[0]=='-'){ans = "-" + stimes(left.substr(1, left.length()-1), right);} else if (right[0]=='-'){ans = "-" + stimes(left, right.substr(1, right.length()-1));} else {int tmp, carry;for (i=0; i<left.length(); i++){char cans[right.length()+i];carry=0;string ans2="";for (j=0; j<right.length(); j++){tmp = (left[left.length()-1-i]-'0') * (right[right.length()-1-j]-'0') + carry;if (tmp>9) {carry = tmp/10;tmp %= 10;} else carry = 0;cans[right.length()-1-j] = (char)(tmp+48);}for (k=0; k<i; k++){cans[right.length()+i-1-k] = '0';}for (k=0; k<right.length()+i; k++) ans2 = ans2 + cans[k];if (carry != 0) ans2 = (char)(carry+48) + ans2;ans = splus(ans, ans2);}}if ((left=="0") || (right=="0")) return "0";else return ans;}string sdivision(string left, string right){string ans="";int i, j, k;if ((left[0]=='-') && (right[0]=='-')){ans = sdivision(left.substr(1, left.length()-1), right.substr(1, right.length()-1));} else if (left[0]=='-'){ans = "-" + sdivision(left.substr(1, left.length()-1), right);} else if (right[0]=='-'){ans = "-" + sdivision(left, right.substr(1, right.length()-1));} else {string leftPart="";for (i=0; i<left.length(); i++){int tmp=0;if (leftPart == "0") leftPart = left[i];else leftPart = leftPart + left[i];while ((sminus(leftPart, stimes(to_string(tmp), right))[0] != '-') && (tmp<10)){tmp++;}if ((ans!="") || (tmp-1!=0)) ans = ans + (char)(tmp+47);leftPart = sminus(leftPart, stimes(to_string(tmp-1), right));}}return ans;}string sremain(string left, string right){string ans="";if ((left[0]=='-') && (right[0]=='-')){ans = "-" + sremain(left.substr(1, left.length()-1), right.substr(1, right.length()-1));} else if (left[0]=='-'){ans = "-" + sremain(left.substr(1, left.length()-1), right);} else if (right[0]=='-'){ans = "-" + sremain(left, right.substr(1, right.length()-1));} else {ans = sminus(left, stimes(right, sdivision(left, right)));}return ans;}string cal(string a){int ans=0, i=0, leftMark=0, rightMark=0, sign=0, mark;bool isNumber = true, newNumber = true;string b, left="0", right="0";// remove bracketswhile(i<a.length()){if (a[i] == '('){int bracket = 1;leftMark=i;i++;while (bracket!=0){if (a[i]==')') bracket--;else if (a[i]=='(') bracket++;i++;}string tmp = cal(a.substr(leftMark+2, i-4-leftMark));a = a.substr(0, leftMark) + tmp + a.substr(i, a.length()-i);i = leftMark + tmp.length() + 1;}i++;}//calculate */%i=0;sign = 3;while(i<a.length()){b = "";mark = i;while ((i<a.length()) && (a[i] != ' ')){b = b + a[i];i++;}if (b[0] == '*'){isNumber = false;newNumber = true;if (sign==0) left = stimes(left, right);else if (sign==1) left = sdivision(left, right);else if (sign==2) left = sremain(left, right);if (sign <3){a = a.substr(0, leftMark) + left + a.substr(mark-1, a.length()-mark+1);i = leftMark + left.length() + 1;}sign = 0;} else if (b[0] == '/'){isNumber = false;newNumber = true;if (sign==0) left = stimes(left, right);else if (sign==1) left = sdivision(left, right);else if (sign==2) left = sremain(left, right);if (sign <3){a = a.substr(0, leftMark) + left + a.substr(mark-1, a.length()-mark+1);i = leftMark + left.length() + 1;}sign = 1;} else if (b[0] == '%'){isNumber = false;newNumber = true;if (sign==0) left = stimes(left, right);else if (sign==1) left = sdivision(left, right);else if (sign==2) left = sremain(left, right);if (sign <3){a = a.substr(0, leftMark) + left + a.substr(mark-1, a.length()-mark+1);i = leftMark + left.length() + 1;}sign = 2;} else if (b[0] == '+'){if (sign==0) left = stimes(left, right);else if (sign==1) left = sdivision(left, right);else if (sign==2) left = sremain(left, right);if (sign <3){a = a.substr(0, leftMark) + left + a.substr(mark-1, a.length()-mark+1);i = leftMark + left.length() + 1;}isNumber = true;newNumber = true;sign = 3;} else if ((b[0] == '-') && (b.length() == 1)){if (sign==0) left = stimes(left, right);else if (sign==1) left = sdivision(left, right);else if (sign==2) left = sremain(left, right);if (sign <3){a = a.substr(0, leftMark) + left + a.substr(mark-1, a.length()-mark+1);i = leftMark + left.length() + 1;}isNumber = true;newNumber = true;sign = 3;} else {if (isNumber) {left = b;if (newNumber) {leftMark = mark;newNumber = false;}}else {right = b;if (newNumber) {rightMark = mark;newNumber = false;}}}i++;}if (sign==0) left = stimes(left, right);else if (sign==1) left = sdivision(left, right);else if (sign==2) left = sremain(left, right);if (sign <3) a = a.substr(0, leftMark) + left;//calculate +-i=0;left="0";right="0";isNumber = true;while(i<a.length()){b = "";while ((i<a.length()) && (a[i] != ' ')){b = b + a[i];i++;}if (b[0] == '+'){isNumber = false;if (sign==0) left = splus(left, right);else left = sminus(left, right);sign = 0;} else if ((b[0] == '-') && (b.length() == 1)){isNumber = false;if (sign==0) left = splus(left, right);else left = sminus(left, right);sign = 1;} else {if (isNumber) left = b;else right = b;}i++;}if (sign==0) left = splus(left, right);else left = sminus(left, right);return left;}int main(){string a;while (getline(cin, a)){cout << cal(a) << endl;}return 0;}
a = a.substr(0, leftMark) + left + a.substr(mark-1, a.length()-mark+1);
你這一行會造成運算子後面直接接數字,沒有空格(另外其實不用寫的這麼辛苦,加減乘除直接算就好了)
非常感謝! 終於發現錯的地方 ><"
p.s. 因為說運算元為 0 ~ 2^31 -1 的整數, 看的時候我理解成每一個運算元.... 以為會出現 (2^31 -1) * (2^31 -1) + (2^31 -1) 之類........