#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string n, ans;
long long int pf, pl, n1, n2, num1, num2, sum, opr, i;
stringstream m;
while(getline(cin, n)){
while(1){
sum = 0, pf = 0, pl = n.size() - 1, opr = 0;
//find out the parentheses
for(i = 0; i < n.size(); i++){
if(n[i] == '('){
pf = i;
}
if(n[i] == ')'){
pl = i;
break;
}
}
//find out the operator
for(i = pf + 2; i < pl; i++){
//find out operator location
if(n[i] == '+' and opr == 0 or n[i] == '-' and opr == 0){
if(n[i] == '-' and n[i + 1] != ' ') continue;
opr = i;
}
else if(n[i] == '*' or n [i] == '/' or n [i] == '%'){
opr = i;
break;
}
}
if(opr == 0){
if(pf == 0 and pl == n.size() - 1) break;
else{
n = n.replace(pf, 2, "");
n = n.replace(pl - 2, 2, "");
continue;
}
}
//find out the first number
for(i = opr - 2; i >= pf; i--){
if(n[i] == ' ' or i == pf){
if(n[i] == ' ') i++;
n1 = i;
for(int j = i; n[j] != ' '; j++){
m << n[j];
}
m >> num1;
m.clear();
m.str("");
break;
}
}
//find out the second number
for(i = opr + 2; n[i] != ' ' and i <= pl; i++){
m << n[i];
}
if(n[i] == ' ') i--;
n2 = i;
m >> num2;
m.clear();
m.str("");
//calculate
switch(n[opr]){
case '+':
sum = num1 + num2;
break;
case '-':
sum = num1 - num2;
break;
case '*':
sum = num1 * num2;
break;
case '/':
sum = num1 / num2;
break;
case '%':
sum = num1 % num2;
break;
}
//replace
m << sum;
m >> ans;
m.clear();
m.str("");
n = n.replace(n1, n2 - n1 + 1, ans);
}
cout << n << '\n';
}
}
我是透過定位及replace(),重複幾次,慢慢算出答案(抱歉可讀性很低,如果看不懂我再補充)
請問哪裡錯了嗎?
還是我少考慮了哪些情況?
#include
#include
#include
using namespace std;int main(){
string n, ans;
long long int pf, pl, n1, n2, num1, num2, sum, opr, i;
stringstream m;
while(getline(cin, n)){
while(1){
sum = 0, pf = 0, pl = n.size() - 1, opr = 0;
//find out the parentheses
for(i = 0; i < n.size(); i++){
if(n[i] == '('){
pf = i;
}
if(n[i] == ')'){
pl = i;
break;
}
}
//find out the operator
for(i = pf + 2; i < pl; i++){
//find out operator location
if(n[i] == '+' and opr == 0 or n[i] == '-' and opr == 0){
if(n[i] == '-' and n[i + 1] != ' ') continue;
opr = i;
}
else if(n[i] == '*' or n [i] == '/' or n [i] == '%'){
opr = i;
break;
}
}
if(opr == 0){
if(pf == 0 and pl == n.size() - 1) break;
else{
n = n.replace(pf, 2, "");
n = n.replace(pl - 2, 2, "");
continue;
}
}
//find out the first number
for(i = opr - 2; i >= pf; i--){
if(n[i] == ' ' or i == pf){
if(n[i] == ' ') i++;
n1 = i;
for(int j = i; n[j] != ' '; j++){
m << n[j];
}
m >> num1;
m.clear();
m.str("");
break;
}
}
//find out the second number
for(i = opr + 2; n[i] != ' ' and i <= pl; i++){
m << n[i];
}
if(n[i] == ' ') i--;
n2 = i;
m >> num2;
m.clear();
m.str("");
//calculate
switch(n[opr]){
case '+':
sum = num1 + num2;
break;
case '-':
sum = num1 - num2;
break;
case '*':
sum = num1 * num2;
break;
case '/':
sum = num1 / num2;
break;
case '%':
sum = num1 % num2;
break;
}//replace
m << sum;
m >> ans;
m.clear();
m.str("");
n = n.replace(n1, n2 - n1 + 1, ans);
}
cout << n << '\n';
}
}我是透過定位及replace(),重複幾次,慢慢算出答案(抱歉可讀性很低,如果看不懂我再補充)
請問哪裡錯了嗎?
還是我少考慮了哪些情況?
括號有可能出現在第一和最後一個字元,例如輸入是( 3 + 4 ),要輸出7而不是( 7 )
#include
#include
#include
using namespace std;int main(){
string n, ans;
long long int pf, pl, n1, n2, num1, num2, sum, opr, i;
stringstream m;
while(getline(cin, n)){
while(1){
sum = 0, pf = 0, pl = n.size() - 1, opr = 0;
//find out the parentheses
for(i = 0; i < n.size(); i++){
if(n[i] == '('){
pf = i;
}
if(n[i] == ')'){
pl = i;
break;
}
}
//find out the operator
for(i = pf + 2; i < pl; i++){
//find out operator location
if(n[i] == '+' and opr == 0 or n[i] == '-' and opr == 0){
if(n[i] == '-' and n[i + 1] != ' ') continue;
opr = i;
}
else if(n[i] == '*' or n [i] == '/' or n [i] == '%'){
opr = i;
break;
}
}
if(opr == 0){
if(pf == 0 and pl == n.size() - 1) break;
else{
n = n.replace(pf, 2, "");
n = n.replace(pl - 2, 2, "");
continue;
}
}
//find out the first number
for(i = opr - 2; i >= pf; i--){
if(n[i] == ' ' or i == pf){
if(n[i] == ' ') i++;
n1 = i;
for(int j = i; n[j] != ' '; j++){
m << n[j];
}
m >> num1;
m.clear();
m.str("");
break;
}
}
//find out the second number
for(i = opr + 2; n[i] != ' ' and i <= pl; i++){
m << n[i];
}
if(n[i] == ' ') i--;
n2 = i;
m >> num2;
m.clear();
m.str("");
//calculate
switch(n[opr]){
case '+':
sum = num1 + num2;
break;
case '-':
sum = num1 - num2;
break;
case '*':
sum = num1 * num2;
break;
case '/':
sum = num1 / num2;
break;
case '%':
sum = num1 % num2;
break;
}//replace
m << sum;
m >> ans;
m.clear();
m.str("");
n = n.replace(n1, n2 - n1 + 1, ans);
}
cout << n << '\n';
}
}我是透過定位及replace(),重複幾次,慢慢算出答案(抱歉可讀性很低,如果看不懂我再補充)
請問哪裡錯了嗎?
還是我少考慮了哪些情況?
括號有可能出現在第一和最後一個字元,例如輸入是( 3 + 4 ),要輸出7而不是( 7 )
對欸,感謝你,多考慮這個狀況後就過了