Advertisement
JewishCat

lab9

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