编写一个模块化解决方案(C 程序),它将接受一定数量的货币(以美分计)作为输入,并返回该货币的最佳硬币数量。 输入的金额以分为单位,应为 1 到 95 之间的整数值(含 1 到 95)。 该程序应按如下方式工作;首先,它会要求用户在以下三种货币中选择一种:
它应该要求用户输入一个 1 到 95 之间的数字,以美分为单位。如果选择的货币是 AU$,你的程序应该确保输入值是 5 的倍数。 然后,您的程序应返回合计为该数字的最小硬币数(以所选货币计)。例如,输入 30 美分的糟糕解决方案将给出 6 个 5 美分的硬币。正确的解决方案是给出一个 20 美分的硬币和一个 10 美分的硬币。 每次输出后,应询问用户是否希望继续(即输入另一个数量)或退出/终止程序。 您的解决方案(算法和程序)应该使用模块化方法进行设计。 **重要提示:对于这个问题,代码复用、低耦合、高内聚的原则,尤为重要
#include <stdio.h>
#include "coins.h"
#define LEN 4 //Length of coin set.
void coin (int amount);
int main (void)
{
int userSelection;
int amount;
int coinsUSD, CoinsAUD, CoinsEuro;
char ch = 'y';
printf("User to select 1 for USD, 2 for AUD or 3 for Euro: \n"); //User to select currency
scanf("%d", &userSelection);
if (userSelection == 1) //User selected USD
{
printf("You have chosen USD\n");
}
if(userSelection == 2) //User selected AUD
{
printf("You have chosen AUD\n");
}
if(userSelection == 3) //User selected Euro
{
printf("You have chosen Euro\n");
}
do
{
printf("User to select amount between 1-95 cents: \n"); //User to select amount between 1 - 95
if(userSelection == 2) //If user selects AUD prompt for amount in multiples of 5
{
printf("If using AUD, amount must be in multiples of 5\n");
}
scanf("%d", &amount);
while((userSelection == 2) && (amount % 5 != 0)) //If AUD amount is not a multiple of 5
{
printf("Invalid amount, please select amount that is a multiple of 5: \n");
scanf("%d", &amount);
}
while ((amount < 1)||(amount > 95)) //If amount is < 1 or > 95 give invalid message and prompt for correct amount
{
printf("Invalid amount, please select amount between 1 - 95 only: \n");
scanf("%d", &amount);
}
if (userSelection ==1) //If currency selected is USD call USD function
{
USD(amount);
}
if(userSelection ==2) //If currency selected is AUD call AUD function
{
AUD(amount);
}
if(userSelection ==3) //If currency selected is Euro call Euro function
{
Euro(amount);
}
printf("Do you wish to continue with a new amount y=yes, n=no: \n"); //Ask user if they wish to continue with a new amount or terminate program
scanf(" %c", &ch);
}
while(ch == 'y');
}
// This is the coins.c function
#include <stdio.h>
#include "coins.h"
#define LEN 4
void USD(int amount)
{
int i=0;//index of the array
int number;//the number of least amount of coin for a specific coin value
int coin_set[LEN]= {50, 25, 10, 1};//The USD coin set
printf("The least amount of change needed for the amount of %d cents is below: \n", amount);
while(i < LEN){
if(coin_set[i] <= amount){
number = amount/coin_set[i];
//Prints the least number of coins needed
if(coin_set[i] == 50 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 25 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 10 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 1 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
amount = amount - number *coin_set[i];
}
i++;//increment i by 1
}
}
void AUD(int amount)
{
int i=0;//index of the array
int number;//the number of least amount of coin for a specific coin value
int coin_set[LEN]= {50, 20, 10, 5};//The AUD coin set
printf("The least amount of change needed for the amount of %d cents is below: \n", amount);
while(i < LEN){
if(coin_set[i] <= amount){
number = amount/coin_set[i];
//Prints the least number of coins needed
if(coin_set[i] == 50 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 20 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 10 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 5 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
amount = amount - number *coin_set[i];
}
i++;//increment i by 1
}
}
void Euro(int amount)
{
int i=0;//index of the array
int number;//the number of least amount of coin for a specific coin value
int coin_set[LEN]= {20, 10, 5, 1};//The Euro coin set
printf("The least amount of change needed for the amount of %d cents is below: \n", amount);
while(i < LEN){
if(coin_set[i] <= amount){
number = amount/coin_set[i];
//Prints the least number of coins needed
if(coin_set[i] == 20 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 10 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 5 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
if(coin_set[i] == 1 ){
printf("%d x %d cents \n", number, coin_set[i]);
}
amount = amount - number *coin_set[i];
}
i++;//increment i by 1
}
}
** 这工作正常但是我的问题是我需要它的硬币功能变得更低耦合,高内聚,可重复使用的代码例如: 常量硬币 硬币 = 硬币 1 = 1 硬币2 = 5 硬币3 = 10 硬币4 = 20 硬币5 = 25 硬币6 = 50 我只是不确定如何实现这个。