Advertisement
JewishCat

lab9

Apr 10th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define N 10
  6.  
  7. struct Student {
  8.     char name [20];
  9.     char surname [40];
  10.     char sexist [10];
  11.     int age;
  12. };
  13.  
  14. void SearchByName (struct Student* st, int size, const char* Name);
  15. void SearchByAge (struct Student* st, int size, int age);
  16.  
  17. int main()
  18. {
  19.     FILE* f;
  20.     int request, i;
  21.     struct Student student[N];
  22.    
  23.     if ((f = fopen("student1.txt", "r")) == NULL)
  24.     {
  25.         printf("Error while opening student.txt/n");
  26.         return 1;    
  27.     }
  28.    
  29.     for(i = 0; i < N; ++i)
  30.     {
  31.         fgets(student[i].name, 20, f);
  32.         fgets(student[i].surname, 40, f);
  33.         fgets(student[i].sexist, 10, f);
  34.         fscanf(f, "%d", &student[i].age);
  35.        
  36.     }
  37.     fclose(f);
  38.    
  39.     while (request != 5)
  40.     {
  41.         printf("\nenter request:\n1-seach Elena\n 2-seach age\n");
  42.         scanf("%d", &request);
  43.         switch(request)
  44.         {
  45.         case 1:
  46.             SearchByName(student, N, "Elena");
  47.             break;
  48.         case 2:
  49.             SearchByAge(student, N, 19);
  50.             break;
  51.         case 3:
  52.             return 0;
  53.         default:
  54.             printf("\nError");
  55.         }
  56.     }
  57.     getchar();
  58. }
  59.  
  60. void SearchByName(Student* arr, int size, const char* name)
  61. {
  62.     int i, count = 0;
  63.     for (i = 0; i < size; ++i)
  64.     {
  65.         if (!strcmp(arr[i].name, name))
  66.         {
  67.             printf("\n%s\t%s\t%s\t%d", arr[i].name
  68.                                      , arr[i].surname
  69.                                      , arr[i].sexist
  70.                                      , arr[i].age);
  71.         }
  72.     }
  73. }
  74.  
  75. void SearchByAge(Student* arr, int size, int age)
  76. {
  77.     int i, count = 0;
  78.     for (i = 0; i < size; ++i)
  79.     {
  80.         if (arr[i].age == age)
  81.         {
  82.             printf("\n%s\t%s\t%s\t%d", arr[i].name
  83.                                      , arr[i].surname
  84.                                      , arr[i].sexist
  85.                                      , arr[i].age);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement