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>
- #include <string.h>
- #define HEADOUTFORMAT "%-15s%-15s%-15s%-15s%-10s\n"
- #define OUTFORMAT "%-15s%-15s%-15s%-15d%-10.3f\n"
- 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);
- for (i = 0; i < n; i++)
- {
- fwrite(&(*persons)[i], sizeof(person), 1, 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;
- while (!feof(file))
- {
- fread(&persons[i], sizeof(person), 1, file);
- i++;
- }
- fclose(file);
- return persons;
- }
- void print(person* persons, int n)
- {
- int i = 0;
- FILE *file = fopen("output.txt", "w");
- if (!file) { printf("File open error"); _getch(); return 0; }
- if (!persons) return;
- putchar('\n'); printf(HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
- fprintf(file, HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
- for (i = 0; i < n; i++)
- {
- printf(OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
- fprintf(file, OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
- }
- fclose(file);
- }
- int age(person *persons, int n, int dage, int uage)
- {
- int i = 0, c = 0;
- time_t t = time(NULL);
- struct tm *now = localtime(&t);
- FILE *file = fopen("ageout.txt", "w");
- if (!file) { printf("File open error"); _getch(); return 0; }
- if (!persons) return -1;
- putchar('\n'); printf(HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
- fprintf(file, HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
- for (i = 0; i < n; i++)
- if (now->tm_year + 1900 - persons[i].bYear >= dage && now->tm_year + 1900 - persons[i].bYear <= uage)
- {
- printf(OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
- fprintf(file, OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
- c++;
- }
- fclose(file);
- return c;
- }
- person *psort(person *p, int n)
- {
- int i, j; person tmp;
- for (i = 0; i < n; i++)
- {
- for (j = i + 1; j < n; j++)
- {
- if (strcmp(p[i].lastName, p[j].lastName) > 0)
- {
- tmp = p[i];
- p[i] = p[j];
- p[j] = tmp;
- }
- }
- }
- return p;
- }
- int main()
- {
- int n = 1, minage, maxage;
- person *p = NULL;
- printf("Input lower age limit: ");
- scanf("%d", &minage);
- printf("Input upper age limit: ");
- scanf("%d", &maxage);
- n = input(&p);
- printf("\nAgeFunc:");
- age(p, n, minage, maxage);
- p = read(n);
- p = psort(p, n);
- printf("\nSortFunc:");
- print(p, n);
- _getch();
- return 0;
- }
- /*Ввести з клавіатури масив з N записів. Кожен запис повинен містити такі поля: прізвище,
- ім'я, рік народження, освіта, зарплата. Вивести масив на екран, впорядкувавши рядки таблиці за
- прізвищами. Обчислити кількість осіб з таблиці віком від 20 до 40 років.*/
Advertisement
Add Comment
Please, Sign In to add comment