duankh @ 2023-11-26 15:19:09
#include<iostream>
using namespace std;
int main(){
int a,b;
char c;
cin>>a,b,c;
switch(c)
{
case '+' :cout<<a<<c<<b<<"="<<a+b; break;
case '-' :cout<<a<<c<<b<<"="<<a-b; break;
case '*' :cout<<a<<c<<b<<"="<<a*b; break;
case '/' :if(b!=0){
cout<<a<<c<<b<<"="<<a+b; break;
}
else{
cout<<"Divided by zero!";break;
}
default : cout<<"Invalid operator!"; break;
}
return 0;
}
by duankh @ 2023-11-26 15:22:22
我知道错误了!!
by Misty7 @ 2023-11-26 15:22:53
@duankh 比如输入 1 + 2 ,不需要输出1 + 2 = 3,直接输出3
by weirdo_shao @ 2023-11-26 15:24:14
Ac代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
char c;
cin>>a>>b>>c;
switch(c)
{
case '+':cout<<a+b;break;
case '-':cout<<a-b;break;
case '*':cout<<a*b;break;
case '/':if (b!=0)
cout<<a/b;
else
cout<<"Divided by zero!";
break;
default:cout<<"Invalid operator!";
}
}
by Rb3f @ 2024-06-22 15:38:44
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
char c;
cin>>a>>b;
cin>>c;
if(b==0&&c=='/'){
cout<<"Divided by zero!"<<endl;
return 0;
}
if(c=='+')
cout<<a+b<<endl;
else if(c=='-')
cout<<a-b<<endl;
else if(c=='*')
cout<<a*b<<endl;
else if(c=='/')
cout<<a/b<<endl;
else
cout<<"Invalid operator!"<<endl;
return 0;
}
by joe_001 @ 2024-08-08 16:06:31
#include<bits/stdc++.h>
using namespace std;
long long x,y,k;
char z;
int main()
{
cin>>x>>y>>z;
if(y==0 && z=='/')
{
cout<<"Divided by zero!"<<endl;
return 0;
}
if(z!='+' && z!='-' && z!='*' && z!='/')
{
cout<<"Invalid operator!"<<endl;
return 0;
}
if(z=='+')
cout<<x+y;
if(z=='-')
cout<<x-y;
if(z=='*')
cout<<x*y;
if(z=='/')
cout<<x/y;
return 0;
}
by luogu_00 @ 2024-08-29 12:18:16
#include<iostream>
using namespace std;
int main(){
int a,b;
char c;
cin>>a,b,c;
switch(c)
{
case '+' :cout<<a<<c<<b<<"="<<a+b; break;
case '-' :cout<<a<<c<<b<<"="<<a-b; break;
case '*' :cout<<a<<c<<b<<"="<<a*b; break;
case '/' :if(b!=0){
cout<<a<<c<<b<<"="<<a+b; break;
}
else{
cout<<"Divided by zero!";break;
}
default : cout<<"Invalid operator!"; break;
}
return 0;
}
这段代码里面有5个缺陷:
cin>>a,b,c
应该是cin>>a>>b>>c
。 case
应该再加一个缩进,所有的case
都是。 if(b!=0)
下面的a+b
应该是a/b
,因为一个double
转int
是自动趋于0
的。if...else
下面的cout
应该再加3个空格。
#include<iostream>
using namespace std;
int main(){
int a,b;
char c;
cin>>a>>b>>c;
switch(c)
{
case '+' :cout<<a+b; break;
case '-' :cout<<a-b; break;
case '*' :cout<<a*b; break;
case '/' :if(b!=0){
cout<<a/b; break;
}
else{
cout<<"Divided by zero!";break;
}
default : cout<<"Invalid operator!"; break;
}
return 0;
}
代码中一个缩进为2个空格,打代码时请用一个Tab代替2个空格,否则可能无法AC
。