Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab9.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define N 10
- #pragma warning(disable : 4996) // Отключение предупреждения компилятора. Ругается на то, что функция strncpy может зайти за границу массива, хотя тут все границы ясные.
- struct Student {
- char name[20];
- char surname[40];
- char sexist[10];
- int age;
- };
- void SearchByName(struct Student* st, int size, const char* Name);
- void SearchByAge(struct Student* st, int size, int age);
- int main()
- {
- FILE* f;
- int request, i;
- struct Student student[N];
- if ((f = fopen("student1.txt", "r")) == NULL)
- {
- printf("Error while opening student.txt/n");
- return 1;
- }
- for (i = 0; i < N; ++i)
- {
- fgets(student[i].name, 20, f);
- fgets(student[i].surname, 40, f);
- fgets(student[i].sexist, 10, f);
- fscanf(f, "%d", &student[i].age);
- }
- fclose(f);
- printf("\nenter request:\n1-seach Elena\n 2-seach age\n");
- scanf("%d", &request);
- while (request != 5)
- {
- switch (request)
- {
- case 1:
- SearchByName(student, N, "Elena");
- break;
- case 2:
- SearchByAge(student, N, 19);
- break;
- case 3:
- return 0;
- default:
- printf("\nError");
- }
- }
- getchar();
- }
- void SearchByName(Student* arr, int size, const char* name)
- {
- int i, count = 0;
- for (i = 0; i < size; ++i)
- {
- if (!strcmp(arr[i].name, name))
- {
- printf("\n%s\t%s\t%s\t%d", arr[i].name
- , arr[i].surname
- , arr[i].sexist
- , arr[i].age);
- }
- }
- }
- void SearchByAge(Student* arr, int size, int age)
- {
- int i, count = 0;
- for (i = 0; i < size; ++i)
- {
- if (arr[i].age == age)
- {
- printf("\n%s\t%s\t%s\t%d", arr[i].name
- , arr[i].surname
- , arr[i].sexist
- , arr[i].age);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement