CE求助

P1957 口算练习题

NKJJ @ 2021-12-08 16:31:14

gets_s()函数不能用吗?一直错误,我在VS上为啥能用

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int f1(char a[][50],int n) {
    int st, count = 0;
    if (a[n][0] == 'a' || a[n][0] == 'b' || a[n][0] == 'c')st = 2;
    else st = 0;
    for (unsigned int i = st; i < strlen(a[n]); i++) {
        if (a[n][i] != ' ')count++;
        else break;
    }
    return count;
}
int f2(char a[][50], int n) {
    int st, count = 0, c2 = 0;
    if (a[n][0] == 'a' || a[n][0] == 'b' || a[n][0] == 'c')st = 2;
    else st = 0;
    for (unsigned int i = st; i < strlen(a[n]); i++) {
        if (a[n][i] == ' ')c2++;
        if (c2)count++;
        else continue;
    }
    return --count;
}
int f3(int n) {
    int count = 1;
    if (n < 0)n = -n;
    while (n >= 10) {
        n /= 10;
        count++;
    }
    return count;
}
int st(char a[][50], int n) {
    if (a[n][0] == 'a' || a[n][0] == 'b' || a[n][0] == 'c')return 2;
    else return 0;
}
int num[50][2];
int main() {
    int n;
    cin >> n;
    char choose;
    char str[50][50];
    for (int i = 1; i < n+1; i++) {
        gets_s(str[i]);
    }
    for (int i = 1; i < n+1; i++) {
        int co = f1(str,i);
        for (int j = st(str, i); j < st(str, i) + f1(str, i); j++) {
            num[i][0] += (int(str[i][j])-int('0')) * pow(10, co-1);
            co--;
        }
        co = f2(str,i);
        for (unsigned int j = st(str, i) + f1(str, i) + 1; j < strlen(str[i]); j++) {
            num[i][1] += (int(str[i][j])-int('0')) * pow(10, co-1);
            co--;
        }
    }
    choose = str[1][0];
    for (int i = 1; i < n+1; i++) {
        if (str[i][0] >= 97 && str[i][0] <= 122)choose = str[i][0];
        switch (choose) {
        case 'a':cout << num[i][0] << "+" << num[i][1] << "=" << num[i][0] + num[i][1] << endl;
            cout << f1(str, i) + f2(str, i) + f3(num[i][0] + num[i][1]) + 2 << endl;
            break;
        case 'b':cout << num[i][0] << "-" << num[i][1] << "=" << num[i][0] - num[i][1] << endl;
            cout << f1(str, i) + f2(str, i) + f3(num[i][0] - num[i][1]) + 2 << endl;
            break;
        case 'c':cout << num[i][0] << "*" << num[i][1] << "=" << num[i][0] * num[i][1] << endl;
            cout << f1(str, i) + f2(str, i) + f3(num[i][0] * num[i][1]) + 2 << endl;
            break;
        }
    }
}

by ud2_ @ 2021-12-08 16:42:17

gets_s 是标准规定可以不实现的函数。建议改成 fgets 或者 getline


by GKxx @ 2021-12-08 17:04:53

@NKJJ gets由于其内存安全性差,自C11起它已经被移除。在C++中,自C++11起它被弃用,自C++14起被移除。

gets_s是Visual Studio这套编译器专门为函数gets实现的替代品,但它不属于标准C++,在其它编译器上没有。


by NKJJ @ 2021-12-08 17:16:11

@GKxx 哦哦,谢谢,下次注意


by NKJJ @ 2021-12-08 17:22:46

@ud2_ 不会用...


by ud2_ @ 2021-12-08 17:29:05


|