rew

Full Member | Редактировать | Профиль | Сообщение | ICQ | Цитировать | Сообщить модератору мож есть какие глюки/баги, но удаление должно быть примерно такое Код: #include<stdio.h> #include<conio.h> #include<string.h> typedef struct{ char* fio; char* address; char* phone; char* marks; }student,*pstudent; typedef struct{ pstudent students; int size; }studentArray,*pstudentArray; void initStudentArray(pstudentArray sa){ sa->students=NULL; sa->size=0; } void addStudentToArray(pstudentArray sa,pstudent ps){ sa->size++; sa->students=(pstudent)realloc(sa->students,sizeof(student)*sa->size); *(sa->students+sa->size-1)=*ps; } pstudent removeStudent(pstudentArray sa,int index){ int i; student removedStudent; if(index>=0&&index<sa->size){ removedStudent=sa->students[index]; for(i=index;i<sa->size-1;i++){ sa->students[i]=sa->students[i+1]; } sa->size--; sa->students=(pstudent)realloc(sa->students,sizeof(student)*sa->size); return &removedStudent; } return NULL; } void freeStudent(pstudent std){ if(std!=NULL){ free(std->fio); free(std->marks); } } void freeStudentArray(pstudentArray sa){ int i; for(i=0;i<sa->size;i++){ freeStudent(&sa->students[i]); } free(sa->students); initStudentArray(sa); } void printStudentArray(studentArray sa){ int i; for(i=0;i<sa.size;i++){ printf("%s:%s\n",sa.students[i].fio,sa.students[i].marks); } } int readLine(char** dst,FILE* file){ char line[1024]; int len; if(feof(file)||fgets(line,1024,file)==NULL){ return 0; } len=strlen(line); if(line[len-1]=='\r'||line[len-1]=='\n'){ line[len-1]='\0'; } *dst=(char*)malloc(len+1); strcpy(*dst,line); return 1; } int ReadFromFile(pstudentArray sa,FILE *from) { pstudent newStudent; newStudent = (pstudent)malloc(sizeof(student)); if(readLine(&newStudent->fio,from)==0){ free(newStudent); return 0; } if(readLine(&newStudent->marks,from)==0){ free(newStudent); return 0; } addStudentToArray(sa,newStudent); return 1; } int fcmpStudentName(const void *src, const void *dst){ return strcmp(((pstudent)(src))->fio,((pstudent)(dst))->fio); } void main(){ FILE *file; studentArray sa; pstudent ds; initStudentArray(&sa); file=fopen("students.dat","r"); if(file==NULL){ puts("file not found"); return; } while(ReadFromFile(&sa,file)); fclose(file); qsort(sa.students,sa.size,sizeof(student),&fcmpStudentName); printStudentArray(sa); ds=removeStudent(&sa,0); freeStudent(ds); puts("------------------------"); printStudentArray(sa); freeStudentArray(&sa); getch(); } |
| Всего записей: 442 | Зарегистр. 09-09-2001 | Отправлено: 23:46 17-05-2003 | Исправлено: rew, 23:57 17-05-2003 |
|