Linisdjxmk @ 2024-11-10 20:22:25
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *getlinex(void)
{
//getchar();
char * x = malloc(sizeof(char) * 1000);
char c;
int t = 0;
while((c = getchar()) != EOF && c != '\n')
{
x[t] = c;
t++;
}
x[t] = 0;
return x;
}
int main(void)
{
int n;
scanf("%d",&n);
getchar();
char *str0 = getlinex();
int c = 0;
char *comm [n];
char *topr [n + 1];
topr[0] = str0;
while(c < n)
{
comm[c] = getlinex();
c++;
}
c = 0;
int cs = 1;
while(c < n)
{
if(comm[c][0] == '1')
{
int tom = strlen(comm[c]) - 2;
int len = strlen(topr[cs - 1]) + tom + 1;
topr[cs] = malloc(len * sizeof(char));
sprintf(topr[cs],"%s%s",topr[cs - 1],comm[c] + 2);
//printf("%s",topr[c]);
}
if(comm[c][0] == '2')
{
int start,end;
sscanf(comm[c],"%*d %d %d",&start,&end);
topr[cs] = malloc(end * sizeof(char) + 1);
int c2 = start;
int c3 = 0;
while(c2 - start != 5)
{
topr[cs][c3] = topr[cs - 1][c2];
c3++;
c2++;
}
topr[cs][c3] = 0;
}
if(comm[c][0] == '3')
{
char tc[400];
int start;
sscanf(comm[c],"%*d %d %s",&start,tc);
int c2 = 0;
int c3 = start;
topr[cs] = malloc(sizeof(char) * (strlen(topr[cs - 1]) + strlen(tc) + 2));
while(c2 != c3)
{
topr[cs][c2] = topr[cs - 1][c2];
c2++;
}
int c4 = 0;
while(c4 < strlen(tc))
{
topr[cs][c2] = tc[c4];
c2++;
c4++;
}
while(topr[cs - 1][c3] != 0)
{
topr[cs][c2] = topr[cs - 1][c3];
c2++;
c3++;
}
topr[cs][c2] = 0;
}
if(comm[c][0] == '4')
{
char *tmpstr = comm[c] + 2;
char * res = strstr(topr[cs - 1],tmpstr);
topr[cs] = malloc(sizeof(char) * 10);
sprintf(topr[cs],"%d",res != NULL ? res - topr[cs - 1] : -1);
}
c++;
cs++;
}
free(str0);
c = 0;
while(c < n)
{
printf("%s\n",topr[c + 1]);
free(comm[c]);
free(topr[c + 1]);
c++;
}
}
在本地用valgrind反复检查,也没找到哪里的指针有问题