hozer

Ura_K1SM2MOD2Lab4

Jun 3rd, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define HEADOUTFORMAT "%-15s%-15s%-15s%-15s\n"
  8. #define OUTFORMAT "%-15s%-15s%-15d%-15d\n"
  9.  
  10. typedef struct {
  11.     char lastName[64], address[64];
  12.     int bYear, dep;
  13. } person;
  14.  
  15. void print(person *persons, int n);
  16.  
  17. int input(person **persons)
  18. {
  19.     int n, i;
  20.     FILE *file = fopen("input.bin", "wb");
  21.     if (!file) { printf("File open error"); _getch(); return 0; }
  22.  
  23.     printf("input n: "); scanf("%d", &n);
  24.     *persons = (person*) malloc(sizeof(person) *n);
  25.  
  26.     printf("Input persons (by format - LastName Address BirthYear Department):\n");
  27.     for (i = 0; i < n; i++)
  28.     {
  29.         person *p = *persons;
  30.         scanf("%s %s %d %d", p[i].lastName, p[i].address, &p[i].bYear, &p[i].dep);
  31.     }
  32.     fclose(file);
  33.     file = fopen("input.bin", "ab");
  34.     for (i = 0; i < n; i++)
  35.     {
  36.         fwrite(&(*persons)[i], sizeof(person), 1, file);
  37.     }
  38.  
  39.     fclose(file);
  40.     return n;
  41. }
  42.  
  43. person* read(int count)
  44. {
  45.     int i = 0;
  46.     FILE *file;
  47.     person *persons = NULL;
  48.     file = fopen("input.bin", "rb");
  49.  
  50.     if (!file) { printf("File open error"); _getch(); return 0; }
  51.  
  52.     persons = (person*) malloc(sizeof(person) *count);
  53.     while (!feof(file))
  54.     {
  55.         fread(&persons[i], sizeof(person), 1, file);
  56.         i++;
  57.     }
  58.  
  59.  
  60.     fclose(file);
  61.     return persons;
  62. }
  63.  
  64.  
  65. void print(person* persons, int n)
  66. {
  67.     int i = 0;
  68.     FILE *file = fopen("output.txt", "w");
  69.     if (!file) { printf("File open error"); _getch(); return; }
  70.  
  71.     if (!persons) return;
  72.     putchar('\n'); printf(HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
  73.     fprintf(file, HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
  74.     for (i = 0; i < n; i++)
  75.     {
  76.         printf(OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
  77.         fprintf(file, OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
  78.     }
  79.  
  80.     fclose(file);
  81. }
  82.  
  83.  
  84. int dep(person *persons, int n)
  85. {
  86.     int i = 0, c = 0;
  87.     FILE *file = fopen("ageout.txt", "w");
  88.     if (!file) { printf("File open error"); _getch(); return 0; }
  89.  
  90.     if (!persons) return -1;
  91.     putchar('\n'); printf(HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
  92.     fprintf(file, HEADOUTFORMAT, "Surname", "Address", "Birth Year", "Department");
  93.     for (i = 0; i < n; i++)
  94.         if (persons[i].dep == 1)
  95.         {
  96.             printf(OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
  97.             fprintf(file, OUTFORMAT, persons[i].lastName, persons[i].address, persons[i].bYear, persons[i].dep);
  98.             c++;
  99.         }
  100.  
  101.     fclose(file);
  102.     return c;
  103. }
  104.  
  105. person *psort(person *p, int n)
  106. {
  107.     int i, j; person tmp;
  108.     for (i = 0; i < n; i++)
  109.     {
  110.         for (j = i + 1; j < n; j++)
  111.         {
  112.             if (strcmp(p[i].lastName, p[j].lastName) > 0)
  113.             {
  114.                 tmp = p[i];
  115.                 p[i] = p[j];
  116.                 p[j] = tmp;
  117.             }
  118.         }
  119.     }
  120.  
  121.     return p;
  122. }
  123.  
  124. int main()
  125. {
  126.     int n = 1;
  127.     person *p = NULL;
  128.  
  129.     n = input(&p);
  130.     dep(p, n);
  131.     p = read(n);
  132.     p = psort(p, n);
  133.     print(p, n);
  134.  
  135.     _getch();
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment