Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAX_CUSTOMERS 100
  4.  
  5. int main()
  6.  
  7. {
  8.  
  9. // declare variables
  10.  
  11. int numCommands, i, accounts[MAX_CUSTOMERS];
  12.  
  13. int command, id, amount, j, ok;
  14.  
  15. char filename[20];
  16.  
  17. /***Declare File Pointer***/
  18.  
  19. FILE *ifp;
  20.  
  21.  
  22.  
  23. // Open file.
  24.  
  25. printf("What is the name of the file?\n");
  26.  
  27. scanf("%s", filename);
  28.  
  29. /***Open Input File***/
  30.  
  31. ifp= fopen(filename, "r");
  32.  
  33. // Initialize accounts.
  34.  
  35. /***Set all values in accounts to zero***/
  36.  
  37.  
  38.  
  39. for(i=0;i<MAX_CUSTOMERS;i++)
  40.  
  41. {
  42.  
  43. accounts[i]=0;
  44.  
  45. }
  46.  
  47. command=0;
  48.  
  49. amount=0;
  50.  
  51. numCommands=0;
  52.  
  53. j=0;
  54.  
  55. i=0;
  56.  
  57. // Read the number of commands from the file
  58.  
  59. fscanf(ifp, "%d", &numCommands);
  60.  
  61.  
  62.  
  63. // Process each command.
  64.  
  65. for (i=0; i<numCommands; i++)
  66.  
  67. {
  68.  
  69.  
  70.  
  71. // Get type of this command.
  72.  
  73. /***Read command value from file***/
  74.  
  75. fscanf(ifp, "%d", &command);
  76.  
  77.  
  78.  
  79.  
  80.  
  81. // Add money.
  82.  
  83. if (command == 1)
  84.  
  85. {
  86.  
  87. /***Read id and amount from file***/
  88.  
  89. fscanf(ifp, "%d %d", &id, &amount);
  90.  
  91. accounts[id] += amount;
  92.  
  93. }
  94.  
  95.  
  96.  
  97. // Subtract money.
  98.  
  99. else if (command == 2)
  100.  
  101. {
  102.  
  103. fscanf(ifp, "%d %d", &id, &amount);
  104.  
  105. /***Deduct amount from the account with identification number id***/
  106.  
  107. accounts[id] -= amount;
  108.  
  109. }
  110.  
  111.  
  112.  
  113. // Print delinquents.
  114.  
  115. else
  116.  
  117. {
  118.  
  119. // Go through each account, flagging negative balances.
  120.  
  121. ok = 1;
  122.  
  123. for (j=0; j<MAX_CUSTOMERS; j++)
  124.  
  125. {
  126.  
  127. if (/***check if jth account is less than zero***/ accounts[j]<0)
  128.  
  129. {
  130.  
  131. printf("Customer %d, you owe $%d. Please pay immediately!\n", j, -accounts[j]);
  132.  
  133. ok = 0;
  134.  
  135. }
  136.  
  137. }
  138.  
  139.  
  140.  
  141. // Best case scenario.
  142.  
  143. if (ok)
  144.  
  145. {
  146.  
  147. printf("All accounts are paid up to date!!!\n");
  148.  
  149. }
  150.  
  151.  
  152.  
  153. }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. /***close file pointer***/
  160.  
  161. fclose(ifp);
  162.  
  163. return 0;
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement