RE了,求大佬解答!!

B2052 简单计算器

python18 @ 2023-12-06 21:10:04

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,k;
    char g;
    cin>>j>>k;
    cin>>g;
    if(k==0&&g=='/')
    {
        printf("Divided by zero!\n");
    }
    if(g!='+'&&g!='-'&&g!='*'&&g!='/')
    {
        cout<<"Invalid operator!"<<endl;    
    }
    if(g=='+')
    {
        printf("%d\n",j+k);
    }
    else if(g=='-')
    {
        printf("%d\n",j-k);
    }
    else if(g=='*')
    {
        printf("%d\n",j*k);
    }
    else if(g=='/')
    {
        printf("%d",j/k);
    }

    return 0;
} 

RE了,求大佬解答,球球了


by honglixian1118 @ 2023-12-06 21:21:19

@python18 我随便改了一下就对了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int j,k;
    char g;
    cin>>j>>k;
    cin>>g;
    if(k==0&&g=='/')
    {
        printf("Divided by zero!\n");
    }
    else if(g!='+'&&g!='-'&&g!='*'&&g!='/')
    {
        cout<<"Invalid operator!"<<endl;    
    }
    else if(g=='+')
    {
        printf("%d\n",j+k);
    }
    else if(g=='-')
    {
        printf("%d\n",j-k);
    }
    else if(g=='*')
    {
        printf("%d\n",j*k);
    }
    else if(g=='/')
    {
        printf("%d\n",j/k);
    }
    return 0;
} 

by honglixian1118 @ 2023-12-06 21:25:33

求关QWQ


by I_will_AKIOI @ 2023-12-06 21:26:43

if(k==0&&g=='/')else if(g=='/') 重复操作了。

你特判了操作一,但是没有结束,继续执行了操作二,每种操作前加 else 或操作结束后 return 0;


by liruizhou_lihui @ 2023-12-06 21:27:03

@python18

你这样写容易混,用switch比较清晰明了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    char n;
    cin>>a>>b>>n;
    switch(n)
    {
        case '+':cout<<a+b;break;
        case '-':cout<<a-b;break;
        case '*':cout<<a*b;break;
        case '/':if(b==0)cout<<"Divided by zero!";else cout<<a/b;break;
        default:
        {
            cout<<"Invalid operator!";break;
        }
    }
}

by python18 @ 2023-12-07 20:54:43

@liruizhou123 我不会

呜呜呜


by python18 @ 2023-12-07 20:55:49

@honglixian1118 谢谢,已经过了

阿巴阿巴


by Carlos20130721 @ 2024-07-28 16:40:23

include<bits/stdc++.h>```cpp



using namespace std;
int main()
{
    int a,b,ans;
    char c;
    cin>>a>>b>>c;
    if(c=='+')
    {
        ans=a+b;
    }
    else if(c=='-')
    {
        ans=a-b;
    }
    else if(c=='*')
    {
        ans=a*b;
    }
    else if(c=='/')
    {
        ans=a/b;
        if(a/b==0)
        {
            cout<<"Divided by zero!"<<endl; 
        }
    }
    else
    {
        cout<<"Invalid operator!"<<endl;
    }
    cout<<ans<<endl; 
    return 0;
}

by Carlos20130721 @ 2024-07-28 16:41:04

include<bits/stdc++.h>

using namespace std; int main() { int a,b,ans; char c; cin>>a>>b>>c; if(c=='+') { ans=a+b; } else if(c=='-') { ans=a-b; } else if(c=='') { ans=ab; } else if(c=='/') { ans=a/b; if(a/b==0) { cout<<"Divided by zero!"<<endl; } } else { cout<<"Invalid operator!"<<endl; } cout<<ans<<endl; return 0; }


by Carlos20130721 @ 2024-07-28 16:41:38

include<bits/stdc++.h>

using namespace std; int main() { int a,b,ans; char c; cin>>a>>b>>c; if(c=='+') { ans=a+b; } else if(c=='-') { ans=a-b; } else if(c=='') { ans=ab; } else if(c=='/') { ans=a/b; if(a/b==0) { cout<<"Divided by zero!"<<endl; } } else { cout<<"Invalid operator!"<<endl; } cout<<ans<<endl; return 0; }```cpp


by Carlos20130721 @ 2024-07-28 16:42:47

include<bits/stdc++.h>

using namespace std; int main() { int a,b,ans; char c; cin>>a>>b>>c; if(c=='+') { ans=a+b; } else if(c=='-') { ans=a-b; } else if(c=='') { ans=ab; } else if(c=='/') { ans=a/b; if(a/b==0) { cout<<"Divided by zero!"<<endl; } } else { cout<<"Invalid operator!"<<endl; } cout<<ans<<endl; return 0; }


| 下一页