Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct {
- char firstName[64], lastName[64], education[64];
- int bYear;
- float salary;
- } person;
- void print(person *persons, int n);
- int input(person **persons)
- {
- int n, i;
- FILE *file = fopen("input.bin", "wb");
- if (!file) { printf("File open error"); _getch(); return 0; }
- printf("input n: "); scanf("%d", &n);
- *persons = (person*) malloc(sizeof(person) *n);
- printf("Input persons (by format - FirstName LastName Education BirthYear Salary):\n");
- for (i = 0; i < n; i++)
- {
- person *p = *persons;
- scanf("%s %s %s %d %f", p[i].firstName, p[i].lastName, p[i].education, &p[i].bYear, &p[i].salary);
- }
- fclose(file);
- file = fopen("input.bin", "ab");
- fwrite(*persons, sizeof(person), n, file);
- fclose(file);
- return n;
- }
- person* read(int count)
- {
- int i = 0;
- FILE *file;
- person *persons = NULL;
- file = fopen("input.bin", "rb");
- if (!file) { printf("File open error"); _getch(); return 0; }
- persons = (person*) malloc(sizeof(person) *count);
- if (fread(&persons, sizeof(person), count, file) == 0) return NULL;
- fclose(file);
- return persons;
- }
- void print(person* persons, int n)
- {
- int i = 0;
- if (!persons) return;
- printf("Name\tSurname\tEducation\tBirth Year\tSalary\n");
- for (i = 0; i < n; i++)
- printf("%s\t%s\t%s\t\t%d\t%.3f\t\n", persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
- }
- int old(person *persons, int n, int dlim, int ulim)
- {
- int i = 0, c = 0;
- time_t t = time(NULL);
- struct tm *now = localtime(&t);
- if (!persons) return -1;
- for (i = 0; i < n; i++)
- if (now->tm_year + 1900 - persons[i].bYear>= 20 && now->tm_year + 1900 - persons[i].bYear <= 40)
- c++;
- return c;
- }
- int main()
- {
- int n = 1;
- person *p = NULL;
- n = input(&p);
- print(p, n);
- printf("%d ", old(p, n, 20, 40));
- p = read(n);
- print(p, n);
- _getch();
- return 0;
- }
- /*Ввести з клавіатури масив з N записів. Кожен запис повинен містити такі поля: прізвище,
- ім'я, рік народження, освіта, зарплата. Вивести масив на екран, впорядкувавши рядки таблиці за
- прізвищами. Обчислити кількість осіб з таблиці віком від 20 до 40 років.*/
Advertisement
Add Comment
Please, Sign In to add comment