C语言,样例对,评判0,求助

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

Nitroglycerine @ 2024-11-18 20:00:19

#include <stdio.h>
#include <string.h>

int main(){
    int q;
    scanf("%d",&q);
    int a,b,c,d,e,f=-1;
    char s[1000];
    char s1[1000];
    char s2[1000];
    char s3[1000];
    char s4[1000];
    char s5[1000];
    char s6[1000];
    scanf("%s",s);
    int n=strlen(s);
    do{
        int x;
        scanf("%d",&x);
        switch(x){
            case 1:
                scanf("%s",s1);
                int m=strlen(s1);
                n = strlen(s);
                for(int i=n;i<n+m;i++){
                    s[i] = s1[i-n];
                }
                s[n+m]=0;
                printf("%s\n",s);
                q--;
                break;
            case 2:
                scanf("%d %d",&a,&b);
                for(int i=a;i<a+b;i++){
                    s2[i-a] = s[i];
                }
                s2[b] = 0;
                strcpy(s,s2);
                printf("%s\n",s);
                q--;
                break;
            case 3:
                scanf("%d",&c);
                scanf("%s",s3);
                strcpy(s4,s);
                d = strlen(s3);
                n = strlen(s);
                for(int i=c;i<=n;i++){
                    s[i+d] = s4[i];
                }
                for(int i=c;i<c+d;i++){
                    s[i] = s3[i-c];
                }
                printf("%s\n",s);
                q--;
                break;
            case 4:
                scanf("%s",s5);
                int e = strlen(s5);
                n =strlen(s);
                for(int i=0;i<n-e;i++){
                    for(int j=i;j<i+e;j++){
                        s6[j-i] = s[j];
                    }
                    s6[e] = 0;
                    if(!strcmp(s6,s5)){
                        f = i;
                        break;
                    }
                }
                printf("%d",f);
                q--;
                break;
        }
    }while(q>0);
    return 0;
}

by litangzheng @ 2024-11-18 20:06:00

@Nitroglycerine
“要是我当时用的是c++,有string的话,那该多好啊。”


by ztse9172 @ 2024-11-29 19:07:12

#include <stdio.h>
#include <string.h>
int main() {
    int q;
    scanf("%d", &q);
    int a = 0, b = 0, c = 0, d = 0, e = 0, f = -1;
    char s[1000];
    char s1[1000];
    char s2[1000];
    char s3[1000];
    char s4[1000];
    char s5[1000];
    char s6[1000];

    scanf("%s", s);
    int n = strlen(s);

    do {
        int x;
        scanf("%d", &x);

        switch (x) {
        case 1: {
            scanf("%s", s1);
            int m = strlen(s1);

            // 检查是否会数组越界
            if (n + m <= 1000) {
                for (int i = n; i < n + m; i++) {
                    s[i] = s1[i - n];
                }
                s[n + m] = 0;
                printf("%s\n", s);
            } else {
                printf("数组越界,无法追加字符串\n");
            }
            q--;
            break;
        }
        case 2: {
            scanf("%d %d", &a, &b);

            // 检查索引是否合法
            if (a >= 0 && a + b <= n && a + b <= 1000) {
                for (int i = a; i < a + b; i++) {
                    s2[i - a] = s[i];
                }
                s2[b] = 0;
                strcpy(s, s2);
                printf("%s\n", s);
            } else {
                printf("索引不合法,无法提取子串\n");
            }
            q--;
            break;
        }
        // 其他case语句也按照类似思路修改

        }
    } while (q > 0);

    return 0;
}

|