Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4.  
  5. struct studentInfo {
  6.     struct {
  7.         char *name;
  8.         char *surname;
  9.         char *patronymic;
  10.     };
  11.     union {
  12.         int height;
  13.         int weight;
  14.     };
  15. };
  16.  
  17. char* stringInput(char *text) {
  18.     int i = 0, key = 0;
  19.     printf("%s", text);
  20.     char *string = nullptr;
  21.     if (!(string = (char*)malloc(sizeof(char)))) {
  22.         return NULL;
  23.     }
  24.     while ((key = getchar()) != '\n') {
  25.         string = (char*)realloc(string, sizeof(char) * (i + 1));
  26.         string[i++] = key;
  27.     }
  28.     string[i] = '\0';
  29.     return string;
  30. }
  31.  
  32. int numberInput(int verification, char* string) {
  33.     int number = 0;
  34.     char buffer = 0;
  35.     printf("%s", string);
  36.     printf(" ");
  37.     while ((!(scanf_s(" %d%c", &number, &buffer, 1))) || (buffer != '\n') || (number < verification) || (number <= 0)) {
  38.         rewind(stdin);
  39.         printf(" You've entered the wrong value. Please, enter again: ");
  40.     }
  41.     return number;
  42. }
  43.  
  44. void studentsInfoOutput(struct studentInfo* studentInitial, int checkup, int studentsAmount) {
  45.     for (int i = 0; i < studentsAmount; i++) {
  46.         if (checkup == 1) {
  47.             printf("___________________\n");
  48.             printf(" Surname: %s\n", studentInitial[i].surname);
  49.             printf(" Name: %s\n", studentInitial[i].name);
  50.             printf(" Patronymic: %s\n", studentInitial[i].patronymic);
  51.             printf(" Height is: %d\n", studentInitial[i].height);
  52.             printf("___________________\n");
  53.         }
  54.         if (checkup == 2) {
  55.             printf("___________________\n");
  56.             printf(" Surname: %s\n", studentInitial[i].surname);
  57.             printf(" Name: %s\n", studentInitial[i].name);
  58.             printf(" Patronymic: %s\n", studentInitial[i].patronymic);
  59.             printf(" Weight is: %d\n", studentInitial[i].weight);
  60.             printf("___________________\n");
  61.         }
  62.     }
  63. }
  64.  
  65. struct studentInfo studentInfoInput() {
  66.     int key = 0;
  67.     struct studentInfo studentInitial;
  68.     studentInitial.name = stringInput(" Enter student name: ");
  69.     studentInitial.surname = stringInput(" Enter student surname: ");
  70.     studentInitial.patronymic = stringInput(" Enter student patronymic: ");
  71.     printf(" What you would like to enter: weight(W) or height(H)?\n Please, press the appropriate button(W/H): ");
  72.     while (((key = getchar()) != 'H' && key != 'W')) {
  73.         printf(" You've entered the wrong value. Please, enter again: ");
  74.         rewind(stdin);
  75.     }
  76.     if (key == 'H') {
  77.         studentInitial.height = numberInput(0, " Enter student height: ");
  78.     }
  79.     else if (key == 'W') {
  80.         studentInitial.weight = numberInput(0, " Enter student weight: ");
  81.     }
  82.     return studentInitial;
  83. }
  84.  
  85. int parityCheck(int &checkup, char* argv[]) {
  86.     if (!(strcmp(argv[1], "Height")) || !(strcmp(argv[1], "height"))) {
  87.         return 1;
  88.     }
  89.     if (!(strcmp(argv[1], "Weight")) || !(strcmp(argv[1], "weight"))) {
  90.         return 2;
  91.     }
  92. }
  93.  
  94. int studentSearch(int studentInitial, int checkup) {
  95.     if (studentInitial == checkup)
  96.         return 1;
  97.     else return 0;
  98. }
  99.  
  100. int main(int argc, char *argv[]) {
  101.     int i = 0;
  102.     int checkup = 0;
  103.     int studentsAmount = 0;
  104.     int switchOption = 0;
  105.     int hey = 0;
  106.     struct studentInfo *studentInitial;
  107.     if (!(studentInitial = (struct studentInfo*)malloc(sizeof(struct studentInfo)))) {
  108.         printf(" Memory error.\n");
  109.         return 0;
  110.     }
  111.     printf(" Hello!\n");
  112.     do {
  113.         while ((switchOption = numberInput(1, " 1. Enter students information. \n 2. Required information output. \n 3. Exit. \n")) > 3) {
  114.             system("CLS");
  115.             printf(" You've entered the wrong value. Please, enter again: ");
  116.             rewind(stdin);
  117.         }
  118.         switch (switchOption) {
  119.         case 1: {
  120.             system("CLS");
  121.             studentsAmount++;
  122.             if (!(studentInitial = (struct studentInfo*)realloc(studentInitial, sizeof(struct studentInfo) * studentsAmount))) {
  123.                 printf(" Memory error.\n");
  124.                 return 0;
  125.             }
  126.             studentInitial[studentsAmount - 1] = studentInfoInput();
  127.             break;
  128.         }
  129.         case 2: {
  130.             system("CLS");
  131.             checkup = parityCheck(checkup, argv);
  132.             studentsInfoOutput(studentInitial, checkup, studentsAmount);
  133.             break;
  134.         }
  135.         case 3: {
  136.             system("CLS");
  137.             printf("Thank you for using this programm! Have a good day!\n\n");
  138.             break;
  139.         }
  140.         }
  141.     } while (switchOption != 3);
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement