Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Pupil
- {
- char first_name[20];
- char second_name[20];
- char gender[7];
- char grade[4];
- char birthdate[11];
- char adress[100];
- int balance;
- int height;
- int key;
- Pupil* Left;
- Pupil* Right;
- Pupil* Parent;
- };
- struct PupilTree
- {
- Pupil* Root;
- };
- void InitPupil(Pupil*);
- bool StringStartsWith(char [], char [], int);
- Pupil* AddPupil(PupilTree*, Pupil*, Pupil*);
- int TreeHeight(Pupil*);
- void OverHeight(Pupil*);
- Pupil* RightRotation(Pupil*);
- Pupil* LeftRotation(Pupil*);
- Pupil* Balance(Pupil*);
- Pupil *RemovePupil(Pupil *, char first_name[]);
- Pupil* FindPupilBySecondName(PupilTree*, char [], Pupil*);
- void SavePupil(Pupil*, FILE*);
- void SaveTree(PupilTree*, char []);
- void LoadTree(PupilTree*, char []);
- Pupil *SearchMin(Pupil*);
- Pupil *DeleteMin(Pupil*);
- int BalanceFactor(Pupil*);
- void ClearTree(PupilTree*);
- //PupilTree* Filter(PupilTree*, Pupil*);
- int BalanceFactor(Pupil *p)
- {
- return TreeHeight(p->Right) - TreeHeight(p->Left);
- }
- Pupil *SearchMin(Pupil *x)
- {
- if (x->Left) return SearchMin(x->Left);
- else return x;
- }
- Pupil *DeleteMin(Pupil *x)
- {
- if (x->Left==0) return x->Right;
- x->Left=DeleteMin(x->Left);
- return Balance(x);
- }
- void InitPupil(Pupil* pupil)
- {
- pupil->Left = NULL;
- pupil->Right = NULL;
- pupil->Parent = NULL;
- pupil->height = 0;
- }
- bool StringStartsWith(char TestArray[], char StringToFind[], int count)
- {
- for(int i=0;i<count;i++)
- {
- if(TestArray[i]!=StringToFind[i])
- return false;
- }
- return true;
- }
- Pupil* AddPupil(PupilTree* Tree, Pupil* pupil, Pupil* Current = NULL)
- {
- //if (Tree==NULL) return NULL;
- if (Tree->Root == NULL)
- {
- Tree->Root = pupil;
- Tree->Root->balance = 0;
- return Tree->Root;
- }
- if (Current == NULL) Current = Tree->Root;
- int c = strcmp(Current->first_name, pupil->first_name);
- if (c < 0)
- {
- if (Current->Left != NULL)
- Current->Left = AddPupil(Tree,pupil,Current->Left);
- else
- {
- Current->Left = pupil;
- pupil->height = Current->height + 1;
- //Balance(pupil);
- }
- }
- else
- {
- if (Current->Right != NULL)
- AddPupil(Tree, pupil, Current->Right);
- else
- {
- Current->Right = pupil;
- pupil->height = Current->height + 1;
- //Balance(pupil);
- }
- }
- return Balance(Current);
- }
- int TreeHeight(Pupil *pupil)
- {
- if (pupil) return pupil->height;
- else return 0;
- }
- void OverHeight(Pupil *pupil)
- {
- int hleft = TreeHeight(pupil->Left);
- int hright = TreeHeight(pupil->Right);
- pupil->height = (hleft>hright ? hleft : hright)+1;
- }
- Pupil* RightRotation(Pupil *pupil)
- {
- Pupil *y = pupil->Left;
- pupil->Left = y->Right;
- y->Right = pupil;
- OverHeight(pupil);
- OverHeight(y);
- return y;
- }
- Pupil* LeftRotation(Pupil *pupil)
- {
- Pupil *x = pupil->Right;
- pupil->Right = x->Left;
- x->Left = pupil;
- OverHeight(pupil);
- OverHeight(x);
- return x;
- }
- Pupil* Balance(Pupil *pupil)
- {
- OverHeight(pupil);
- if (BalanceFactor(pupil) == 2)
- {
- if (BalanceFactor(pupil->Right) < 0)
- pupil->Right=RightRotation(pupil->Right);
- return LeftRotation(pupil);
- }
- if (BalanceFactor(pupil) == -2)
- {
- if (BalanceFactor(pupil->Left) > 0)
- pupil->Left = LeftRotation(pupil->Left);
- return RightRotation(pupil);
- }
- return pupil;
- }
- Pupil *RemovePupil(Pupil *x, char first_name[])
- {
- if (x == NULL) return 0;
- if (strcmp(x->first_name, first_name) < 0)
- x->Left=RemovePupil(x->Left, first_name);
- else if (strcmp(x->first_name, first_name) > 0)
- x->Right=RemovePupil(x->Right, first_name);
- else
- {
- Pupil *l = x->Left;
- Pupil *r = x->Right;
- free(x);
- if (r == NULL) return l;
- Pupil* min = SearchMin(r);
- min->Right = DeleteMin(r);
- min->Left=l;
- return Balance(min);
- }
- return Balance(x);
- }
- Pupil* FindPupilBySecondName(PupilTree* Tree, char pupil_second_name[], Pupil* Current = NULL)
- {
- if (Tree == NULL) return NULL;
- if (Current == NULL) Current = Tree->Root;
- int c = strcmp(Current->second_name, pupil_second_name);
- if (c < 0)
- {
- if (Current->Left != NULL) return FindPupilBySecondName(Tree, pupil_second_name, Current->Left);
- else return NULL;
- }
- if (c > 0)
- {
- if (Current->Right != NULL) return FindPupilBySecondName(Tree, pupil_second_name, Current->Right);
- else return NULL;
- }
- if (c == 0)
- return Current;
- return NULL;
- }
- void SavePupil(Pupil* pupil, FILE* file)
- {
- if (file == NULL)
- return;
- if (pupil != NULL)
- {
- //fprintf(file, "%s\n%s\n%s\n%d\n%s\n%s", pupil->first_name, pupil->second_name, pupil->gender, pupil->grade, pupil->birthdate, pupil->adress);
- fputs(pupil->first_name, file);
- fputs("\n", file);
- fputs(pupil->second_name, file);
- fputs("\n", file);
- fputs(pupil->gender, file);
- fputs("\n", file);
- fputs(pupil->grade, file);
- fputs("\n", file);
- fputs(pupil->birthdate, file);
- fputs("\n", file);
- fputs(pupil->adress, file);
- fputs("\n", file);
- if (pupil->Left != NULL)
- SavePupil(pupil->Left, file);
- if (pupil->Right != NULL)
- SavePupil(pupil->Right, file);
- }
- }
- void SaveTree(PupilTree* Tree, char file_name[])
- {
- if (Tree == NULL) return;
- FILE *file;
- file = fopen(file_name, "w");
- if (file == NULL)
- {
- printf("\nCouldn't open the file: %s", file_name);
- return;
- }
- SavePupil(Tree->Root, file);
- fputs("NULL\n", file);
- fputs("NULL", file);
- fclose(file);
- }
- void LoadTree(PupilTree* Tree, char file_name[])
- {
- if (Tree == NULL) return;
- FILE* file;
- file = fopen(file_name, "r");
- if (file == NULL)
- {
- printf("\nCouldn't open the file: %s", file_name);
- return;
- }
- Pupil new_input_pupil;
- while (1)
- {
- fgets(new_input_pupil.first_name, 20, file);
- while (strcmp(new_input_pupil.first_name, "\n") == 0)
- {
- strcpy(new_input_pupil.first_name, "");
- fgets(new_input_pupil.first_name, 20, file);
- }
- fgets(new_input_pupil.second_name, 20, file);
- while (strcmp(new_input_pupil.second_name, "\n") == 0)
- {
- strcpy(new_input_pupil.second_name, "");
- fgets(new_input_pupil.second_name, 20, file);
- }
- fgets(new_input_pupil.gender, 7, file);
- while (strcmp(new_input_pupil.gender, "\n") == 0)
- {
- strcpy(new_input_pupil.gender, "");
- fgets(new_input_pupil.gender, 7, file);
- }
- fgets(new_input_pupil.grade, 4, file);
- while (strcmp(new_input_pupil.grade, "\n") == 0)
- {
- strcpy(new_input_pupil.grade, "");
- fgets(new_input_pupil.grade, 4, file);
- }
- fgets(new_input_pupil.birthdate, 11, file);
- while (strcmp(new_input_pupil.birthdate, "\n") == 0)
- {
- strcpy(new_input_pupil.birthdate, "");
- fgets(new_input_pupil.birthdate, 11, file);
- }
- fgets(new_input_pupil.adress, 100, file);
- while (strcmp(new_input_pupil.adress, "\n") == 0)
- {
- strcpy(new_input_pupil.adress, "");
- fgets(new_input_pupil.adress, 100, file);
- }
- if(!StringStartsWith(new_input_pupil.first_name, "NULL", 4) && !StringStartsWith(new_input_pupil.second_name, "NULL", 4))
- {
- Pupil* pupil = (Pupil*)malloc(sizeof(Pupil));
- size_t len;
- len = strlen(new_input_pupil.first_name);
- if (len > 0 && new_input_pupil.first_name[len-1] == '\n')
- new_input_pupil.first_name[--len] = '\0';
- strcpy(pupil->first_name, new_input_pupil.first_name);
- len = strlen(new_input_pupil.second_name);
- if (len > 0 && new_input_pupil.second_name[len-1] == '\n')
- new_input_pupil.second_name[--len] = '\0';
- strcpy(pupil->second_name, new_input_pupil.second_name);
- len = strlen(new_input_pupil.gender);
- if (len > 0 && new_input_pupil.gender[len-1] == '\n')
- new_input_pupil.gender[--len] = '\0';
- strcpy(pupil->gender, new_input_pupil.gender);
- len = strlen(new_input_pupil.grade);
- if (len > 0 && new_input_pupil.grade[len-1] == '\n')
- new_input_pupil.grade[--len] = '\0';
- strcpy(pupil->grade, new_input_pupil.grade);
- len = strlen(new_input_pupil.birthdate);
- if (len > 0 && new_input_pupil.birthdate[len-1] == '\n')
- new_input_pupil.birthdate[--len] = '\0';
- strcpy(pupil->birthdate, new_input_pupil.birthdate);
- len = strlen(new_input_pupil.adress);
- if (len > 0 && new_input_pupil.adress[len-1] == '\n')
- new_input_pupil.adress[--len] = '\0';
- strcpy(pupil->adress, new_input_pupil.adress);
- InitPupil(pupil);
- AddPupil(Tree, pupil);
- }
- else
- break;
- }
- fclose(file);
- }
- void ClearTree(PupilTree* Tree, Pupil* Current = NULL)
- {
- if (Current == NULL) Current = Tree->Root;
- if (Current == NULL) return;
- if (Current->Left != NULL)
- ClearTree(Tree, Current->Left);
- if (Current->Right != NULL)
- ClearTree(Tree, Current->Right);
- free(Current);
- }
- void InitTree(PupilTree *t)
- {
- t->Root = NULL;
- }
- void Filter()
- {
- return;
- }
- int main()
- {
- int is_continue = 1;
- char file_name[] = "file.txt";
- char name[20];
- PupilTree t;
- PupilTree filter_t;
- InitTree(&t);
- InitTree(&filter_t);
- PupilTree *Tree = &t;
- PupilTree *FilterTree = &filter_t;
- while (is_continue)
- {
- printf("\nMenu\n\n");
- printf("1 - Add pupil\n");
- printf("2 - Remove pupil\n");
- printf("3 - Find pupil by second name\n");
- printf("4 - Save tree in file\n");
- printf("5 - Load tree from file\n");
- printf("6 - Filer by birthdate\n");
- printf("7 - Exit\n");
- printf("What to do? ");
- int action;
- scanf("%d", &action);
- if (action == 1)
- {
- Pupil *pupil = (Pupil*) malloc(sizeof(Pupil));
- printf("Input data about new pupil:\nFirst name: ");
- scanf("%s", pupil->first_name);
- printf("Second name: ");
- scanf("%s", pupil->second_name);
- printf("Gender: ");
- scanf("%s", pupil->gender);
- printf("Grade: ");
- scanf("%s", pupil->grade);
- printf("Birthdate: ");
- scanf("%s", pupil->birthdate);
- printf("Adress: ");
- scanf("%s", pupil->adress);
- InitPupil(pupil);
- pupil = AddPupil(Tree, pupil);
- //free(pupil);
- }
- else if (action == 2)
- {
- printf("Input first name: ");
- scanf("%s", name);
- RemovePupil(Tree->Root, name);
- }
- else if (action == 3)
- {
- printf("Input second name: ");
- scanf("%s", name);
- Pupil *pupil = FindPupilBySecondName(Tree, name);
- if (pupil == NULL)
- printf("Not found\n");
- else
- {
- printf("SUCCESS!\n");
- printf("First name: %s\n", pupil->first_name);
- printf("Second name: %s\n", pupil->second_name);
- printf("Gender: %s\n", pupil->gender);
- printf("Grate: %s\n", pupil->grade);
- printf("Birthday: %s\n", pupil->birthdate);
- printf("Adress: %s\n", pupil->adress);
- }
- }
- else if (action == 4)
- SaveTree(Tree, file_name);
- else if (action == 5)
- {
- //ClearTree(Tree, NULL);
- LoadTree(Tree, file_name);
- }
- else if (action == 6)
- {
- Filter();
- }
- else if (action == 7)
- is_continue = 0;
- else is_continue = 1;
- }
- ClearTree(Tree, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement