请问,本题为什么为WA

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

leyouyuan @ 2024-08-31 23:47:46

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>

char command [110];
char Data [110];

void add(char *cmd,char *data)
{
    while(*data != '\0')
    {
        data ++;
    }
    strcpy(data,cmd);
}

void str_del(int n,int k,char *data)
{
    char *tmp_data = new char [110];
    strcpy(tmp_data,data+n);
    tmp_data [k] = '\0';
    strcpy(data,tmp_data);
    delete tmp_data;
}

void del(char *rest_command,char *data)
{
    int n,k;
    sscanf(rest_command,"%d %d",&n,&k);
    str_del(n,k,data);
}

void str_insert(int a,char *cmd,char *data)
{
    strcpy(data+a,cmd);
}

void insert(char *rest_command,char *data)
{
    int a;
    char *tmp_data = new char [110];
    sscanf(rest_command,"%d %s",&a,tmp_data);
    str_insert(a,tmp_data,data);
    delete tmp_data;
}

int seek(char *rest_command,char *data)
{
    char *p = strstr(data,rest_command);
    if(p==NULL)
    {
        return -1;
    }
    return p - data;
}

void process(int mode,char *rest_command,char *data)
{
    switch(mode)
    {
        case 1:
        add(rest_command,data);
        break;
        case 2:
        del(rest_command,data);
        case 3:
        insert(rest_command,data);
        case 4:
        std::cout<<seek(rest_command,data)<<std::endl;
        break;
        default:
        std::cout<<data<<std::endl;
    }
}

int main(int argc,char **argv)
{
    int q;
    scanf("%d\n",&q);
    int mode;
    scanf("%s\n",Data);
    for(int i = 1;i<=q;i++)
    {
        scanf("%s\n",command);
        sscanf(command,"%d ",&mode);
        process(mode,command+2,Data);
    }
    return 0;
}

代码如上。不知道为什么。


by DustyMark @ 2024-09-01 03:01:32

@leyouyuan 既然用了iostreamcstring为何不用?

#include<bits/stdc++.h>
using namespace std;

int n;

int x;

int a,b;

string str;
string s;

int main(){

    cin>>n;
    cin>>s;

    for(int k=1;k<=n;k++)
    {
        cin>>x;

        if(x==1)
        {
            cin>>str;
            s=s+str;
            cout<<s<<"\n";
        }
        else if(x==2)
        {
            cin>>a>>b;
            string tmp="";
            for(int i=a;i<b+a;i++)
            {
                tmp+=s[i];
            }
            s=tmp;
            cout<<s<<"\n";
        }
        else if(x==3)
        {
            cin>>a;
            cin>>str;
            string s2="",s3="";
            for(int i=0;i<a;i++)
            {
                s2+=s[i];
            }
            for(int i=a;i<s.length();i++)
            {
                s3+=s[i];
            }
            s=s2+str+s3;
            cout<<s<<"\n";

        }
        else if(x==4)
        {
            cin>>str;
            bool f=1;
            for(int i=0;i<s.length();i++)
            {
//              cout<<s[i]<<"|"<<str[0]<<" ";
                if(s[i]==str[0])
                {
                    string s4="";
                    for(int j=i;j<i+str.length();j++)
                    {
                        s4+=s[j];
                    }
//                    cout<<"*"<<s4;
                    if(s4==str)
                    {
                        f=0;
                        cout<<i<<"\n";
                        break;
                    }
                }
            }

            if(f)cout<<-1<<"\n";

        }

    }

    return 0;
}

by pmkmzfuzsotqotmzs @ 2024-09-01 07:19:06

@leyouyuan

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

int main()
{
    int n;
    cin>>n;
    string str;
    cin>>str;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        if(x==1)
        {
            string s;
            cin>>s;
            cout<<str.append(s)<<endl;
        }
        if(x==2)
        {
            int a,b;
            cin>>a>>b;
            cout<<str.substr(a,b)<<endl;
            str=str.substr(a,b);
        }
        if(x==3)
        {
            int a;
            string s;
            cin>>a>>s;
            cout<<str.insert(a,s)<<endl;
        }
        if(x==4)
        {
            string s;
            cin>>s;
            if(str.find(s)!=std::string::npos)
                cout<<str.find(s)<<endl;
            else    
                cout<<"-1"<<endl;
        }
    }
    return 0;
}

by pmkmzfuzsotqotmzs @ 2024-09-01 07:20:13

@leyouyuan 直接用string函数


|