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 <string.h>
- #define HEADOUTFORMAT "%-15s%-15s%-15s%-15s\n"
- #define OUTFORMAT "%-15s%-15s%-15d%-15d\n"
- typedef struct {
- char lastName[64], address[64];
- int bYear, dep;
- } 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 - LastName Address BirthYear Department):\n");
- for (i = 0; i < n; i++)
- {
- person *p = *persons;
- scanf("%s %s %d %d", p[i].lastName, p[i].address, &p[i].bYear, &p[i].dep);
- }
- fclose(file);
- file = fopen("input.bin", "ab");
- 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);
- 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; }
- if (!persons) return;
- putchar('\n'); printf(HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
- fprintf(file, HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
- for (i = 0; i < n; i++)
- {
- printf(OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
- fprintf(file, OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
- }
- fclose(file);
- }
- int dep(person *persons, int n)
- {
- int i = 0, c = 0;
- FILE *file = fopen("ageout.txt", "w");
- if (!file) { printf("File open error"); _getch(); return 0; }
- if (!persons) return -1;
- putchar('\n'); printf(HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
- fprintf(file, HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
- for (i = 0; i < n; i++)
- if (persons[i].dep == 1)
- {
- printf(OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
- fprintf(file, OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
- 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;
- person *p = NULL;
- n = input(&p);
- dep(p, n);
- p = read(n);
- p = psort(p, n);
- print(p, n);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment