Advertisement
dtung

Untitled

Apr 4th, 2020
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define SIZE 10
  4. #define CL 120
  5.  
  6. typedef char name[SIZE];
  7. typedef struct customer {
  8.     char names[SIZE];
  9.     int change;
  10. } details, copy;
  11.  
  12. int choice(int option);
  13. int readInputs(details s1[], FILE *coins);
  14. int searchname(details s1[], int count);
  15. int printcoin(int sum, int count);
  16. void csvoutput(details s1[], int count, char *argv[], int argc);
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.  
  21.     FILE *coins;
  22.     FILE *ChangeFile;
  23.     int count, sum;
  24.     int option;
  25.     char search;
  26.     int i;
  27.  
  28.     details s1[SIZE];
  29.     copy s2[SIZE];
  30.     if (argc == 1)
  31.     {
  32.         printf("Invalid Input \n");
  33.     }
  34.     else
  35.     {
  36.         coins = fopen(argv[1], "r");
  37.         if (coins == NULL)
  38.         {
  39.             printf("No file name matching \n");
  40.             return 1;
  41.         }
  42.         else
  43.         {
  44.             printf("File has opened\n");
  45.             count = readInputs(s1, coins);
  46.             fclose(coins);
  47.             do
  48.             {
  49.                 option = choice(option);
  50.  
  51.                 if (option == 1)
  52.                 {
  53.                     printf("\n Enter the name \n");
  54.                     sum = searchname(s1, count);
  55.                     printcoin(sum, count);
  56.                 }
  57.                 else if (option == 2)
  58.                 {
  59.                     printf("\n Creating CSv FIle\n");
  60.                     csvoutput(s1, count, argv, argc);
  61.                 }
  62.             } while (option != 2);
  63.         }
  64.     }
  65.     return 0;
  66. }
  67.  
  68. int choice(int option)
  69. {
  70.  
  71.     printf("\n Search Customer");
  72.     printf("\n Create CSV and exit\n");
  73.     do
  74.     {
  75.         printf("\n Please enter your choice:\n ");
  76.         scanf("%d%*c", &option);
  77.         if (option != 1 || option != 2)
  78.         {
  79.             printf("\n Please enter 1 or 2 for your choice\n ");
  80.         }
  81.     } while (option < 1 || option > 2);
  82.     return option;
  83. }
  84.  
  85. int readInputs(details s1[], FILE *coins)
  86. {
  87.     int count = 0;
  88.     char line[CL];
  89.  
  90.     while (fgets(line, CL, coins) != NULL)
  91.     {
  92.         sscanf(line, "%s %d\n", &s1[count].names, &s1[count].change);
  93.         printf("%s, %d\n", s1[count].names, s1[count].change);
  94.         count++;
  95.     }
  96.     fclose(coins);
  97.     printf("Number of Lines:%d\n", count);
  98.     return count;
  99. }
  100.  
  101. int searchname(details s1[], int count)
  102. {
  103.     int i, n, sum = 0;
  104.     char search[CL];
  105.     do
  106.     {
  107.         printf("\n ENter customer name:\n");
  108.         fgets(search, CL, stdin);
  109.         search[strlen(search) - 1] = '\0';
  110.         for (i = 0; i < count; i++)
  111.         {
  112.             if (!strcmp(search, s1[i].names))
  113.             {
  114.                 sum = sum + s1[i].change;
  115.             }
  116.         }
  117.         if (sum == 0)
  118.         {
  119.             printf("\n No match \n Please renetr tour choice\n");
  120.         }
  121.         else
  122.         {
  123.             printf("%s ,%d\n\n", search, sum);
  124.         }
  125.         if (sum < 5 || sum > 95)
  126.         {
  127.             printf("Invalid entry...!!! Please enter between 5 to 95\n");
  128.             sum = 0;
  129.         }
  130.     } while (sum == 0);
  131.     return sum;
  132. }
  133.  
  134. int printcoin(int sum, int count)
  135. {
  136.     int f, fiftycents, twentycents, tencents, fivecents;
  137.     for (f = 0; f < count; f++)
  138.     {
  139.         if (sum / 50)
  140.         {
  141.             fiftycents = sum / 50;
  142.             printf("\n fiftycents: %d", fiftycents);
  143.             sum %= 50;
  144.         }
  145.         else if (sum / 20)
  146.         {
  147.             twentycents = sum / 20;
  148.             printf("\n twentycents: %d", twentycents);
  149.             sum %= 20;
  150.         }
  151.         else if (sum / 10)
  152.         {
  153.             twentycents = sum / 10;
  154.             printf("\n tencents: %d", tencents);
  155.             sum %= 10;
  156.         }
  157.         else if (sum / 5)
  158.         {
  159.             twentycents = sum / 5;
  160.             printf("\n fivecents: %d", fivecents);
  161.             sum %= 5;
  162.         }
  163.     }
  164. }
  165.  
  166. void csvoutput(details s1[], int count, char *argv[], int argc)
  167. {
  168.     int i, j, k, sum, fiftycents, twentycents, tencents, fivecents;
  169.     FILE *ChangeFile;
  170.  
  171.     if (argc == 3)
  172.     {
  173.         ChangeFile = fopen(argv[2], "w");
  174.         fprintf(ChangeFile, "Name, Total,fiftycents, twentycents, tencents, fivecents\n");
  175.         for (i = 0; i < count; i++)
  176.         {
  177.             for (j = i + 1; j < count;)
  178.             {
  179.                 if (strcmp(s1[i].names, s1[j].names) == 0)
  180.                 {
  181.                     s1[i].change = s1[i].change + s1[j].change;
  182.                     for (k = j; k < count; k++)
  183.                     {
  184.                         strcpy(s1[k].names, s1[k + 1].names);
  185.                         (s1[k].change = s1[k + 1].change);
  186.                     }
  187.                     count--;
  188.                 }
  189.                 else
  190.                 {
  191.                     j++;
  192.                 }
  193.             }
  194.             sum = s1[i].change;
  195.             if (sum < 5 || sum > 95)
  196.                 continue;
  197.             fiftycents = sum / 50;
  198.             sum %= 50;
  199.             twentycents = sum / 20;
  200.             sum %= 20;
  201.             tencents = sum / 10;
  202.             sum %= 10;
  203.             fivecents = sum / 5;
  204.             sum %= 5;
  205.             fprintf(ChangeFile, "%s,%d, %d,%d,%d,%d\n", s1[i].names, s1[i].change, fiftycents, twentycents, tencents, fivecents);
  206.         }
  207.     }
  208.     fclose(ChangeFile);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement