winner999 @ 2024-11-20 19:07:47
#include <bits/stdc++.h>
using namespace std;
int main () {
int a,b;
char c;
cin >> a >> b >> c;
if (c == '+') {
cout << a+b;
}else if (c == '-') {
cout << a - b;
}else if (c == '*') {
cout << a * b;
}else if (c == '/') {
cout << a / b;
}else if ((a == 0 || b == 0) && c == '/' ){
cout << "Divided by zero!";
}else {
cout << "Invalid operator";
}
return 0;
}
by Hero_Broom @ 2024-11-20 19:13:21
@winner999
(求关)
#include <bits/stdc++.h>
using namespace std;
int main () {
int a,b;
char c;
cin >> a >> b >> c;
if (c == '+') {
cout << a+b;
}else if (c == '-') {
cout << a - b;
}else if (c == '*') {
cout << a * b;
}else if (c == '/') {
if ((a == 0 || b == 0) && c == '/' ){
cout << "Divided by zero!";
}
else cout << a / b;
}
else {
cout << "Invalid operator";
}
return 0;
}
by Hero_Broom @ 2024-11-20 19:15:19
@winner999 如果为除法且b为0时会直接进到你 cout<<a/b
的地方,b=0
就re了,他不会进到你 Devided by 0
里面
by XuYueming @ 2024-11-20 19:17:09
@Hero_Broom 请不要误导别人,仅需要特判除数为零
by XuYueming @ 2024-11-20 19:17:42
@Hero_Broom 抱歉,原来他原先代码就是这样写的
by Hero_Broom @ 2024-11-20 19:18:27
@XuYueming 所以不是才说把判断除数为零的情况放到 c=='/'
里
by XuYueming @ 2024-11-20 19:34:10
@Hero_Broom 我是指,不能特判被除数为零的情况