本地测试通过,提交全部WA。求大佬求解!!!

P1957 口算练习题

lucas_777 @ 2023-01-27 19:34:24

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main(){

    int i, n1, n2;
    cin >> i;
    char op;
    for(int x = 0; x <= i; ++x) {
        string s;
        getline(cin, s);

        if(s.size() != 0) {
            char tmp = s.front();
            if(tmp == 'a' || tmp == 'b' || tmp == 'c') {
                op = tmp;
                s = s.substr(2);
            }
            sscanf(s.data(), "%d%d", &n1, &n2);
            string ops;
            int result;
            switch(op) {
                case 'a' :
                    ops = "+";
                    result = n1 + n2;
                    break;

                case 'b' :
                    ops = "-";
                    result = n1 - n2;
                    break;

                case 'c' :
                    ops = "*";
                    result = n1 * n2;
                    break;
            }
            string xx = to_string(n1) + ops + to_string(n2) + "=" + to_string(result);

            cout << xx << endl;
            cout << xx.length() << endl;
        }
    }
    return 0;   
}

by qianzhongye @ 2023-02-18 20:47:19

不能用getline。

不要问我咋知道的


by AlexFad @ 2023-04-28 21:08:11

可以用

scanf("%*[\r\n]%[^\n]",s);

by lucas_777 @ 2023-06-15 16:29:28

ok 谢谢


|