Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int i;
- // coins & bills of the monetary sys
- const int COINS[] = {500, 400, 100, 50, 40, 10, 5, 4, 1};
- // ten's of aviable in sys *10 to have integers
- const int TENS_COINS[] = {5, 4, 1};
- float treasury;
- // coeffs of the COINS in the order of COINS[] array
- unsigned int stock_of_coins[] = {0, 0, 2, 10, 10, 10, 10, 10, 10};
- unsigned int stock_of_tens[] = {20, 20, 20};
- void change_calculation(int v, int state) {
- int c;
- float tens;
- while(v != 0) {
- for (i = 0 ; i<state ; i++) {
- if(v >= COINS[i] && state == 9) {
- //calcul
- c = v / COINS[i];
- v = v % COINS[i];
- //removing from the stock
- stock_of_coins[i] -= c;
- //displaying
- printf("%hd times %hd$ coin\n", c, COINS[i]);
- }
- if(v >= TENS_COINS[i] && state == 3) {
- //calcul
- c = v / TENS_COINS[i];
- v = v % TENS_COINS[i];
- //removing from the stock
- stock_of_tens[i] = stock_of_tens[i] - c;
- //displaying
- tens = TENS_COINS[i]*0.1;
- printf("%hd times %.1f$ coin\n", c, tens);
- }
- }
- }
- }
- void display_stock(int *array_stock, const int *array_coins, int state){
- for (i = 0 ; i < state ; i++){
- if (state == 9) {
- printf("%hd times %hd$ coin\n", array_stock[i], array_coins[i]);
- }
- if (state == 3) {
- float tens = array_coins[i]*0.1;
- printf("%hd times %.1f$ coin\n", array_stock[i], tens);
- }
- }
- }
- float calcul_treasury() {
- float _treasury;
- for (i = 0 ; i < 9 ; i++) {
- _treasury += COINS[i]*stock_of_coins[i];
- }
- for (i = 0 ; i < 3 ; i++) {
- _treasury += TENS_COINS[i]*stock_of_tens[i]*0.1;
- }
- return _treasury;
- }
- float moneyback_calculation(float a, float b) {
- return (b-a);
- }
- int get_int_part(float in) {
- int out = (int)in;
- return out;
- }
- int get_dec_part(float in) {
- in -= (int)in;
- in *= 10;
- int out = (int)in;
- return out;
- }
- int main(void) {
- //price variables
- float amount_tobepaid = 0;
- //money input variables
- float amount_received_instant = 0.0;
- float amount_received = 0.0;
- float moneyback = 0.0;
- int moneyback_int = 0;
- int moneyback_dec = 0;
- // enter the amount to be paid
- printf("Enter the amount to be paid : ");
- scanf("%f", &amount_tobepaid);
- // enter the amount received
- while((int)(amount_tobepaid*10) > (int)(amount_received*10)) {
- printf("Enter your payment : ");
- scanf("%f", &amount_received_instant);
- amount_received = amount_received + amount_received_instant;
- }
- // calculate the money back
- moneyback = moneyback_calculation(amount_tobepaid, amount_received);
- printf("Money back : %.1f$\n", moneyback);
- // separating the float money back into 2 int
- moneyback_int = get_int_part(moneyback);
- moneyback_dec = get_dec_part(moneyback);
- //printf("int %hd\ndec %hd\n\n", moneyback_int, moneyback_dec);
- //get the details of the moneyback
- printf("Details of the moneyback :\n");
- change_calculation(moneyback_int, 9);
- change_calculation(moneyback_dec, 3);
- //display stcock COINS
- printf("\nIn stock :\n");
- display_stock(&stock_of_coins[0], &COINS[0], 9);
- display_stock(&stock_of_tens[0], &TENS_COINS[0], 3);
- //calcul treasury
- treasury = calcul_treasury();
- treasury += amount_tobepaid;
- //display treasury
- printf("\nTreasury : %.1f$\n", treasury);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement