hozer

file

May 28th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 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.  
  7. typedef struct {
  8.     char firstName[64], lastName[64], education[64];
  9.     int bYear;
  10.     float salary;
  11. } person;
  12.  
  13. void print(person *persons, int n);
  14.  
  15. int input(person **persons)
  16. {
  17.     int n, i;
  18.     FILE *file = fopen("input.bin", "wb");
  19.     if (!file) { printf("File open error"); _getch(); return 0; }
  20.  
  21.     printf("input n: "); scanf("%d", &n);
  22.     *persons = (person*) malloc(sizeof(person) *n);
  23.  
  24.     printf("Input persons (by format - FirstName LastName Education BirthYear Salary):\n");
  25.     for (i = 0; i < n; i++)
  26.     {
  27.         person *p = *persons;
  28.         scanf("%s %s %s %d %f", p[i].firstName, p[i].lastName, p[i].education, &p[i].bYear, &p[i].salary);
  29.     }
  30.     fclose(file);
  31.     file = fopen("input.bin", "ab");
  32.     fwrite(*persons, sizeof(person), n, file);
  33.  
  34.     fclose(file);
  35.     return n;
  36. }
  37.  
  38. person* read(int count)
  39. {
  40.     int i = 0;
  41.     FILE *file;
  42.     person *persons = NULL;
  43.     file = fopen("input.bin", "rb");
  44.  
  45.     if (!file) { printf("File open error"); _getch(); return 0; }
  46.    
  47.     persons = (person*) malloc(sizeof(person) *count);
  48.     if (fread(&persons, sizeof(person), count, file) == 0) return NULL;
  49.  
  50.  
  51.     fclose(file);
  52.     return persons;
  53. }
  54.  
  55.  
  56. void print(person* persons, int n)
  57. {
  58.     int i = 0;
  59.     if (!persons) return;
  60.     printf("Name\tSurname\tEducation\tBirth Year\tSalary\n");
  61.     for (i = 0; i < n; i++)
  62.         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);
  63. }
  64.  
  65.  
  66. int old(person *persons, int n, int dlim, int ulim)
  67. {
  68.     int i = 0, c = 0;
  69.     time_t t = time(NULL);
  70.     struct tm *now = localtime(&t);
  71.     if (!persons) return -1;
  72.  
  73.     for (i = 0; i < n; i++)
  74.         if (now->tm_year + 1900 - persons[i].bYear>= 20 && now->tm_year + 1900 - persons[i].bYear <= 40)
  75.             c++;
  76.  
  77.     return c;
  78. }
  79.  
  80. int main()
  81. {
  82.     int n = 1;
  83.     person *p = NULL;
  84.     n = input(&p);
  85.     print(p, n);
  86.     printf("%d ", old(p, n, 20, 40));
  87.     p = read(n);
  88.     print(p, n);
  89.     _getch();
  90.     return 0;
  91. }
  92. /*Ввести з клавіатури масив з N записів. Кожен запис повинен містити такі поля: прізвище,
  93. ім'я, рік народження,   освіта, зарплата. Вивести масив на  екран, впорядкувавши рядки   таблиці  за
  94. прізвищами. Обчислити кількість осіб з таблиці віком від 20 до 40 років.*/
Advertisement
Add Comment
Please, Sign In to add comment