6 条题解
-
7
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'*': cout<<x*y; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }
-
1
-
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'*': cout<<x*y; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }
-
0
rc245zhangjiayi1 LV 6 @ 1 天前
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }
-
0
rc247chenbosheng1 LV 8 @ 3 天前
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'*': cout<<x*y; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }
-
-
1
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'*': cout<<x*y; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }
-
0
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c; char d; cin>>a>>b>>d; if(d!='+'&&d!='-'&&d!=''&&d!='/') { cout<<"Invalid operator!";} else { if(d=='+') { c=a+b;} else if(d=='-') { c=a-b; } else if(d=='') { c=a*b; } else if(d=='/') { if(b==0) { cout<<"Divided by zero!"; exit(0); } else { c=a/b; } } else {
} } cout<<c; return 0;
}
-
0
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }
-
-1
叠甲:c风格处理速度比c++风格处理速度快,但两者皆可通过c++测试,请谅解!!!
首先,我们先排除计算器的常见问题:
#include<bits/stdc++.h> using namespace std; int main(){ int a,b; char c; cin>>a>>b>>c; if(c!='+'&&c!='-'&&c!='*'&&c!='/') { cout<<"Invalid operator!"; return 0;//return 0指系统返回值,不再执行下个语句 } if(c=='/'&&b==0) { cout<<"Divided by zero!"; return 0; }
这样,我们就可以排除符号写错、除数为0的问题。
符号写错是+、-、*、/以外的符号写入,从而导致的错误;
除数为0是一个DDDD(懂的都懂)的问题,不再多提。
其次,再加入算法,运行
if(c=='+') cout<<a+b; if(c=='-') cout<<a-b; if(c=='*') cout<<a*b; if(c=='/') cout<<a/b; }
总结:此题难点在于一些错误输出的应对方法,但对算法没有过多要求,总体来说还是比较简单的。
- 1
信息
- ID
- 1222
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- (无)
- 递交数
- 169
- 已通过
- 112
- 上传者