Advertisement
Stoycho_KK

tu-hw

May 16th, 2021 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. //used to use scanf instead of scanf_s
  6. #pragma warning(disable: 4996)
  7.  
  8. //used for validation of year
  9. const int P_YEAR = 2020;
  10.  
  11. //date-time struct in the correct format
  12. struct dateTime {
  13.     int mount;
  14.     int day;
  15.     int year;
  16. };
  17.  
  18. //printing date time obj
  19. void showDateTime(const struct dateTime* toPrint) {
  20.     printf("$Mount: %d\n$Day: %d\n$Year: %d \n", toPrint->mount, toPrint->day, toPrint->year);
  21. }
  22.  
  23. //setting date time to default vals
  24. void nullDateTime(struct dateTime* toInit) {
  25.     toInit->day = 0;
  26.     toInit->mount = 0;
  27.     toInit->year = 0;
  28. }
  29.  
  30. //getting data from the user about date-time obj
  31. void initDateTime(struct dateTime* toInit) {
  32.     int mnt = 0;
  33.     int day = 0;
  34.     int year = 0;
  35.  
  36.     printf("$Enter mount[number between 1-12]: ");
  37.     scanf("%4d", &mnt);
  38.  
  39.     printf("$Enter day[number between 1-31]: ");
  40.     scanf("%4d", &day);
  41.  
  42.     printf("$Enter year[number greater than %d]: ", P_YEAR);
  43.     scanf("%4d", &year);
  44.  
  45.     if (mnt < 13 && year > P_YEAR && day < 31) {
  46.         toInit->mount = mnt; toInit->day = day; toInit->year = year;
  47.     }
  48.     else {
  49.         nullDateTime(toInit);
  50.         return;
  51.     }
  52. }
  53.  
  54. //tourist agency structure
  55. struct touristAgency {
  56.     char* name;
  57.     char* dest;
  58.     struct dateTime departure;
  59.     struct dateTime arrival;
  60. };
  61.  
  62. //takes care of dynamic memory
  63. void freeAgency(struct touristAgency* freeMe) {
  64.     free(freeMe->name);
  65.     free(freeMe->dest);
  66. }
  67.  
  68. //initializing t.a. obj. with passed data
  69. void initAgency(struct touristAgency* toInit, struct dateTime departure, struct dateTime arrival) {
  70.     printf("$Enter name: ");
  71.     char name[1024];
  72.     scanf("%s", &name);
  73.     printf("$Enter destination: ");
  74.     char dest[1024];
  75.     scanf("%s", &dest);
  76.  
  77.     int lenName = strlen(name);
  78.     if (!lenName)
  79.         return;
  80.  
  81.     toInit->name = malloc(sizeof(char) * lenName + 1);
  82.     memcpy(toInit->name, name, lenName * sizeof(char));
  83.  
  84.     toInit->name[lenName] = '\0';
  85.  
  86.     int lenDest = strlen(dest);
  87.  
  88.     if (!lenDest) {
  89.         return;
  90.     }
  91.  
  92.     toInit->dest = malloc(sizeof(char) * lenDest + 1);
  93.     memcpy(toInit->dest, dest, lenDest * sizeof(char));
  94.     toInit->dest[lenDest] = '\0';
  95.  
  96.     printf("======arrival======\n");
  97.  
  98.     initDateTime(&toInit->arrival, arrival.mount, arrival.day, arrival.year);
  99.  
  100.     printf("======departure======\n");
  101.     initDateTime(&toInit->departure, departure.mount, departure.day, departure.year);
  102. }
  103.  
  104. //printing t.a. data
  105. void showAgency(const struct touristAgency* printMe) {
  106.     printf("$Name: %s\n", printMe->name);
  107.     printf("$Destination: %s\n", printMe->dest);
  108.     showDateTime(&printMe->arrival);
  109.     showDateTime(&printMe->departure);
  110.  
  111. }
  112.  
  113. //checks if two dates are "equal"
  114. int equalDates(const struct dateTime* _first, const struct dateTime* _second) {
  115.     return ((_first->day == _second->day) &&
  116.         (_first->mount == _second->mount) &&
  117.         (_first->year == _second->year));
  118. }
  119.  
  120. //linear search
  121. void search(const struct touristAgency* database, int len) {
  122.     struct dateTime dt;
  123.     int found = 0;
  124.     initDateTime(&dt);
  125.  
  126.     for (int i = 0; i < len; i++)
  127.         if (equalDates(&database[i].arrival, &dt) == 1) {
  128.             printf("\n======FOUND======\n");
  129.             showAgency(&database[i]);
  130.             found = 1;
  131.             printf("\n");
  132.         }
  133.     if (!found)
  134.         printf("No such list\n");
  135. }
  136.  
  137. //main loop:
  138. void run() {
  139.     struct touristAgency* data = malloc(20 * sizeof(struct touristAgency));
  140.     char c = '\0';
  141.     struct dateTime arrival;
  142.     struct dateTime departure;
  143.     for (int i = 0; i < 20; i++) {
  144.         printf("$Press\n$a to add\n$s to search\n$v to view\n$x to exit: ");
  145.  
  146.         scanf("%s", &c);
  147.  
  148.         switch (c) {
  149.         case'a':
  150.             nullDateTime(&arrival);
  151.             nullDateTime(&departure);
  152.             initAgency(&data[i], departure, arrival);
  153.             break;
  154.         case 'v':
  155.             for (int j = 0; j < i; j++)
  156.                 showAgency(&data[j]);
  157.             i--;
  158.             break;
  159.         case 's':
  160.             if (i == 0)
  161.                 printf("No data!\n");
  162.             else
  163.                 search(data, i);
  164.             i--;
  165.             break;
  166.         case 'x':
  167.             printf("Bye!!!");
  168.             for (int j = 0; j < i; j++)
  169.                 freeAgency(&data[j]);
  170.             free(data);
  171.             exit(1);
  172.             break;
  173.         default:
  174.             printf("$invalid comand\n");
  175.             i--;
  176.             break;
  177.         }
  178.     }
  179.     printf("$Maximum of 20 records reached!");
  180.  
  181.     for (int i = 0; i < 20; i++)
  182.         freeAgency(&data[i]);
  183.  
  184.     free(data);
  185. }
  186.  
  187. int main() {
  188.     run();
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement