hozer

RZLab2(files)

Jun 3rd, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <string.h>
  7.  
  8. #define HEADOUTFORMAT "%-15s%-15s%-15s%-15s%-10s\n"
  9. #define OUTFORMAT "%-15s%-15s%-15s%-15d%-10.3f\n"
  10.  
  11. typedef struct {
  12.     char firstName[64], lastName[64], education[64];
  13.     int bYear;
  14.     float salary;
  15. } person;
  16.  
  17. void print(person *persons, int n);
  18.  
  19. int input(person **persons)
  20. {
  21.     int n, i;
  22.     FILE *file = fopen("input.bin", "wb");
  23.     if (!file) { printf("File open error"); _getch(); return 0; }
  24.  
  25.     printf("input n: "); scanf("%d", &n);
  26.     *persons = (person*) malloc(sizeof(person) *n);
  27.  
  28.     printf("Input persons (by format - FirstName LastName Education BirthYear Salary):\n");
  29.     for (i = 0; i < n; i++)
  30.     {
  31.         person *p = *persons;
  32.         scanf("%s %s %s %d %f", p[i].firstName, p[i].lastName, p[i].education, &p[i].bYear, &p[i].salary);
  33.     }
  34.     fclose(file);
  35.     file = fopen("input.bin", "ab");
  36.     //fwrite(*persons, sizeof(person), n, file);
  37.     for (i = 0; i < n; i++)
  38.     {
  39.         fwrite(&(*persons)[i], sizeof(person), 1, file);
  40.     }
  41.  
  42.     fclose(file);
  43.     return n;
  44. }
  45.  
  46. person* read(int count)
  47. {
  48.     int i = 0;
  49.     FILE *file;
  50.     person *persons = NULL;
  51.     file = fopen("input.bin", "rb");
  52.  
  53.     if (!file) { printf("File open error"); _getch(); return 0; }
  54.  
  55.     persons = (person*) malloc(sizeof(person) *count);
  56.     //if (fread(&persons, sizeof(person), count, file) == 0) return NULL;
  57.     while (!feof(file))
  58.     {
  59.         fread(&persons[i], sizeof(person), 1, file);
  60.         i++;
  61.     }
  62.  
  63.  
  64.     fclose(file);
  65.     return persons;
  66. }
  67.  
  68.  
  69. void print(person* persons, int n)
  70. {
  71.     int i = 0;
  72.     FILE *file = fopen("output.txt", "w");
  73.     if (!file) { printf("File open error"); _getch(); return 0; }
  74.  
  75.     if (!persons) return;
  76.     putchar('\n'); printf(HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
  77.     fprintf(file, HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
  78.     for (i = 0; i < n; i++)
  79.     {
  80.         printf(OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
  81.         fprintf(file, OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
  82.     }
  83.  
  84.     fclose(file);
  85. }
  86.  
  87.  
  88. int age(person *persons, int n, int dage, int uage)
  89. {
  90.     int i = 0, c = 0;
  91.     time_t t = time(NULL);
  92.     struct tm *now = localtime(&t);
  93.     FILE *file = fopen("ageout.txt", "w");
  94.     if (!file) { printf("File open error"); _getch(); return 0; }
  95.  
  96.     if (!persons) return -1;
  97.     putchar('\n'); printf(HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
  98.     fprintf(file, HEADOUTFORMAT, "Name", "Surname", "Education", "Birth Year", "Salary");
  99.     for (i = 0; i < n; i++)
  100.         if (now->tm_year + 1900 - persons[i].bYear >= dage && now->tm_year + 1900 - persons[i].bYear <= uage)
  101.         {
  102.             printf(OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
  103.             fprintf(file, OUTFORMAT, persons[i].firstName, persons[i].lastName, persons[i].education, persons[i].bYear, persons[i].salary);
  104.             c++;
  105.         }
  106.  
  107.     fclose(file);
  108.     return c;
  109. }
  110.  
  111. person *psort(person *p, int n)
  112. {
  113.     int i, j; person tmp;
  114.     for (i = 0; i < n; i++)
  115.     {
  116.         for (j = i + 1; j < n; j++)
  117.         {
  118.             if (strcmp(p[i].lastName, p[j].lastName) > 0)
  119.             {
  120.                 tmp = p[i];
  121.                 p[i] = p[j];
  122.                 p[j] = tmp;
  123.             }
  124.         }
  125.     }
  126.  
  127.     return p;
  128. }
  129.  
  130. int main()
  131. {
  132.     int n = 1, minage, maxage;
  133.     person *p = NULL;
  134.     printf("Input lower age limit: ");
  135.     scanf("%d", &minage);
  136.     printf("Input upper age limit: ");
  137.     scanf("%d", &maxage);
  138.  
  139.     n = input(&p);
  140.     printf("\nAgeFunc:");
  141.     age(p, n, minage, maxage);
  142.     p = read(n);
  143.     p = psort(p, n);
  144.     printf("\nSortFunc:");
  145.     print(p, n);
  146.  
  147.     _getch();
  148.     return 0;
  149. }
  150.  
  151. /*Ввести з клавіатури масив з N записів. Кожен запис повинен містити такі поля: прізвище,
  152. ім'я, рік народження,   освіта, зарплата. Вивести масив на  екран, впорядкувавши рядки   таблиці  за
  153. прізвищами. Обчислити кількість осіб з таблиці віком від 20 до 40 років.*/
Advertisement
Add Comment
Please, Sign In to add comment