Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. struct students {
  6.     int flag, age;
  7.     float GradePointAverage;
  8.     char *surname, *name;
  9.     union {
  10.         char *region, *city, *street;
  11.         int house, flat;
  12.     };
  13. };
  14. char* EnterInformation(char* st) {
  15.     int i = 0, sch = 0;
  16.     while (1) {
  17.         st[i++] = (char)getchar();
  18.         if (st[i - 1] == '\n') {
  19.             st[i - 1] = '\0';
  20.             break;
  21.         }
  22.         if (!(st = (char*)realloc(st, i + 2)))
  23.             st = (char*)realloc(st, i + 2);
  24.     }
  25.     i = 0;
  26.     if (st[0] == '\0') {
  27.         printf("Incorrect information,reenter!\n");
  28.         rewind(stdin);
  29.         EnterInformation(st);
  30.     }
  31.     while (st[i] != '\0') {
  32.         if (!(st[i] >= 'a' && st[i] <= 'z') && !(st[i] >= 'A' && st[i] <= 'Z'))
  33.             sch++;
  34.         i++;
  35.     }
  36.     if (sch > 0) {
  37.         printf("Incorrect data,reenter!\n");
  38.         rewind(stdin);
  39.         EnterInformation(st);
  40.     }
  41.     return st;
  42. }
  43. students *GetStudents(int &n) {
  44.     int i, j = 0, typeofinformation;
  45.     students *p;
  46.     if (!(p = (struct students*)malloc(sizeof(students)*n)))
  47.         p = (students*)realloc(p, n + 1);
  48.     for (i = 0; i < n; i++) {
  49.         if (!(p[i].surname = (char*)malloc(sizeof(char))))
  50.             p[i].surname = (char*)realloc(p[i].surname, 2);
  51.         if (!(p[i].name = (char*)malloc(sizeof(char))))
  52.             p[i].name = (char*)realloc(p[i].name, 2);
  53.         printf("Enter student surname\n");
  54.         rewind(stdin);
  55.         p[i].surname = EnterInformation(p[i].surname);
  56.         printf("Enter student name\n");
  57.         rewind(stdin);
  58.         p[i].name = EnterInformation(p[i].name);
  59.         printf("Enter age of student\n");
  60.         while (!(scanf_s("%d", &p[i].age)) || p[i].age <= 0 || p[i].age > 113) {
  61.             rewind(stdin);
  62.             printf("Incorrect age,reenter!\n");
  63.         }
  64.         printf("Enter student grade point average\n");
  65.         while (!(scanf_s("%f", &p[i].GradePointAverage)) || p[i].GradePointAverage <= 0 || p[i].GradePointAverage > 10) {
  66.             rewind(stdin);
  67.             printf("Incorrect grade point average,reenter!\n");
  68.         }
  69.         printf("What type of information you prefer to put?\n1.Refion\n2.City\n3.Street\n4.House\n5.Flat\n");
  70.         while (!(scanf_s("%d", &typeofinformation)) || typeofinformation <= 0 || typeofinformation > 5) {
  71.             rewind(stdin);
  72.             printf("Incorrect number,reenter!\n");
  73.         }
  74.         rewind(stdin);
  75.         switch (typeofinformation) {
  76.         case 1: {
  77.             printf("Enter region where the student live\n");
  78.             if (!(p[i].region = (char*)malloc(sizeof(char))))
  79.                 p[i].region = (char*)realloc(p[i].region, 2);
  80.             p[i].region = EnterInformation(p[i].name);
  81.             p[i].flag = 1;
  82.             break;
  83.         }
  84.         case 2: {
  85.             printf("Enter city where the student live\n");
  86.             if (!(p[i].city = (char*)malloc(sizeof(char))))
  87.                 p[i].city = (char*)realloc(p[i].city, 2);
  88.             p[i].city = EnterInformation(p[i].city);
  89.             p[i].flag = 2;
  90.             break;
  91.         }
  92.         case 3: {
  93.             printf("Enter street where the student live\n");
  94.             if (!(p[i].street = (char*)malloc(sizeof(char))))
  95.                 p[i].street = (char*)realloc(p[i].street, 2);
  96.             p[i].street = EnterInformation(p[i].street);
  97.             p[i].flag = 3;
  98.             break;
  99.         }
  100.         case 4: {
  101.             printf("Enter number of house where the student live\n");
  102.             while (!(scanf_s("%d", &p[i].house)) || p[i].house <= 0) {
  103.                 rewind(stdin);
  104.                 printf("Incorrect number of house,reenter\n");
  105.             }
  106.             p[i].flag = 4;
  107.             break;
  108.         }
  109.         case 5: {
  110.             printf("Enter number of flat where the student live\n");
  111.             while (!(scanf_s("%d", &p[i].flat)) || p[i].flat <= 0) {
  112.                 rewind(stdin);
  113.                 printf("Incorrect number of flat,reenter\n");
  114.             }
  115.             p[i].flag = 5;
  116.             break;
  117.         }
  118.         }
  119.     }
  120.     return p;
  121. }
  122. int GetDefiner(int &definer, char** argv) {
  123.     if (!(strcmp(argv[1], "Street")) || !(strcmp(argv[1], "street"))) {
  124.         return 3;
  125.     }
  126.     if (!(strcmp(argv[1], "Region")) || !(strcmp(argv[1], "region"))) {
  127.         return 1;
  128.     }
  129.     if (!(strcmp(argv[1], "City")) || !(strcmp(argv[1], "city"))) {
  130.         return 2;
  131.     }
  132.     if (!(strcmp(argv[1], "House")) || !(strcmp(argv[1], "house"))) {
  133.         return 4;
  134.     }
  135.     if (!(strcmp(argv[1], "Flat")) || !(strcmp(argv[1], "flat"))) {
  136.         return 5;
  137.     }
  138.     else return 0;
  139. }
  140. int SearchInformationAboutStudents(int p, int definer) {
  141.     if (p == definer)
  142.         return 1;
  143.     else return 0;
  144. }
  145. void ShowInformationAboutStudents(students p, int definer) {
  146.     if (definer == 1) {
  147.         printf("------------------\n");
  148.         printf("Surname : %s\n", p.surname);
  149.         printf("Name : %s\n", p.name);
  150.         printf("Age : %d\n", p.age);
  151.         printf("Grade point average : %.1f\n", p.GradePointAverage);
  152.         printf("Region : %s\n", p.region);
  153.         printf("------------------\n");
  154.     }
  155.     if (definer == 2) {
  156.         printf("------------------\n");
  157.         printf("Surname : %s\n", p.surname);
  158.         printf("Name : %s\n", p.name);
  159.         printf("Age : %d\n", p.age);
  160.         printf("Grade point average : %.1f\n", p.GradePointAverage);
  161.         printf("City : %s\n", p.city);
  162.         printf("------------------\n");
  163.     }
  164.     if (definer == 3) {
  165.         printf("------------------\n");
  166.         printf("Surname : %s\n", p.surname);
  167.         printf("Name : %s\n", p.name);
  168.         printf("Age : %d\n", p.age);
  169.         printf("Grade point average : %.1f\n", p.GradePointAverage);
  170.         printf("Street : %s\n", p.street);
  171.         printf("------------------\n");
  172.     }
  173.     if (definer == 4) {
  174.         printf("------------------\n");
  175.         printf("Surname : %s\n", p.surname);
  176.         printf("Name : %s\n", p.name);
  177.         printf("Age : %d\n", p.age);
  178.         printf("Grade point average : %.1f\n", p.GradePointAverage);
  179.         printf("House number : %d\n", p.house);
  180.         printf("------------------\n");
  181.     }
  182.     if (definer == 5) {
  183.         printf("------------------\n");
  184.         printf("Surname : %s\n", p.surname);
  185.         printf("Name : %s\n", p.name);
  186.         printf("Age : %d\n", p.age);
  187.         printf("Grade point average : %.1f\n", p.GradePointAverage);
  188.         printf("Flat number : %d\n", p.flat);
  189.         printf("------------------\n");
  190.     }
  191. }
  192. char* EnterInformation(char*);
  193. students *GetStudents(int&);
  194. int GetDefiner(int&, char**);
  195. int SearchInformationAboutStudents(int, int);
  196. int SearchInformationAboutStudents(int, int);
  197. int main(int argc, char** argv) {
  198.     int n, sch = 0, i, definer, value;
  199.     students *p;
  200.     printf("Enter number of students\n");
  201.     while (!(scanf_s("%d", &n)) || n < 1) {
  202.         rewind(stdin);
  203.         printf("Incorrect number,reenter!\n");
  204.     }
  205.     p = GetStudents(n);
  206.     definer = GetDefiner(definer, argv);
  207.     if (definer == 0) {
  208.         printf("Wrong data in command line,reenter!\n");
  209.         free(p);
  210.         return 1;
  211.     }
  212.     int(*SearchInf)(int p, int definer);
  213.     for (i = 0; i < n; i++) {
  214.         SearchInf = SearchInformationAboutStudents;
  215.         if ((value = SearchInf(p[i].flag, definer)) == 1) {
  216.             ShowInformationAboutStudents(p[i], definer);
  217.             sch++;
  218.         }
  219.     }
  220.     if (sch == 0)
  221.         printf("No students with this information\n");
  222.     free(p);
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement