Advertisement
wa12rior

Untitled

Jun 16th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. struct Student {
  6.     char *imie;
  7.     char *nazwisko;
  8.     int nr_in;
  9. };
  10.  
  11. struct Node {
  12.     struct Node* next;
  13.     struct Student student;
  14. };
  15.  
  16. int show_menu() {
  17.     int choice;
  18.     printf("\n\n\t*] -------------------------------- [*");
  19.     printf("\n\n\t1. Wyswietl wszystkich studentow.\n");
  20.     printf("\t2. Dodanie nowego studenta.\n");
  21.     printf("\t3. Usuniecie studenta o podanym nr indeksu\n");
  22.     printf("\t4. Zakonczenie programu\n");
  23.     printf("\n\t*] -------------------------------- [*\n\n");
  24.     printf("\tTwoj wybor: ");
  25.     scanf("%d", &choice);
  26.     return choice;
  27. }
  28.  
  29. void remove_student() {
  30.     FILE *file = fopen("studenci.bin", "rb");
  31.     struct Node *head = NULL;
  32.    
  33.     int index;
  34.     printf("\n\tPodaj index: ");
  35.     scanf("%d", &index);
  36.    
  37.     struct Student student;
  38.     while(fread(&student, sizeof(struct Student), 1, file)) {
  39.         if (index == student.nr_in) {
  40.             continue;  
  41.         }
  42.        
  43.         do_listy(&head, student);
  44.        
  45.     }
  46.    
  47.     fclose(file);
  48.    
  49.     FILE *wrFile = fopen("studenci.bin", "wb");
  50.  
  51.     while (head != NULL) {
  52.      fwrite(&(head->student), sizeof(struct Student), 1, wrFile);
  53.      head = head->next;
  54.     }
  55.    
  56.     fclose(wrFile);
  57.  
  58. }
  59.  
  60. void add_student() {
  61.     FILE *file = fopen("studenci.bin", "ab");
  62.     struct Student student;
  63.     student.imie = (char *) malloc(sizeof(char));
  64.     student.nazwisko = (char *) malloc(sizeof(char));
  65.     printf("\n\tPodaj imie: ");
  66.     scanf("%s", student.imie);
  67.     printf("\n\tPodaj Nazwisko: ");
  68.     scanf("%s", student.nazwisko);
  69.     printf("\n\tPodaj index: ");
  70.     scanf("%d", &(student.nr_in));
  71.    
  72.     fwrite(&student, sizeof(struct Student), 1, file);
  73.     fclose(file);
  74. }
  75.  
  76. void print_students() {
  77.     FILE *file = fopen("studenci.bin", "rb");
  78.     struct Student student;
  79.    
  80.    
  81.     while(fread(&student, sizeof(struct Student), 1, file)) {
  82.         printf("\n\tImie: %s, Nazwisko: %s, Numer Indeksu: %d \n", student.imie, student.nazwisko, student.nr_in);
  83.     }
  84.    
  85.     fclose(file);
  86. }
  87.  
  88. void do_listy(struct Node** head_ref, struct Student student)
  89. {
  90.     struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
  91.  
  92.     struct Node *last = *head_ref;
  93.  
  94.     new_node->student  = student;
  95.  
  96.     new_node->next = NULL;
  97.  
  98.     if (*head_ref == NULL)
  99.     {
  100.        *head_ref = new_node;
  101.        return;
  102.     }  
  103.  
  104.     while (last->next != NULL)
  105.         last = last->next;
  106.  
  107.     last->next = new_node;
  108.     return;    
  109. }
  110.  
  111. int main(void) {
  112.    
  113.     int choice = show_menu();
  114.    
  115.     while (choice != 4) {
  116.         switch(choice) {
  117.             case 1:
  118.                 print_students();
  119.                 break;
  120.             case 2:
  121.                 add_student();
  122.                 break;
  123.             case 3:
  124.                 remove_student();
  125.                 break; 
  126.             default:
  127.                 break; 
  128.         }
  129.        
  130.         printf("\n");
  131.        
  132.         choice = show_menu();
  133.     }
  134.    
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement