Jack-Carter
08-07-2008, 13:58
Salve a tutti, avevo postato già qualche giorno fa per un problema riguardo ad una malloc, senza riscuotere molto successo. Provo a riproporre il problema che si presenta questa volta con i campi di una struttura. Il programma deve leggere da un file la sequenza di mosse di alcune partite di scacchi, e poi aprire un secondo file, leggere una sequenza di mosse, e dire in quali partite si è trovata quella sequenza in ordine cronologico. Il file è di questo tipo:
5
#Roma 23.12.1999 Jack Mike 5
c4 c1
c5 c6
c7 c8
c9 c9
c4 c2
#Cairo 14.05.1996 Luke David 3
c4 c1
c5 c6
c7 c8
#Parigi 03.10.1998 Mark Marcel 7
c9 c9
c4 c2
c3 c7
c1 c8
c6 c2
c8 c9
c5 c7
#Londra 01.02.1997 Albert Mitch 10
c6 c2
c8 c9
c5 c7
c9 c9
c4 c2
c3 c7
c7 c1
c9 c5
c6 c3
c4 c8
#Berlino 28.01.1997 Charlotte Sophie 14
c4 c1
c5 c6
c7 c8
c4 c1
c5 c6
c7 c8
c4 c1
c5 c6
c7 c8
c9 c9
c4 c2
c3 c7
c9 c5
c6 c3
Mentre il codice del programma scritto finora è questo:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct match{
char *loc;
char *white;
char *black;
int day, month, year;
char **moves;
int nmoves;
};
int swap(struct match *sorted, int x);
int main()
{
FILE *input, *seq;
struct match *mc, *sorted;
int i, j, day, month, year, nmoves, n, nseq, nsorted, w, x, l;
char inputn[100], c, loc[100], white[100], black[100], moves[22];
input=fopen("input.txt", "r");
fscanf(input, "%d%c", &n, &c);
mc=(struct match*)malloc(n*sizeof(struct match));
for(i=0;i<n;i++)
{
fscanf(input, "%c%s %d%c%d%c%d%c %s %s %d", &c, loc, &day, &c, &month, &c, &year, &c, white, black, &nmoves);
mc[i].nmoves=nmoves;
mc[i].day=day;
mc[i].month=month;
mc[i].year=year;
mc[i].loc=(char*)malloc((strlen(loc)+1)*sizeof(char));
mc[i].white=(char*)malloc((strlen(white)+1)*sizeof(char));
mc[i].black=(char*)malloc((strlen(black)+1)*sizeof(char));
mc[i].moves=(char**)malloc(nmoves*sizeof(char*));
strcpy(mc[i].loc, loc);
strcpy(mc[i].white, white);
strcpy(mc[i].black, black);
printf("%s %d %d %d %s %s\n", loc, day, month, year, white, black);
fscanf(input, "%c", &c);
for(j=0;j<nmoves;j++)
{
fgets(moves, 22, input);
mc[i].moves[j]=(char*)malloc((strlen(moves)+1)*sizeof(char));
strcpy(mc[i].moves[j], moves);
}
}
fclose(input);
char **seqmov;
do{
//printf("Inserisci il nome del file con la sequenza di mosse ");
//scanf("%s", inputn);
if(strcmp(inputn, "fine")!=0)
{
seq=fopen("mov1.txt", "r");
fscanf(seq, "%d%c", &nseq, &c);
seqmov=(char**)malloc(nseq*sizeof(char*));
for(i=0;i<nseq;i++)
{
fgets(moves, 22, seq);
seqmov[i]=(char*)malloc((strlen(moves)+1)*sizeof(char));
strcpy(seqmov[i], moves);
}
l=strlen(seqmov[i-1]);
seqmov[i-1][l]='\n';
seqmov[i-1][l+1]='\0';
nsorted=0;
for(i=0;i<n;i++)
{
for(j=0;j<mc[i].nmoves;j++)
{
if(strcmp(mc[i].moves[j], seqmov[0])==0)
{
int z=j+1, y=1;
while((z<j+nseq)&&(y!=0)&&(z<mc[i].nmoves))
{
if(strcmp(mc[i].moves[z], seqmov[y])==0)
{
//printf("stringa uguale, i=%d, z=%d, y=%d\n", i, z, y);
z++;
y++;
}
else
{
y=0;
}
}
if(y==nseq)
{
if(nsorted==0)
{
sorted=(struct match*)malloc(sizeof(struct match));
sorted[nsorted].day=mc[i].day;
sorted[nsorted].month=mc[i].month;
sorted[nsorted].year=mc[i].year;
sorted[nsorted].nmoves=j;
sorted[nsorted].loc=(char*)malloc(sizeof(char));
sorted[nsorted].white=(char*)malloc(sizeof(char));
sorted[nsorted].black=(char*)malloc(sizeof(char));
strcpy(sorted[nsorted].loc, mc[i].loc);
strcpy(sorted[nsorted].white, mc[i].white);
strcpy(sorted[nsorted].black, mc[i].black);
}
else
{
sorted=(struct match*)realloc(sorted, (nsorted+2));
sorted[nsorted].day=mc[i].day;
sorted[nsorted].month=mc[i].month;
sorted[nsorted].year=mc[i].year;
sorted[nsorted].nmoves=j;
sorted[nsorted].loc=(char*)malloc(sizeof(char));
sorted[nsorted].white=(char*)malloc(sizeof(char));
sorted[nsorted].black=(char*)malloc(sizeof(char));
strcpy(sorted[nsorted].loc, mc[i].loc);
strcpy(sorted[nsorted].white, mc[i].white);
strcpy(sorted[nsorted].black, mc[i].black);
}
for(w=1;w<nsorted+1;w++)
{
for(x=0;x<nsorted;x++)
{
if(sorted[x].year>sorted[x+1].year)
{
swap(sorted, x);
printf("%d", sorted[x].year);
}
else if(sorted[x].year==sorted[x+1].year)
{
if(sorted[x].month>sorted[x+1].month)
{
swap(sorted, x);
}
else if(sorted[x].month==sorted[x+1].month)
{
if(sorted[x].day>sorted[x+1].day)
{
swap(sorted, x);
}
}
}
}
}
nsorted++;
j=z;
}
}
}
}
for(i=0;i<nsorted;i++)
{
printf("%s %d.%d.%d %s %s %d\n", sorted[i].loc, sorted[i].day, sorted[i].month, sorted[i].year, sorted[i].white, sorted[i].black, sorted[i].nmoves);
}
free(sorted);
free(seqmov);
fclose(seq);
}
}while(n==0);
//while(strcmp(inputn, "fine")!=0);
return 0;
}
int swap(struct match *sorted, int x)
{
struct match temp;
temp.day=sorted[x].day;
temp.month=sorted[x].month;
temp.year=sorted[x].year;
temp.nmoves=sorted[x].nmoves;
temp.loc=(char*)malloc(sizeof(char));
temp.white=(char*)malloc(sizeof(char));
temp.black=(char*)malloc(sizeof(char));
strcpy(temp.loc, sorted[x].loc);
strcpy(temp.white, sorted[x].white);
strcpy(temp.black, sorted[x].black);
sorted[x].day=sorted[x+1].day;
sorted[x].month=sorted[x+1].month;
sorted[x].year=sorted[x+1].year;
sorted[x].nmoves=sorted[x+1].nmoves;
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1));
sorted[x].white=(char*)realloc(sorted[x].white, sizeof(strlen(sorted[x+1].white)+1));
sorted[x].black=(char*)realloc(sorted[x].black, sizeof(strlen(sorted[x+1].black)+1));
strcpy(sorted[x].loc, sorted[x+1].loc);
strcpy(sorted[x].white, sorted[x+1].white);
strcpy(sorted[x].black, sorted[x+1].black);
sorted[x+1].day=temp.day;
sorted[x+1].month=temp.month;
sorted[x+1].year=temp.year;
sorted[x+1].nmoves=temp.nmoves;
sorted[x+1].loc=(char*)realloc(sorted[x+1].loc, strlen(temp.loc)+1);
sorted[x+1].white=(char*)realloc(sorted[x+1].white, strlen(temp.white)+1);
sorted[x+1].black=(char*)realloc(sorted[x+1].black, strlen(temp.black)+1);
strcpy(sorted[x+1].loc, temp.loc);
strcpy(sorted[x+1].white, temp.white);
strcpy(sorted[x+1].black, temp.black);
return 0;
}
Il problema si presenta nelle realloc della funzione swap. ho provato anche a scrivere una cosa tipo
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1)*(char));
Ma non è cambiato nulla.
5
#Roma 23.12.1999 Jack Mike 5
c4 c1
c5 c6
c7 c8
c9 c9
c4 c2
#Cairo 14.05.1996 Luke David 3
c4 c1
c5 c6
c7 c8
#Parigi 03.10.1998 Mark Marcel 7
c9 c9
c4 c2
c3 c7
c1 c8
c6 c2
c8 c9
c5 c7
#Londra 01.02.1997 Albert Mitch 10
c6 c2
c8 c9
c5 c7
c9 c9
c4 c2
c3 c7
c7 c1
c9 c5
c6 c3
c4 c8
#Berlino 28.01.1997 Charlotte Sophie 14
c4 c1
c5 c6
c7 c8
c4 c1
c5 c6
c7 c8
c4 c1
c5 c6
c7 c8
c9 c9
c4 c2
c3 c7
c9 c5
c6 c3
Mentre il codice del programma scritto finora è questo:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct match{
char *loc;
char *white;
char *black;
int day, month, year;
char **moves;
int nmoves;
};
int swap(struct match *sorted, int x);
int main()
{
FILE *input, *seq;
struct match *mc, *sorted;
int i, j, day, month, year, nmoves, n, nseq, nsorted, w, x, l;
char inputn[100], c, loc[100], white[100], black[100], moves[22];
input=fopen("input.txt", "r");
fscanf(input, "%d%c", &n, &c);
mc=(struct match*)malloc(n*sizeof(struct match));
for(i=0;i<n;i++)
{
fscanf(input, "%c%s %d%c%d%c%d%c %s %s %d", &c, loc, &day, &c, &month, &c, &year, &c, white, black, &nmoves);
mc[i].nmoves=nmoves;
mc[i].day=day;
mc[i].month=month;
mc[i].year=year;
mc[i].loc=(char*)malloc((strlen(loc)+1)*sizeof(char));
mc[i].white=(char*)malloc((strlen(white)+1)*sizeof(char));
mc[i].black=(char*)malloc((strlen(black)+1)*sizeof(char));
mc[i].moves=(char**)malloc(nmoves*sizeof(char*));
strcpy(mc[i].loc, loc);
strcpy(mc[i].white, white);
strcpy(mc[i].black, black);
printf("%s %d %d %d %s %s\n", loc, day, month, year, white, black);
fscanf(input, "%c", &c);
for(j=0;j<nmoves;j++)
{
fgets(moves, 22, input);
mc[i].moves[j]=(char*)malloc((strlen(moves)+1)*sizeof(char));
strcpy(mc[i].moves[j], moves);
}
}
fclose(input);
char **seqmov;
do{
//printf("Inserisci il nome del file con la sequenza di mosse ");
//scanf("%s", inputn);
if(strcmp(inputn, "fine")!=0)
{
seq=fopen("mov1.txt", "r");
fscanf(seq, "%d%c", &nseq, &c);
seqmov=(char**)malloc(nseq*sizeof(char*));
for(i=0;i<nseq;i++)
{
fgets(moves, 22, seq);
seqmov[i]=(char*)malloc((strlen(moves)+1)*sizeof(char));
strcpy(seqmov[i], moves);
}
l=strlen(seqmov[i-1]);
seqmov[i-1][l]='\n';
seqmov[i-1][l+1]='\0';
nsorted=0;
for(i=0;i<n;i++)
{
for(j=0;j<mc[i].nmoves;j++)
{
if(strcmp(mc[i].moves[j], seqmov[0])==0)
{
int z=j+1, y=1;
while((z<j+nseq)&&(y!=0)&&(z<mc[i].nmoves))
{
if(strcmp(mc[i].moves[z], seqmov[y])==0)
{
//printf("stringa uguale, i=%d, z=%d, y=%d\n", i, z, y);
z++;
y++;
}
else
{
y=0;
}
}
if(y==nseq)
{
if(nsorted==0)
{
sorted=(struct match*)malloc(sizeof(struct match));
sorted[nsorted].day=mc[i].day;
sorted[nsorted].month=mc[i].month;
sorted[nsorted].year=mc[i].year;
sorted[nsorted].nmoves=j;
sorted[nsorted].loc=(char*)malloc(sizeof(char));
sorted[nsorted].white=(char*)malloc(sizeof(char));
sorted[nsorted].black=(char*)malloc(sizeof(char));
strcpy(sorted[nsorted].loc, mc[i].loc);
strcpy(sorted[nsorted].white, mc[i].white);
strcpy(sorted[nsorted].black, mc[i].black);
}
else
{
sorted=(struct match*)realloc(sorted, (nsorted+2));
sorted[nsorted].day=mc[i].day;
sorted[nsorted].month=mc[i].month;
sorted[nsorted].year=mc[i].year;
sorted[nsorted].nmoves=j;
sorted[nsorted].loc=(char*)malloc(sizeof(char));
sorted[nsorted].white=(char*)malloc(sizeof(char));
sorted[nsorted].black=(char*)malloc(sizeof(char));
strcpy(sorted[nsorted].loc, mc[i].loc);
strcpy(sorted[nsorted].white, mc[i].white);
strcpy(sorted[nsorted].black, mc[i].black);
}
for(w=1;w<nsorted+1;w++)
{
for(x=0;x<nsorted;x++)
{
if(sorted[x].year>sorted[x+1].year)
{
swap(sorted, x);
printf("%d", sorted[x].year);
}
else if(sorted[x].year==sorted[x+1].year)
{
if(sorted[x].month>sorted[x+1].month)
{
swap(sorted, x);
}
else if(sorted[x].month==sorted[x+1].month)
{
if(sorted[x].day>sorted[x+1].day)
{
swap(sorted, x);
}
}
}
}
}
nsorted++;
j=z;
}
}
}
}
for(i=0;i<nsorted;i++)
{
printf("%s %d.%d.%d %s %s %d\n", sorted[i].loc, sorted[i].day, sorted[i].month, sorted[i].year, sorted[i].white, sorted[i].black, sorted[i].nmoves);
}
free(sorted);
free(seqmov);
fclose(seq);
}
}while(n==0);
//while(strcmp(inputn, "fine")!=0);
return 0;
}
int swap(struct match *sorted, int x)
{
struct match temp;
temp.day=sorted[x].day;
temp.month=sorted[x].month;
temp.year=sorted[x].year;
temp.nmoves=sorted[x].nmoves;
temp.loc=(char*)malloc(sizeof(char));
temp.white=(char*)malloc(sizeof(char));
temp.black=(char*)malloc(sizeof(char));
strcpy(temp.loc, sorted[x].loc);
strcpy(temp.white, sorted[x].white);
strcpy(temp.black, sorted[x].black);
sorted[x].day=sorted[x+1].day;
sorted[x].month=sorted[x+1].month;
sorted[x].year=sorted[x+1].year;
sorted[x].nmoves=sorted[x+1].nmoves;
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1));
sorted[x].white=(char*)realloc(sorted[x].white, sizeof(strlen(sorted[x+1].white)+1));
sorted[x].black=(char*)realloc(sorted[x].black, sizeof(strlen(sorted[x+1].black)+1));
strcpy(sorted[x].loc, sorted[x+1].loc);
strcpy(sorted[x].white, sorted[x+1].white);
strcpy(sorted[x].black, sorted[x+1].black);
sorted[x+1].day=temp.day;
sorted[x+1].month=temp.month;
sorted[x+1].year=temp.year;
sorted[x+1].nmoves=temp.nmoves;
sorted[x+1].loc=(char*)realloc(sorted[x+1].loc, strlen(temp.loc)+1);
sorted[x+1].white=(char*)realloc(sorted[x+1].white, strlen(temp.white)+1);
sorted[x+1].black=(char*)realloc(sorted[x+1].black, strlen(temp.black)+1);
strcpy(sorted[x+1].loc, temp.loc);
strcpy(sorted[x+1].white, temp.white);
strcpy(sorted[x+1].black, temp.black);
return 0;
}
Il problema si presenta nelle realloc della funzione swap. ho provato anche a scrivere una cosa tipo
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1)*(char));
Ma non è cambiato nulla.