Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // UchilishtaSvurzanSpisuk.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //School Struct
- typedef struct {
- char name[50];
- char directorName[30];
- int ID,children;
- int area;
- }School;
- //Node
- typedef struct node {
- School school;
- struct node *next;//Points to the next node
- }node;
- node *makeSrtLst(node *head, FILE *f,int option = 0) {
- rewind(f);
- node *p, *crnt, *prev;
- do {
- p = (node *)malloc(sizeof(node)); /* if p==0 */
- p->next = NULL;
- if (!fread(&(p->school), sizeof(School), 1, f)) {
- free(p);
- break;
- }
- /* find the correct place sorted by n */
- if (option != 4) {
- for (prev = NULL, crnt = head;
- crnt && (crnt->school.children > p->school.children);
- prev = crnt, crnt = crnt->next);
- }
- else {
- for (prev = NULL, crnt = head;
- crnt && (crnt->school.area > p->school.area);
- prev = crnt, crnt = crnt->next);
- }
- // put in the list
- if (prev) {
- prev->next = p;
- }
- else {
- head = p;
- }
- p->next = crnt;
- /* or put directly in the head of the list no sort
- p->next=head;
- head = p;
- */
- } while (1);
- return head;
- }
- //Print the given node
- void prt(node *crnt) {
- while (crnt) {
- printf("Next School Information:\n");
- printf("School Name: %s\n", crnt->school.name);
- printf("School ID: %d\n", crnt->school.ID);
- printf("School Students: %d\n", crnt->school.children);
- printf("School Area: %d\n", crnt->school.area);
- printf("School director's name: %s\n", crnt->school.directorName);
- crnt = crnt->next;
- }
- }
- //Free Memory
- node *free_m(node *crnt) {
- node *next;
- printf("Memory free\n");
- while (crnt) {
- next = crnt->next; free(crnt); crnt = next;
- }
- return NULL;
- }
- //Validation(If The given ID is uniqe returns 1)
- int validateID(node *head, int ID) {
- node *crnt;
- crnt = head;
- while (crnt) {
- if (crnt->school.ID == ID) {
- printf("zashto");
- return 0;
- }
- crnt = crnt->next;
- }
- return 1;
- }
- //Create a new binary file and save the input information
- void saveInfoInFile(FILE *f) {
- School s;
- node *head = NULL;
- do {
- head = makeSrtLst(head, f);
- printf("Next School name (Put '*' To end):");
- scanf(" %49[^\n]", s.name);
- if (!strcmp(s.name, "*")) {
- break;
- }
- printf("%s School ID: ", s.name); fflush(stdin);
- scanf(" %d", &s.ID);
- //Check if the ID is unique
- while (!validateID(head,s.ID)) {
- printf("The ID of the schools should be uniqe\n: ");
- printf("%s School ID: ", s.name); fflush(stdin);
- scanf("%d", &s.ID);
- }
- printf("%d %s Number of students: ", s.ID, s.name); fflush(stdin);
- scanf(" %d", &(s.children));
- //The students must be more than 0
- while (s.children < 0) {
- printf("The children should be >= 0\n: ");
- printf("%s - Number of Students: ", s.name); fflush(stdin);
- scanf(" %d", &s.children);
- }
- printf("%d %s School Area:", s.ID, s.name); fflush(stdin);
- scanf(" %d", &(s.area));
- while (s.area <= 50) {
- printf("The area should be > 50\n: ");
- printf("%s - Area : ", s.name); fflush(stdin);
- scanf(" %d", &s.area);
- }
- printf("%d %s School director's name:", s.ID, s.name); fflush(stdin);
- scanf(" %29[^\n]", s.directorName); fflush(stdin);
- //Write the structure into the file
- fwrite(&s, sizeof(School), 1, f);
- printf("Writing the structure in the file\n");
- } while (1);
- head = free_m(head);
- printf("The file is created\n");
- }
- //Change School Information
- void renewInfoFile(node *crnt,FILE *fp) {
- School s;
- while (crnt) {
- s = crnt->school;
- fwrite(&s, sizeof(School), 1, fp);
- crnt = crnt->next;
- }
- }
- node *changeSchoolInfo(node *crnt, int ID) {
- node *head = NULL, *prev;
- int choice = 0, tmpID;
- head = crnt;
- prev = crnt;
- while (crnt) {
- if (crnt->school.ID == ID) {
- do {
- printf("School Information:\n");
- printf("School Name: %s\n", crnt->school.name);
- printf("School ID: %d\n", crnt->school.ID);
- printf("School Students: %d\n", crnt->school.children);
- printf("School Area: %d\n", crnt->school.area);
- printf("School director's name: %s\n", crnt->school.directorName);
- 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);
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("New School Name:"); fflush(stdin);
- scanf(" %49[^\n]", crnt->school.name);
- break;
- case 2:
- printf("New School ID:"); fflush(stdin);
- scanf(" %d", &tmpID);
- while ((!validateID(head, tmpID)) ) {
- if (tmpID == crnt->school.ID) {
- break;
- }
- printf("The ID of the schools should be uniqe\n: "); fflush(stdin);
- scanf("%d", &tmpID);
- }
- crnt->school.ID = tmpID;
- break;
- case 3:
- printf("New Number of students:"); fflush(stdin);
- scanf(" %d", &crnt->school.children);
- if(crnt->school.children <= 0) {
- prev->next = crnt->next;
- return head;
- }
- break;
- case 4:
- printf("New School Area:"); fflush(stdin);
- scanf(" %d", &crnt->school.area);
- break;
- case 5:
- printf("New Director's Name:"); fflush(stdin);
- scanf(" %29[^\n]", crnt->school.directorName);
- break;
- default:
- break;
- }
- } while (choice > 0 && choice < 6);
- break;
- }
- prev = crnt;
- crnt = crnt->next;
- }
- prt(head);
- return head;
- }
- //Add school to the file
- void addSchool() {
- FILE *fp;
- School s;
- fp = fopen("text.dat", "ab+");
- if (!fp) {
- return;
- }
- saveInfoInFile(fp);
- fclose(fp);
- }
- //Change information about the school
- void changeInfo(node *head) {
- FILE *fp;
- School s;
- fp = fopen("text.dat", "wb");
- if (!fp) {
- return;
- }
- int choice = 0, ID = 0, value = 0;
- do {
- printf("School's ID which has to be updated");
- printf("\nVuvedete 0 za izhod: "); fflush(stdin);
- scanf(" %d", &ID);
- if (ID == 0) {
- break;
- }
- printf("\nstigame li do tuk");
- head = changeSchoolInfo(head,ID);
- } while (1);
- renewInfoFile(head,fp);
- fclose(fp);
- }
- void printMenu() {
- printf("Choose an option between 1 - 4\n");
- printf("1 - Add New School(s)\n");
- printf("2 - Change Information about a school\n");
- printf("3 - Print the List of Schools based on number of students\n");
- printf("4 - Print the List of Schools based on their Area\n");
- printf("5 - Exit\n");
- printf("Option Number: ");
- }
- int main() {
- FILE *f;
- node *head = NULL;
- int option = 0;
- if (!(f = fopen("text.dat", "rb"))) {
- return 1;
- }
- do {
- printMenu(); fflush(stdin);
- scanf(" %d", &option);
- switch (option) {
- case 1:
- addSchool();
- break;
- case 2:
- head = makeSrtLst(head, f);
- changeInfo(head);
- head = free_m(head);
- break;
- case 3:
- head = makeSrtLst(head, f);
- prt(head);
- head = free_m(head);
- break;
- case 4:
- head = makeSrtLst(head, f, 4);
- prt(head);
- head = free_m(head);
- break;
- default:
- break;
- }
- } while (option > 0 && option < 5);
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement