Begmart

Junior Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Во. Правда нет ввода из файла, есть только фио и оценки, но это всё мелочи, главное то, что всё работает . Код: #include "stdio.h" #include "alloc.h" #include "string.h" typedef struct { char *fio; char *marks; } student, *pstudent; typedef struct { pstudent students; int count; } *pStudentsArray; void InitStudentsArray(pStudentsArray psa) { psa->students = NULL; psa->count = 0; } void AddStudent(pStudentsArray psa, pstudent ps) { psa->count++; psa->students = (pstudent)realloc(psa->students, sizeof(student) * psa->count); psa->students[psa->count - 1].fio = (char*)malloc(strlen(ps->fio) + 1); strcpy(psa->students[psa->count - 1].fio, ps->fio); psa->students[psa->count - 1].marks = (char*)malloc(strlen(ps->marks) + 1); strcpy(psa->students[psa->count - 1].marks, ps->marks); } void DeleteStudent(pStudentsArray psa, int index) { int i; if (index >= 0 && index < psa->count) { free(psa->students[index].fio); free(psa->students[index].marks); for(i = index; i < psa->count - 1; i++) psa->students[i] = psa->students[i + 1]; psa->count--; psa->students = (pstudent)realloc(psa->students, sizeof(student) * psa->count); } } void ReplaceStudent(pStudentsArray psa, int index, pstudent ps) { if (index >= 0 && index < psa->count) { free(psa->students[index].fio); free(psa->students[index].marks); psa->students[index].fio = (char*)malloc(strlen(ps->fio) + 1); strcpy(psa->students[index].fio, ps->fio); psa->students[index].marks = (char*)malloc(strlen(ps->marks) + 1); strcpy(psa->students[index].marks, ps->marks); } } void FreeStudentsArray(pStudentsArray psa) { int i, j = psa->count; for(i = 0; i < j; i++) DeleteStudent(psa, 0); } void PrintAll(pStudentsArray psa) { int i; printf("Count: %d\n",psa->count); for(i = 0; i < psa->count; i++) printf("#%d %s - %s\n", i + 1, psa->students[i].fio, psa->students[i].marks); } void main() { pStudentsArray psa; pstudent p; InitStudentsArray(psa); printf("\n"); p = (pstudent)malloc(sizeof(student)); p->fio = (char*)malloc(6); p->marks = (char*)malloc(5); strcpy(p->fio, "test1"); strcpy(p->marks, "1111"); AddStudent(psa, p); PrintAll(psa); strcpy(p->fio, "test2"); strcpy(p->marks, "2222"); AddStudent(psa, p); PrintAll(psa); strcpy(p->fio, "test3"); strcpy(p->marks, "3333"); AddStudent(psa, p); PrintAll(psa); strcpy(p->fio, "test4"); strcpy(p->marks, "4444"); AddStudent(psa, p); PrintAll(psa); strcpy(p->fio, "test5"); strcpy(p->marks, "5555"); AddStudent(psa, p); PrintAll(psa); DeleteStudent(psa, 1); DeleteStudent(psa, 2); strcpy(p->fio, "test6"); strcpy(p->marks, "6666"); ReplaceStudent(psa, 0, p); PrintAll(psa); FreeStudentsArray(psa); PrintAll(psa); } | Добавлено Кстати, писано на Borland Turbo C++ 3.0, если что. Правда вроде как компилятор должен быть сишный... Добавлено Кстати 2. Не проверял на строках разной длины . Хотя работать должно... |