Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6.  
  7. const char* locations[] = {"Bali", "Mali", "Cook islands", "Bahamas", "Iceland"};
  8. int location_count[] = {0, 0, 0, 0, 0};
  9. int location_min[] = {2, 5, 5, 5, 5};
  10. const char* transports[] = {"Airplane", "Ship", "Bus"};
  11.  
  12. const char* DATAFILENAME = "data.txt";
  13. const char* TEMPFILENAME = "data_temp.txt";
  14.  
  15. int locations_len = sizeof(locations) / sizeof(locations[0]);
  16. int transports_len = sizeof(transports) / sizeof(transports[0]);
  17.  
  18. char* people[1024][4];
  19.  
  20. // enum Transport{Airplane, Ship, Bus};
  21.  
  22. // struct PersonInfo{
  23. //   char name[60];
  24. //   char location[20];
  25. //   char phoneNumber[20];
  26. //   char transport[20];
  27. //   // enum Transport transport;
  28. // };
  29.  
  30. void printSeparator()
  31. {
  32.   printf("\n###############################\n\n");
  33. }
  34.  
  35. int printAllData()
  36. {
  37.   printSeparator();
  38.  
  39.   FILE *fp = fopen(DATAFILENAME, "r");
  40.   if(fp == NULL) {
  41.     perror("Unable to open file!"); //TODO: create file if does not exist
  42.     exit(1);
  43.   }
  44.  
  45.   int chunkIndex = 0;
  46.   char chunk[256];
  47.   while(fgets(chunk, sizeof(chunk), fp) != NULL)
  48.   {
  49.     char *pt;
  50.     pt = strtok (chunk, ",");
  51.    
  52.     char* personInfo[4];
  53.  
  54.     int n = 0;
  55.     while (pt != NULL) {
  56.       personInfo[n] = pt;
  57.       pt = strtok(NULL, ",");
  58.       n++;
  59.     }
  60.    
  61.     printf("%d) name: %s, location: %s, phone number: %s, transport: %s\n", chunkIndex, personInfo[0], personInfo[1], personInfo[2], personInfo[3]);
  62.     ++chunkIndex;
  63.   }
  64.   printSeparator();
  65.   fclose(fp);
  66.   return chunkIndex;
  67. }
  68.  
  69. int getIntUserInput()
  70. {
  71.   int input;
  72.   char ch;
  73.  
  74.   while (scanf("%d", &input) != 1)
  75.   {
  76.     while ((ch = getchar()) != '\n')
  77.     {
  78.       putchar(ch);
  79.     }
  80.     printf(" is not a number.\nPlease enter a number!\n");
  81.   }
  82.   return input;
  83. }
  84.  
  85.  
  86. int chooseLocationNum()
  87. {
  88.   int selectedLocationNum;
  89.  
  90.   printf("The available locations are the following:\n");
  91.  
  92.   printSeparator();
  93.   for (int i = 0; i < locations_len; ++i)
  94.   {
  95.     printf("%d: %s\n", i, locations[i]);
  96.   }
  97.   printSeparator();
  98.  
  99.   while(true)
  100.   {
  101.     selectedLocationNum = getIntUserInput();
  102.     if (selectedLocationNum >= 0 && selectedLocationNum < locations_len)
  103.     {
  104.       break;
  105.     }
  106.  
  107.     printf("Invalid number!\nSelect the number of location you need information about!\n");
  108.     printSeparator();
  109.     for (int i = 0; i < locations_len; ++i)
  110.     {
  111.       printf("%d: %s\n", i, locations[i]);
  112.     }
  113.     printSeparator();
  114.   }
  115.  
  116.   return selectedLocationNum;
  117. }
  118.  
  119. int chooseTransportNum()
  120. {
  121.   int selectedTransportNum;
  122.  
  123.   printf("The available transports are the following:\n");
  124.  
  125.   printSeparator();
  126.   for (int i = 0; i < transports_len; ++i)
  127.   {
  128.     printf("%d: %s\n", i, transports[i]);
  129.   }
  130.   printSeparator();
  131.  
  132.   while(true)
  133.   {
  134.     selectedTransportNum = getIntUserInput();
  135.     if (selectedTransportNum >= 0 && selectedTransportNum < transports_len)
  136.     {
  137.       break;
  138.     }
  139.  
  140.     printf("Invalid number!\nSelect the number of location you need information about!\n");
  141.     printSeparator();
  142.     for (int i = 0; i < transports_len; ++i)
  143.     {
  144.       printf("%d: %s\n", i, transports[i]);
  145.     }
  146.     printSeparator();
  147.   }
  148.  
  149.   return selectedTransportNum;
  150. }
  151.  
  152.  
  153. int getChosenMenuPoint(void)
  154. {
  155.   printf("0) exit the program\n");
  156.   printf("1) input data\n");
  157.   printf("2) delete data\n");
  158.   printf("3) modify existing data\n");
  159.   printf("4) list data by location\n");
  160.   printf("5) list all data\n");
  161.  
  162.   return getIntUserInput();
  163. }
  164.  
  165. void enterNewData()
  166. {
  167.   const char* personInfo[4];
  168.  
  169.   char name[60];
  170.   char location[20];
  171.   char phoneNumber[20];
  172.   char transport[20];
  173.  
  174.   printf("[Enter name]\n");
  175.   getchar();
  176.   scanf("%[^\n]", name);
  177.   personInfo[0] = name;
  178.  
  179.   printf("[Enter location numer]\n");
  180.   int chosenLocation = chooseLocationNum();
  181.   personInfo[1] = locations[chosenLocation];
  182.   location_count[chosenLocation] += 1;
  183.  
  184.   printf("[Enter phone number]\n");
  185.   scanf("%s", phoneNumber);
  186.   personInfo[2] = phoneNumber;
  187.  
  188.   printf("[Enter transport]\n");
  189.   int chosenTransport = chooseTransportNum();
  190.   personInfo[3] = transports[chosenTransport];
  191.  
  192.   char lineToAdd[1024];
  193.  
  194.   snprintf(lineToAdd, sizeof(lineToAdd), "%s,%s,%s,%s\n", personInfo[0], personInfo[1], personInfo[2], personInfo[3]);
  195.  
  196.   FILE *fp;
  197.   fp = fopen(DATAFILENAME, "a");
  198.  
  199.   if(fp == NULL) {
  200.     perror("Error opening file.");
  201.     exit(1);
  202.   }
  203.  
  204.   fprintf(fp, "%s", lineToAdd);
  205.   fclose(fp);
  206. }
  207.  
  208. int deleteData(const char* outputText)
  209. {
  210.   int maxLineIndex = printAllData() - 1;
  211.   if (maxLineIndex == -1)
  212.   {
  213.     return -1;
  214.   }
  215.   printf("%s", outputText);
  216.   int lineIndexToDelete;
  217.  
  218.   while(true)
  219.   {
  220.     lineIndexToDelete = getIntUserInput();
  221.     if (lineIndexToDelete >= 0 && lineIndexToDelete <= maxLineIndex)
  222.     {
  223.       break;
  224.     }
  225.  
  226.     printf("Invalid number!\nSelect a valid number!\n");
  227.   }
  228.  
  229.   FILE *fp1, *fp2;
  230.   char filename[100], c;
  231.  
  232.   // Open one file for reading
  233.   fp1 = fopen(DATAFILENAME, "r");
  234.   if (fp1 == NULL)
  235.   {
  236.       printf("Cannot open file %s \n", DATAFILENAME);
  237.       exit(1);
  238.   }
  239.  
  240.   // Open another file for writing
  241.   fp2 = fopen(TEMPFILENAME, "w");
  242.   if (fp2 == NULL)
  243.   {
  244.       printf("Cannot open file %s \n", TEMPFILENAME);
  245.       exit(1);
  246.   }
  247.  
  248.   int line_index = 0;
  249.   while (true)
  250.   {
  251.     char ch = getc(fp1);
  252.     if (ch == EOF)
  253.     {
  254.       break;
  255.     }
  256.     //except the line to be deleted
  257.     if (line_index != lineIndexToDelete)
  258.     {
  259.       putc(ch, fp2);
  260.     }
  261.     if (ch == '\n')
  262.     {
  263.       line_index++;
  264.     }
  265.   }
  266.  
  267.   fclose(fp1);
  268.   fclose(fp2);
  269.  
  270.   remove(DATAFILENAME);
  271.   rename(TEMPFILENAME, DATAFILENAME);
  272.   return 0;
  273. }
  274.  
  275. void modifyData()
  276. {
  277.   if (deleteData("choose the index of the line you wanna modify!\n") != -1)
  278.   {
  279.     enterNewData();
  280.   }
  281.   else
  282.   {
  283.     printf("there is no data stored yet! Nothing to modify!\n");
  284.   }
  285.  
  286. }
  287.  
  288. void listData(bool silent_init = false)
  289. {
  290.   int selectedLocationNum = -1;
  291.  
  292.   if (!silent_init)
  293.   {
  294.     printf("[Select the number of location you need information about]\n");
  295.  
  296.     selectedLocationNum = chooseLocationNum();
  297.  
  298.     printf("selected location is: %s.  The following people are stuck there:\n", locations[selectedLocationNum]);
  299.     printSeparator();
  300.   }
  301.  
  302.   FILE *fp = fopen(DATAFILENAME, "r");
  303.   if(fp == NULL) {
  304.     perror("Unable to open file!"); //TODO: create file if does not exist
  305.     exit(1);
  306.   }
  307.    char chunk[256];
  308.    while(fgets(chunk, sizeof(chunk), fp) != NULL)
  309.    {
  310.       char *pt;
  311.       pt = strtok (chunk, ",");
  312.      
  313.       char* personInfo[4];
  314.  
  315.       int n = 0;
  316.       while (pt != NULL) {
  317.         personInfo[n] = pt;
  318.         pt = strtok(NULL, ",");
  319.         n++;
  320.       }
  321.  
  322.       if (silent_init)
  323.       {
  324.         for (int i = 0; i < 5; ++i)
  325.         {
  326.           if (strcmp(personInfo[1], locations[i]) == 0)
  327.           {
  328.             location_count[i] += 1;
  329.             break;
  330.           }
  331.         }
  332.       }
  333.       else if (strcmp(personInfo[1], locations[selectedLocationNum]) == 0)
  334.       {
  335.         printf("name: %s, location: %s, phone number: %s, transport: %s\n", personInfo[0], personInfo[1], personInfo[2], personInfo[3]);
  336.       }
  337.    }
  338.    if (!silent_init)
  339.    {
  340.      printSeparator();
  341.    }
  342.  
  343.    fclose(fp);
  344. }
  345.  
  346. void rescue(int location_index)
  347. {
  348.   if (fork() == 0)
  349.   {
  350.     printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
  351.     exit(0);
  352.   }
  353. }
  354.  
  355. int main()
  356. {
  357.   if (access(DATAFILENAME, F_OK) == -1) {
  358.     printf("file does not exist.. creating data.txt\n");
  359.  
  360.     FILE* fp = fopen(DATAFILENAME, "w");
  361.     fclose(fp);
  362.   }
  363.  
  364.   listData(true); //init number of stuck people per location
  365.   for (int i = 0; i < 5; ++i)
  366.   {
  367.     if (location_count[i] >= location_min[i])
  368.     {
  369.       printf("Let's rescue some people from: %s!\n", locations[i]);
  370.       rescue(i);
  371.     }
  372.   }
  373.  
  374.   int n = -1;
  375.   while (n = getChosenMenuPoint()) //it will exit if n is 0
  376.   {
  377.     switch (n)
  378.     {
  379.       case 1:
  380.         enterNewData();
  381.         break;
  382.       case 2:
  383.         if (deleteData("choose the index of the line you wanna delete!\n") == -1)
  384.         {
  385.           printf("there is no data stored yet! Nothing to delete!\n");
  386.         }
  387.         break;
  388.       case 3:
  389.         modifyData();
  390.         break;
  391.       case 4:
  392.         listData();
  393.         break;
  394.       case 5:
  395.         printAllData();
  396.       break;
  397.       case 6:
  398.         for (int i = 0; i < 5; ++i)
  399.         {
  400.           printf("%d, ", location_count[i]);
  401.         }
  402.         printf("\n");
  403.       default:
  404.         printf("Please input a valid menu point number!\n");
  405.         break;
  406.     }
  407.   }
  408.  
  409.   return 0;
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement