main.c 4.34 KB
 Dominik Widhalm committed Jul 19, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 ``````/****************************************************************************** * C PROGRAMMING * * BASIC EXERCISES - EXAMPLE SOLUTIONS * * * * Task_4.07: Automatic Teller * * Author: Dominik Widhalm * * Email: dominik.widhalm@technikum-wien.at * * Date: 2017-07-19 * * * ******************************************************************************/ /***** INCLUDES ***************************************************************/ #include /***** MACROS *****************************************************************/ // Maximum amount of money #define MONEY_MAX 200 /***** FUNCTION PROTOTYPES ****************************************************/ int get_number_of_coins (int deposit, int coinage); /***** LOCAL FUNCTIONS ********************************************************/ /****************************************************************************** * get_number_of_coins * * @brief Function to calculate the number of coins of specific value. * * * * This function returns the maximum number of coins of a specific value * * contained in the given deposit value. * * * * @param deposit Value of cash deposit * * @param coinage Value of the coin * * @return Number of coins contained * ******************************************************************************/ int get_number_of_coins (int deposit, int coinage) { /* Return the number of coins contained */ return (int)((double)deposit/(double)coinage); } /***** MAIN ROUTINE ***********************************************************/ int main (void) { /*** Local Variables ***/ double cash; int cash_tmp; int coins[5] = {0,0,0,0,0}; int coin_value[5] = {20,10,5,2,1}; /* At least one user input is required */ do { /* Ask the user to enter the number of students */ printf("Please enter the cash deposit (max. 200 EUR): "); /* Read in the user's input */ scanf("%lf",&cash); /* Check if input was valid */ `````` Dominik Widhalm committed Nov 14, 2017 58 `````` if (cash < 0.1) { `````` Dominik Widhalm committed Jul 19, 2017 59 `````` /* Number has to be greater than 0 */ `````` Dominik Widhalm committed Nov 14, 2017 60 `````` printf("The entered deposit is too less!\n"); `````` Dominik Widhalm committed Jul 19, 2017 61 62 63 64 65 `````` }else if (cash > MONEY_MAX) { /* Number entered is too big */ printf("The entered deposit is too big!\n"); } /* Repeat when a invalid number was entered */ `````` Dominik Widhalm committed Nov 14, 2017 66 `````` } while ((cash < 0.1) || (cash > MONEY_MAX)); `````` Dominik Widhalm committed Jul 19, 2017 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 `````` /* * From this moment on we calculate with (int), because ... * ... if you use double, it may have problems with the internal representation. * E.g., the value 1.2 may represented as 1.19999999999999995559 (true story) */ cash_tmp = (int)(cash*10); /* Determine the number of coins */ for (int i=0; i<5; i++) { /* Get number of coins contained */ coins[i] = get_number_of_coins(cash_tmp,coin_value[i]); /* Subtract value from total */ cash_tmp -= (coin_value[i] * coins[i]); } /* Output the resulting amount of coins */ printf("\nFor a deposit of %3.2fEUR you get ...\n",cash); for (int i=0; i<5; i++) { `````` Dominik Widhalm committed Nov 14, 2017 86 87 88 89 90 `````` /* Check if there are coins of the current value */ if (coins[i] > 0) { /* Print the current number of coins */ printf("... %d coins of value %1.2fEUR\n",coins[i],((double)coin_value[i]/10.0)); } `````` Dominik Widhalm committed Jul 19, 2017 91 92 93 94 95 96 97 98 `````` } /* Notify the user about the termination of the program */ printf("\nThe program will now be terminated...\n"); return 0; /* Return with Success (0) */ } /******************************************************************************/``````