Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include<bits\stdc++.h>
  2.  
  3. typedef struct stud
  4. {
  5.     char *first_name, *last_name;
  6.     int year, month, day;
  7.     double av_grade;
  8.     struct stud *next;
  9. };
  10.  
  11.  
  12. int main()
  13. {
  14.     stud *root = NULL;
  15.     stud *cur, *cur2;
  16.     root = NULL;
  17.     char file_name[128];
  18.     char first_name[128], last_name[128], year[128], month [128], day [128], av_grade[128], new_file_name[128];
  19.     int year_n, month_n, day_n;
  20.     double av_grade_n;
  21.     int action;
  22.  
  23.     while(1)
  24.     {
  25.         printf("1: Read the source database\n");
  26.         printf("2: Put base into file\n");
  27.         printf("3: Print the base into the file\n");
  28.         printf("4: Add student from file\n");
  29.         printf("5: Add student from keyboard\n");
  30.         printf("6: Delete student from the base\n");
  31.         printf("7: Search student in the base by his surname\n");
  32.         scanf("%d", &action);
  33.         switch(action)
  34.         {
  35.             case 1:
  36.                 printf("Please enter the name of the file with source data base.\n");
  37.                 scanf ("%s", file_name);
  38.                 FILE *p;
  39.                 if ((p = fopen(file_name, "rb")) == NULL)
  40.                 {
  41.                      printf("Sorry, can not open the file:(\n");
  42.                      exit(1);
  43.                 }
  44.                 while(fgets(last_name,  128, p) != NULL)
  45.                     {
  46.                         fgets(last_name, 128, p);
  47.                         fgets(first_name, 128, p);
  48.                         fgets(year, 128, p);
  49.                         year_n = atoi(year);
  50.                         fgets(month, 128, p);
  51.                         month_n = atoi(month);
  52.                         fgets(day, 128, p);
  53.                         day_n = atoi(day);
  54.                         fgets(av_grade,  128, p);
  55.                         av_grade_n = atof(av_grade);
  56.                         if(!root)
  57.                         {
  58.                             root = (stud *) malloc(sizeof(stud));
  59.                             root -> first_name = first_name;
  60.                             root -> last_name = last_name;
  61.                             root -> year = year_n;
  62.                             root -> month = month_n;
  63.                             root -> day = day_n;
  64.                             root -> av_grade = av_grade_n;
  65.                             root -> next = NULL;
  66.                         }
  67.                         for(cur = root; cur -> next; cur = cur -> next);
  68.                             cur -> next = (stud *) malloc(sizeof(stud));
  69.                             cur = cur -> next;
  70.                             cur -> first_name = first_name;
  71.                             cur -> last_name = last_name;
  72.                             cur -> year = year_n;
  73.                             cur -> month = month_n;
  74.                             cur -> day = day_n;
  75.                             cur -> av_grade = av_grade_n;
  76.                             cur -> next = NULL;
  77.                         }
  78.             break;
  79.             case 2:
  80.             cur = root;
  81.             printf("Enter filename with file extension, where you need to put structure\n");
  82.             scanf("%s", &file_name);
  83.             p = fopen(file_name, "wb");
  84.             while (cur != NULL)
  85.             {
  86.                 fprintf(p,"%s", cur -> last_name);
  87.                 fprintf(p,"%s", cur -> first_name);
  88.                 fprintf(p,"%d", cur -> year);
  89.                 fprintf(p,"%d", cur -> month);
  90.                 fprintf(p,"%d", cur -> day);
  91.                 fprintf(p,"%f", cur -> av_grade);
  92.                 cur = cur -> next;
  93.             }
  94.                 fclose (p);
  95.             break;
  96.  
  97.         }
  98.  
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement