求助,样例都过了,一交全WA!

P1957 口算练习题

biophitma_wby @ 2022-08-01 19:33:35

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
char s[10];
int main(){
    int qwe;
    int n;cin>>n;
    cin.get();
    while(n--){

        cin.getline(s,10);
        int len=strlen(s);
        bool d=false;
        int a=0,b=0;
        int sum=0;
        int num=0;
        if(s[0]=='a'){
            for(int i=2;i<len;i++){
                if(s[i]==' '){
                    d=true;
                }
                if(d==false&&s[i]!=' '){
                    a=a*10+int(s[i]-'0');
                }
                if(d==true&&s[i]!=' '){
                    b=b*10+int(s[i]-'0');

                }
                if(s[i]!=' ')sum++;
            }
            int c=a+b;
            int k=c;
            cout<<a<<"+"<<b<<"="<<c<<endl;
            while(c!=0){
                num++;
                c/=10;
            }
            if(k<0){
                cout<<sum+num+3<<endl;
            }
            else cout<<sum+num+2<<endl;
            qwe=1;
        }else if(s[0]=='b'){
            for(int i=2;i<len;i++){
                if(s[i]==' '){
                    d=true;
                }
                if(d==false&&s[i]!=' '){
                    a=a*10+int(s[i]-'0');
                }
                if(d==true&&s[i]!=' '){
                    b=b*10+int(s[i]-'0');

                }
                if(s[i]!=' ')sum++;
            }
            int c=a-b;
            int k=c;
            cout<<a<<"-"<<b<<"="<<c<<endl;
            while(c!=0){
                num++;
                c/=10;
            }
            if(k<0){
                cout<<sum+num+3<<endl;
            }
            else cout<<sum+num+2<<endl;
            qwe=2;
        }else if(s[0]=='c'){
            for(int i=2;i<len;i++){
                if(s[i]==' '){
                    d=true;
                }
                if(d==false&&s[i]!=' '){
                    a=a*10+int(s[i]-'0');
                }
                if(d==true&&s[i]!=' '){
                    b=b*10+int(s[i]-'0');

                }
                if(s[i]!=' ')sum++;
            }
            int c=a*b;
            int k=c;
            cout<<a<<"*"<<b<<"="<<c<<endl;
            while(c!=0){
                num++;
                c/=10;
            }
            if(k<0){
                cout<<sum+num+3<<endl;
            }
            else cout<<sum+num+2<<endl;
            qwe=3;
        }else{
            if(qwe==1){
                for(int i=0;i<len;i++){
                if(s[i]==' '){
                    d=true;
                }
                if(d==false&&s[i]!=' '){
                    a=a*10+int(s[i]-'0');
                }
                if(d==true&&s[i]!=' '){
                    b=b*10+int(s[i]-'0');

                }
                if(s[i]!=' ')sum++;
            }
            int c=a+b;
            int k=c;
            cout<<a<<"+"<<b<<"="<<c<<endl;
            while(c!=0){
                num++;
                c/=10;
            }
            if(k<0){
                cout<<sum+num+3<<endl;
            }
            else cout<<sum+num+2<<endl;
            qwe=1;
            }
            if(qwe==2){
                for(int i=0;i<len;i++){
                if(s[i]==' '){
                    d=true;
                }
                if(d==false&&s[i]!=' '){
                    a=a*10+int(s[i]-'0');
                }
                if(d==true&&s[i]!=' '){
                    b=b*10+int(s[i]-'0');

                }
                if(s[i]!=' ')sum++;
            }
            int c=a-b;
            int k=c;
            cout<<a<<"-"<<b<<"="<<c<<endl;
            while(c!=0){
                num++;
                c/=10;
            }
            if(k<0){
                cout<<sum+num+3<<endl;
            }
            else cout<<sum+num+2<<endl;
            qwe=2;
            }
            if(qwe==3){
                for(int i=0;i<len;i++){
                if(s[i]==' '){
                    d=true;
                }
                if(d==false&&s[i]!=' '){
                    a=a*10+int(s[i]-'0');
                }
                if(d==true&&s[i]!=' '){
                    b=b*10+int(s[i]-'0');

                }
                if(s[i]!=' ')sum++;
            }
            int c=a*b;
            int k=c;
            cout<<a<<"*"<<b<<"="<<c<<endl;
            while(c!=0){
                num++;
                c/=10;
            }
            if(k<0){
                cout<<sum+num+3<<endl;
            }
            else cout<<sum+num+2<<endl;
            qwe=3;
            }
        }
    }
    return 0;
}

by fzy1026 @ 2022-08-01 19:41:16

cin.getline(s,10);

这一句错了


by fzy1026 @ 2022-08-01 19:42:00

这样getline会把输入的字符限制在10个以内 hack:

1

a 100000 100000


by biophitma_wby @ 2022-08-01 20:11:13

@fzy1026 谢谢


by biophitma_wby @ 2022-08-01 20:12:36

@fzy1026 还是WA了


|