Обьясниите пожалуйста, как работает этот код. Чем подробнее, тем лучше
func.h
Код:
#pragma once
#ifndef FUNC_H
#define FUNC_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct SBirthday
{
int mDay;
int mMonth;
int mYear;
};
typedef struct SStudent
{
char mName[25];
char mSurname[25];
struct SBirthday mBirthday;
int mMarks[6];
struct SStudent* next, *prev;
} student;
void scanStudent(FILE* f, student* p);
void insertInProperPosition(student** head, student** tail, student* p);
void insertInTheEnd(student** tail, student* p);
void printList(student* head);
int studentCmp(student* a, student* b);
student* deleteStudent(student** head, student** p);
#endif // !FUNC_H
Source.c
Код:
#include "func.h"
int main()
{
int n = 1;
FILE* fl = fopen("C:\\Users\\oleg\\Desktop\\fl.txt", "r");
student* head = (student*)malloc(sizeof(student));
scanStudent(fl, head);
student* tail = head, *p = NULL;
while (!feof(fl))
{
p = (student*)malloc(sizeof(student));
scanStudent(fl, p);
insertInProperPosition(&head, &tail, p);
n++;
}
p = head;
student* psave = p;
while (p)
{
psave = p->next;
if (isStudentToDelete(p))
free(deleteStudent(&head, &p));
p = psave;
}
printList(head);
_getch();
return 0;
}
func.c
Код:
#include "func.h"
void scanStudent(FILE* fl, student* p)
{
fscanf(fl, "%s", &p->mSurname);
fscanf(fl, "%s", &p->mName);
fscanf(fl, "%d", &p->mBirthday.mDay);
fscanf(fl, "%d", &p->mBirthday.mMonth);
fscanf(fl, "%d", &p->mBirthday.mYear);
for (int i = 0; i < 6; i++)
fscanf(fl, "%d", &p->mMarks[i]);
p->next = NULL;
p->prev = NULL;
}
int isStudentToDelete(student* s)
{
int del = 0, m[5] = { 0 };
for (int i = 0; i < 6; i++)
m[s->mMarks[i] - 1] = 1;
for (int i = 1; i < 5; i++)
if (m[i] == 0)
return 0;
return 1;
}
void printList(student* head)
{
student* a = head;
while (a)
{
printf("%s %s\n", a->mSurname, a->mName);
a = a->next;
}
}
void insertInProperPosition(student** head, student** tail, student* p)
{
int inserted = 0;
if (studentCmp(p, *head) < 0)
{
p->next = *head;
(*head)->prev = p;
*head = p;
inserted = 1;
}
student* temp = *head;
while (temp->next && !inserted)
{
if (studentCmp(p, temp->next) < 0)
{
student* s = temp->next;
temp->next = p;
p->next = s;
s->prev = p;
p->prev = temp;
inserted = 1;
}
temp = temp->next;
}
if (!inserted)
{
(*tail)->next = p;
p->prev = *tail;
*tail = p;
}
}
void insertInTheEnd(student** tail, student* p)
{
(*tail)->next = p;
p->prev = (*tail);
(*tail) = p;
}
int studentCmp(student* a, student* b)
{
if (strcmp(a->mSurname, b->mSurname) < 0)
return -1;
if (strcmp(a->mSurname, b->mSurname) > 0)
return 1;
// surnames are equal here:
if (strcmp(a->mName, b->mName) < 0)
return -1;
if (strcmp(a->mName, b->mName) > 0)
return 1;
return 0;
}
student* deleteStudent(student** head, student** p)
{
student* toFree = *p;
if (!((*p)->prev))
{// p is head
(*p)->next->prev = NULL;
*p = (*p)->next;
*head = *p;
}
else if (!((*p)->next))
{// p is tail
(*p)->prev->next = (*p)->next;
}
else
{// p is in the middle
(*p)->prev->next = (*p)->next;
(*p)->next->prev = (*p)->prev;
}
return toFree;
}
Заранее благодарен.