Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define SIZE 10
- #define CL 120
- typedef char name[SIZE];
- typedef struct customer {
- char names[SIZE];
- int change;
- } details, copy;
- int choice(int option);
- int readInputs(details s1[], FILE *coins);
- int searchname(details s1[], int count);
- int printcoin(int sum, int count);
- void csvoutput(details s1[], int count, char *argv[], int argc);
- int main(int argc, char *argv[])
- {
- FILE *coins;
- FILE *ChangeFile;
- int count, sum;
- int option;
- char search;
- int i;
- details s1[SIZE];
- copy s2[SIZE];
- if (argc == 1)
- {
- printf("Invalid Input \n");
- }
- else
- {
- coins = fopen(argv[1], "r");
- if (coins == NULL)
- {
- printf("No file name matching \n");
- return 1;
- }
- else
- {
- printf("File has opened\n");
- count = readInputs(s1, coins);
- fclose(coins);
- do
- {
- option = choice(option);
- if (option == 1)
- {
- printf("\n Enter the name \n");
- sum = searchname(s1, count);
- printcoin(sum, count);
- }
- else if (option == 2)
- {
- printf("\n Creating CSv FIle\n");
- csvoutput(s1, count, argv, argc);
- }
- } while (option != 2);
- }
- }
- return 0;
- }
- int choice(int option)
- {
- printf("\n Search Customer");
- printf("\n Create CSV and exit\n");
- do
- {
- printf("\n Please enter your choice:\n ");
- scanf("%d%*c", &option);
- if (option != 1 || option != 2)
- {
- printf("\n Please enter 1 or 2 for your choice\n ");
- }
- } while (option < 1 || option > 2);
- return option;
- }
- int readInputs(details s1[], FILE *coins)
- {
- int count = 0;
- char line[CL];
- while (fgets(line, CL, coins) != NULL)
- {
- sscanf(line, "%s %d\n", &s1[count].names, &s1[count].change);
- printf("%s, %d\n", s1[count].names, s1[count].change);
- count++;
- }
- fclose(coins);
- printf("Number of Lines:%d\n", count);
- return count;
- }
- int searchname(details s1[], int count)
- {
- int i, n, sum = 0;
- char search[CL];
- do
- {
- printf("\n ENter customer name:\n");
- fgets(search, CL, stdin);
- search[strlen(search) - 1] = '\0';
- for (i = 0; i < count; i++)
- {
- if (!strcmp(search, s1[i].names))
- {
- sum = sum + s1[i].change;
- }
- }
- if (sum == 0)
- {
- printf("\n No match \n Please renetr tour choice\n");
- }
- else
- {
- printf("%s ,%d\n\n", search, sum);
- }
- if (sum < 5 || sum > 95)
- {
- printf("Invalid entry...!!! Please enter between 5 to 95\n");
- sum = 0;
- }
- } while (sum == 0);
- return sum;
- }
- int printcoin(int sum, int count)
- {
- int f, fiftycents, twentycents, tencents, fivecents;
- for (f = 0; f < count; f++)
- {
- if (sum / 50)
- {
- fiftycents = sum / 50;
- printf("\n fiftycents: %d", fiftycents);
- sum %= 50;
- }
- else if (sum / 20)
- {
- twentycents = sum / 20;
- printf("\n twentycents: %d", twentycents);
- sum %= 20;
- }
- else if (sum / 10)
- {
- twentycents = sum / 10;
- printf("\n tencents: %d", tencents);
- sum %= 10;
- }
- else if (sum / 5)
- {
- twentycents = sum / 5;
- printf("\n fivecents: %d", fivecents);
- sum %= 5;
- }
- }
- }
- void csvoutput(details s1[], int count, char *argv[], int argc)
- {
- int i, j, k, sum, fiftycents, twentycents, tencents, fivecents;
- FILE *ChangeFile;
- if (argc == 3)
- {
- ChangeFile = fopen(argv[2], "w");
- fprintf(ChangeFile, "Name, Total,fiftycents, twentycents, tencents, fivecents\n");
- for (i = 0; i < count; i++)
- {
- for (j = i + 1; j < count;)
- {
- if (strcmp(s1[i].names, s1[j].names) == 0)
- {
- s1[i].change = s1[i].change + s1[j].change;
- for (k = j; k < count; k++)
- {
- strcpy(s1[k].names, s1[k + 1].names);
- (s1[k].change = s1[k + 1].change);
- }
- count--;
- }
- else
- {
- j++;
- }
- }
- sum = s1[i].change;
- if (sum < 5 || sum > 95)
- continue;
- fiftycents = sum / 50;
- sum %= 50;
- twentycents = sum / 20;
- sum %= 20;
- tencents = sum / 10;
- sum %= 10;
- fivecents = sum / 5;
- sum %= 5;
- fprintf(ChangeFile, "%s,%d, %d,%d,%d,%d\n", s1[i].names, s1[i].change, fiftycents, twentycents, tencents, fivecents);
- }
- }
- fclose(ChangeFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement