SHARE
TWEET

prosit7

Ant0ine64 Nov 9th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int i;
  4.  
  5. // coins & bills of the monetary sys
  6. const int COINS[] = {500, 400, 100, 50, 40, 10, 5, 4, 1};
  7.  
  8. // ten's of aviable in sys *10 to have integers
  9. const int TENS_COINS[] = {5, 4, 1};
  10.  
  11. float treasury;
  12.  
  13. // coeffs of the COINS in the order of COINS[] array
  14. unsigned int stock_of_coins[] = {0, 0, 2, 10, 10, 10, 10, 10, 10};
  15. unsigned int stock_of_tens[] = {20, 20, 20};
  16.  
  17.  
  18. void change_calculation(int v, int state) {
  19.    
  20.     int c;
  21.     float tens;
  22.  
  23.     while(v != 0) {
  24.    
  25.         for (i = 0 ; i<state ; i++) {
  26.            
  27.             if(v >= COINS[i] && state == 9) {
  28.                 //calcul
  29.                 c = v / COINS[i];
  30.                 v = v % COINS[i];
  31.                
  32.                 //removing from the stock
  33.                 stock_of_coins[i] -= c;
  34.                
  35.                 //displaying
  36.                 printf("%hd times %hd$ coin\n", c, COINS[i]);
  37.             }
  38.             if(v >= TENS_COINS[i] && state == 3) {
  39.                 //calcul
  40.                 c = v / TENS_COINS[i];
  41.                 v = v % TENS_COINS[i];
  42.                
  43.                 //removing from the stock
  44.                 stock_of_tens[i] = stock_of_tens[i] - c;
  45.                
  46.                 //displaying
  47.                 tens = TENS_COINS[i]*0.1;
  48.                 printf("%hd times %.1f$ coin\n", c, tens);
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. void display_stock(int *array_stock, const int *array_coins, int state){
  56.        
  57.     for (i = 0 ; i < state ; i++){
  58.        
  59.         if (state == 9) {
  60.             printf("%hd times %hd$ coin\n", array_stock[i], array_coins[i]);
  61.         }
  62.        
  63.         if (state == 3) {
  64.             float tens = array_coins[i]*0.1;
  65.             printf("%hd times %.1f$ coin\n", array_stock[i], tens);
  66.         }
  67.     }
  68. }
  69.  
  70. float calcul_treasury() {
  71.    
  72.     float _treasury;
  73.    
  74.     for (i = 0 ; i < 9 ; i++) {
  75.         _treasury += COINS[i]*stock_of_coins[i];
  76.     }
  77.    
  78.     for (i = 0 ; i < 3 ; i++) {
  79.         _treasury += TENS_COINS[i]*stock_of_tens[i]*0.1;
  80.     }
  81.    
  82.     return _treasury;
  83. }
  84.  
  85.  
  86. float moneyback_calculation(float a, float b) {
  87.     return (b-a);
  88. }
  89.  
  90.  
  91. int get_int_part(float in) {
  92.     int out = (int)in;
  93.     return out;
  94. }
  95.  
  96.  
  97. int get_dec_part(float in) {   
  98.     in -= (int)in;
  99.     in *= 10;
  100.     int out = (int)in;
  101.     return out;
  102. }
  103.  
  104.  
  105. int main(void) {
  106.    
  107.     //price variables
  108.     float amount_tobepaid = 0;
  109.    
  110.     //money input variables
  111.     float amount_received_instant = 0.0;
  112.     float amount_received = 0.0;
  113.    
  114.     float moneyback = 0.0;
  115.     int moneyback_int = 0;
  116.     int moneyback_dec = 0;
  117.    
  118.    
  119.     // enter the amount to be paid
  120.     printf("Enter the amount to be paid : ");
  121.     scanf("%f", &amount_tobepaid);
  122.  
  123.    
  124.     // enter the amount received
  125.     while((int)(amount_tobepaid*10) > (int)(amount_received*10)) {
  126.         printf("Enter your payment : ");
  127.         scanf("%f", &amount_received_instant);
  128.         amount_received = amount_received + amount_received_instant;
  129.     }
  130.    
  131.    
  132.     // calculate the money back
  133.     moneyback = moneyback_calculation(amount_tobepaid, amount_received);
  134.     printf("Money back : %.1f$\n", moneyback);
  135.    
  136.     // separating the float money back into 2 int
  137.     moneyback_int = get_int_part(moneyback);
  138.     moneyback_dec = get_dec_part(moneyback);
  139.     //printf("int %hd\ndec %hd\n\n", moneyback_int, moneyback_dec);
  140.    
  141.     //get the details of the moneyback
  142.     printf("Details of the moneyback :\n");
  143.     change_calculation(moneyback_int, 9);
  144.     change_calculation(moneyback_dec, 3);
  145.    
  146.    
  147.     //display stcock COINS
  148.     printf("\nIn stock :\n");
  149.     display_stock(&stock_of_coins[0], &COINS[0], 9);
  150.     display_stock(&stock_of_tens[0], &TENS_COINS[0], 3);
  151.    
  152.    
  153.     //calcul treasury
  154.     treasury = calcul_treasury();
  155.     treasury += amount_tobepaid;
  156.     //display treasury
  157.     printf("\nTreasury : %.1f$\n", treasury);
  158.    
  159.    
  160.     return 0;
  161. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top