求助 运行对照一看全对 但都是WA

P5734 【深基6.例6】文字处理软件

QQ1809773200 @ 2024-12-18 13:13:50

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
void insert(char* str,char *sp1) {
    char str1[100];
    fgets(str1, 100, stdin);
    str1[strlen(str1) - 1] = '\0';
    strcat(str,str1);
    strcpy(sp1, str);
}
void sub(char* str,char* sp2) {
    int a, b;
    scanf_s("%d %d", &a, &b);
    getchar();
    char new_str[100] = {};
    for (int i = 0; i < b; i++) {
        *(new_str + i) = *(str + a + i);
    }
    strcpy(str, new_str);
    strcpy(sp2, str);
}
void insert1(char* str,char* sp3) {
    int a;
    scanf_s("%d", &a);
    getchar();
    char str2[100] = {};
    fgets(str2, 100, stdin);
    str2[strlen(str2) - 1] = '\0';
    char str3[100] = {};
    strncpy(str3, str, a);
    str3[a] = '\0';
    strcat(str3, str2);
    strcat(str3, str + a);
    strcpy(str, str3);
    strcpy(sp3, str);
}
void finding(char* str,int*sp4) {
    char son[100];
    scanf("%s", son);
    char* p;
    static int cnt = 0;
    p = strstr(str, son);
    if (p  == NULL) {
        *(sp4+cnt) = -1;

    }
    else {
        *(sp4+cnt) = p - str;
    }
    cnt++;
}
int main() {
    int n;
    scanf_s("%d", &n);
    getchar();
    char str[100];
    fgets(str, 100, stdin);
    str[strlen(str)-1] = '\0';
    char sp1[100] = {}, sp2[100] = {}, sp3[100] = {};
    int sp4[100] = {};

    for (int i = 0; i < n; i++) {
        char op;
        scanf_s(" %c", &op);
        getchar();
        switch (op) {
        case '1':insert(str,sp1); break;
        case '2':sub(str,sp2); break;
        case '3':insert1(str,sp3); break;
        case '4':finding(str,sp4); break;
        default: return 0;
        }
    }
    printf("%s\n", sp1);
    printf("%s\n", sp2);
    printf("%s\n", sp3);
    int i = 0;
    while (sp4[i] != 0) {
        printf("%d\n", sp4[i]);
        i++;
    }
    return 0;
}

by BVV_d_ @ 2024-12-18 13:47:27

实际上每种操作会执行多次

即输出共 q


by BVV_d_ @ 2024-12-18 13:48:06

题都没看就写。。。


|