Advertisement
Guest User

Untitled

a guest
May 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.02 KB | None | 0 0
  1. // UchilishtaSvurzanSpisuk.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8.  
  9. //School Struct
  10. typedef struct {
  11.     char name[50];
  12.     char directorName[30];
  13.     int ID,children;
  14.     int area;
  15. }School;
  16. //Node
  17. typedef struct node {
  18.     School school;
  19.     struct node *next;//Points to the next node
  20. }node;
  21.  
  22. node *makeSrtLst(node *head, FILE *f,int option = 0) {
  23.     rewind(f);
  24.     node *p, *crnt, *prev;
  25.     do {
  26.         p = (node *)malloc(sizeof(node)); /* if p==0 */
  27.         p->next = NULL;
  28.         if (!fread(&(p->school), sizeof(School), 1, f)) {
  29.             free(p);
  30.             break;
  31.         }
  32.         /* find the correct place – sorted by n */
  33.         if (option != 4) {
  34.             for (prev = NULL, crnt = head;
  35.                 crnt && (crnt->school.children > p->school.children);
  36.                 prev = crnt, crnt = crnt->next);
  37.         }
  38.         else {
  39.             for (prev = NULL, crnt = head;
  40.                 crnt && (crnt->school.area > p->school.area);
  41.                 prev = crnt, crnt = crnt->next);
  42.         }
  43.         // put in the list
  44.         if (prev) {
  45.             prev->next = p;
  46.         }
  47.         else {
  48.             head = p;
  49.         }
  50.         p->next = crnt;
  51.         /* or put directly in the head of the list – no sort
  52.         p->next=head;
  53.         head = p;
  54.         */
  55.     } while (1);
  56.     return head;
  57. }
  58.  
  59. //Print the given node
  60. void prt(node *crnt) {
  61.     while (crnt) {
  62.         printf("Next School Information:\n");
  63.         printf("School Name: %s\n", crnt->school.name);
  64.         printf("School ID: %d\n", crnt->school.ID);
  65.         printf("School Students: %d\n", crnt->school.children);
  66.         printf("School Area: %d\n", crnt->school.area);
  67.         printf("School director's name: %s\n", crnt->school.directorName);
  68.         crnt = crnt->next;
  69.     }
  70. }
  71. //Free Memory
  72. node *free_m(node *crnt) {
  73.     node *next;
  74.     printf("Memory free\n");
  75.     while (crnt) {
  76.         next = crnt->next; free(crnt); crnt = next;
  77.     }
  78.     return NULL;
  79. }
  80. //Validation(If The given ID is uniqe returns 1)
  81. int validateID(node *head, int ID) {
  82.     node *crnt;
  83.     crnt = head;
  84.     while (crnt) {
  85.         if (crnt->school.ID == ID) {
  86.             printf("zashto");
  87.             return 0;
  88.         }
  89.         crnt = crnt->next;
  90.     }
  91.     return 1;
  92. }
  93. //Create a new binary file and save the input information
  94. void saveInfoInFile(FILE *f) {
  95.     School s;
  96.     node *head = NULL;
  97.     do {
  98.         head = makeSrtLst(head, f);
  99.         printf("Next School name (Put '*' To end):");
  100.         scanf(" %49[^\n]", s.name);
  101.         if (!strcmp(s.name, "*")) {
  102.             break;
  103.         }
  104.         printf("%s School ID: ", s.name); fflush(stdin);
  105.         scanf(" %d", &s.ID);
  106.         //Check if the ID is unique
  107.         while (!validateID(head,s.ID)) {
  108.             printf("The ID of the schools should be uniqe\n: ");
  109.             printf("%s School ID: ", s.name); fflush(stdin);
  110.             scanf("%d", &s.ID);
  111.         }
  112.         printf("%d %s Number of students: ", s.ID, s.name); fflush(stdin);
  113.         scanf(" %d", &(s.children));
  114.         //The students must be more than 0
  115.         while (s.children < 0) {
  116.             printf("The children should be >= 0\n: ");
  117.             printf("%s - Number of Students: ", s.name); fflush(stdin);
  118.             scanf(" %d", &s.children);
  119.         }
  120.         printf("%d %s School Area:", s.ID, s.name); fflush(stdin);
  121.         scanf(" %d", &(s.area));
  122.         while (s.area <= 50) {
  123.             printf("The area should be > 50\n: ");
  124.             printf("%s - Area : ", s.name); fflush(stdin);
  125.             scanf(" %d", &s.area);
  126.         }
  127.         printf("%d %s School director's name:", s.ID, s.name); fflush(stdin);
  128.         scanf(" %29[^\n]", s.directorName); fflush(stdin);
  129.         //Write the structure into the file
  130.         fwrite(&s, sizeof(School), 1, f);
  131.         printf("Writing the structure in the file\n");
  132.     } while (1);
  133.     head = free_m(head);
  134.     printf("The file is created\n");
  135. }
  136. //Change School Information
  137. void renewInfoFile(node *crnt,FILE *fp) {
  138.     School s;
  139.     while (crnt) {
  140.         s = crnt->school;
  141.         fwrite(&s, sizeof(School), 1, fp);
  142.         crnt = crnt->next;
  143.     }
  144. }
  145. node *changeSchoolInfo(node *crnt, int ID) {
  146.     node *head = NULL, *prev;
  147.     int choice = 0, tmpID;
  148.     head = crnt;
  149.     prev = crnt;
  150.     while (crnt) {
  151.         if (crnt->school.ID == ID) {
  152.             do {
  153.                 printf("School Information:\n");
  154.                 printf("School Name: %s\n", crnt->school.name);
  155.                 printf("School ID: %d\n", crnt->school.ID);
  156.                 printf("School Students: %d\n", crnt->school.children);
  157.                 printf("School Area: %d\n", crnt->school.area);
  158.                 printf("School director's name: %s\n", crnt->school.directorName);
  159.                 printf("1 - Change name\n2 - Change SchoolID\n3 - Change number of students\n4 - Change School area\n5 - Change director's name\n6 - Za izhod\n"); fflush(stdin);
  160.                 scanf("%d", &choice);
  161.                 switch (choice) {
  162.                 case 1:
  163.                     printf("New School Name:"); fflush(stdin);
  164.                     scanf(" %49[^\n]", crnt->school.name);
  165.                     break;
  166.                 case 2:
  167.                     printf("New School ID:"); fflush(stdin);
  168.                     scanf(" %d", &tmpID);
  169.                     while ((!validateID(head, tmpID)) ) {
  170.                         if (tmpID == crnt->school.ID) {
  171.                             break;
  172.                         }
  173.                         printf("The ID of the schools should be uniqe\n: "); fflush(stdin);
  174.                         scanf("%d", &tmpID);
  175.                     }
  176.                     crnt->school.ID = tmpID;
  177.                     break;
  178.                 case 3:
  179.                     printf("New Number of students:"); fflush(stdin);
  180.                     scanf(" %d", &crnt->school.children);
  181.                     if(crnt->school.children <= 0) {
  182.                         prev->next = crnt->next;
  183.                         return head;
  184.                     }
  185.                     break;
  186.                 case 4:
  187.                     printf("New School Area:"); fflush(stdin);
  188.                     scanf(" %d", &crnt->school.area);
  189.                     break;
  190.                 case 5:
  191.                     printf("New Director's Name:"); fflush(stdin);
  192.                     scanf(" %29[^\n]", crnt->school.directorName);
  193.                     break;
  194.                 default:
  195.                     break;
  196.                 }
  197.                
  198.             } while (choice > 0 && choice < 6);
  199.             break;
  200.         }
  201.         prev = crnt;
  202.         crnt = crnt->next;
  203.     }
  204.     prt(head);
  205.     return head;
  206. }
  207. //Add school to the file
  208. void addSchool() {
  209.     FILE *fp;
  210.     School s;
  211.     fp = fopen("text.dat", "ab+");
  212.     if (!fp) {
  213.         return;
  214.     }
  215.     saveInfoInFile(fp);
  216.     fclose(fp);
  217. }
  218. //Change information about the school
  219. void changeInfo(node *head) {
  220.     FILE *fp;
  221.     School s;
  222.     fp = fopen("text.dat", "wb");
  223.     if (!fp) {
  224.         return;
  225.     }
  226.     int choice = 0, ID = 0, value = 0;
  227.     do {
  228.         printf("School's ID which has to be updated");
  229.         printf("\nVuvedete 0 za izhod: "); fflush(stdin);
  230.         scanf(" %d", &ID);
  231.         if (ID == 0) {
  232.             break;
  233.         }
  234.         printf("\nstigame li do tuk");
  235.         head = changeSchoolInfo(head,ID);
  236.     } while (1);
  237.     renewInfoFile(head,fp);
  238.     fclose(fp);
  239. }
  240. void printMenu() {
  241.     printf("Choose an option between 1 - 4\n");
  242.     printf("1 - Add New School(s)\n");
  243.     printf("2 - Change Information about a school\n");
  244.     printf("3 - Print the List of Schools based on number of students\n");
  245.     printf("4 - Print the List of Schools based on their Area\n");
  246.     printf("5 - Exit\n");
  247.     printf("Option Number: ");
  248. }
  249. int main() {
  250.     FILE *f;
  251.     node *head = NULL;
  252.     int option = 0;
  253.     if (!(f = fopen("text.dat", "rb"))) {
  254.         return 1;
  255.     }  
  256.     do {
  257.         printMenu(); fflush(stdin);
  258.         scanf(" %d", &option);
  259.         switch (option) {
  260.         case 1:
  261.             addSchool();
  262.             break;
  263.         case 2:
  264.             head = makeSrtLst(head, f);
  265.             changeInfo(head);
  266.             head = free_m(head);
  267.             break;
  268.         case 3:
  269.             head = makeSrtLst(head, f);
  270.             prt(head);
  271.             head = free_m(head);
  272.             break;
  273.         case 4:
  274.             head = makeSrtLst(head, f, 4);
  275.             prt(head);
  276.             head = free_m(head);
  277.             break;
  278.         default:
  279.             break;
  280.         }
  281.     } while (option > 0 && option < 5);
  282.     fclose(f);
  283.     return 0;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement