Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

c - Modular coin change program logic [Australian currency]

My aim with this program is to tell how many coins of each denomination should be given to a customer with the assumption of inputs being from 5-95.

I have written my program in the best modular form I can, and it seems to be working. However, the program shows 0 for each denomination.

What is the problem?

void PrintDisplay(void)
{   
    printf("Enter an amount to calculate change: ");
    return;
}

int GetChange(void)
{
    int change;
    scanf("%d%*c", &change);
    return(change);
}

int GetCoins(int cents, int coin)
{
    int quantity;
    quantity = cents / coin;
    return(quantity);
}

int GetNewChange(int cents, int coin)
{
    int newchange;
    newchange = cents - coin;
    return(newchange);
}

void PrintResult(int fifties, int twenties, int tens, int fives)
{
    printf("The amount of each coin denomination you should give are: 
");
    printf("Fifty cent coins: %d 
", fifties);
    printf("Twenty cent coins: %d 
", twenties);
    printf("Ten cent coins: %d 
", tens);
    printf("Five cent coins: %d 
", fives);
    return;
}

int main(int input)
{

    int FiftyCentAmount;
    int TwentyCentAmount;
    int TenCentAmount;
    int FiveCentAmount;
    PrintDisplay();
    GetChange();
    FiftyCentAmount = GetCoins(input, 50);
    GetNewChange(input, 50);
    TwentyCentAmount = GetCoins(input, 20);
    GetNewChange(input, 20);
    TenCentAmount = GetCoins(input, 10);
    GetNewChange(input, 10);
    FiveCentAmount = GetCoins(input, 5);
    GetNewChange(input, 5);
    PrintResult(FiftyCentAmount, TwentyCentAmount, TenCentAmount, FiveCentAmount);
    system("pause");
   return(0);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A function has an input (the parameters), an output (return value and maybe parameters passed by pointer) and an algorithm that generates the output from the input.

Sometimes function do not really have an input or output like your superflous PrintDisplay. This is generally okay if you make your code more readable/ better organized that way. Your function is superflous because you make a function (which has four lines) for a simple one line statement. Not inherently bad but superflous.

The return value "appears" at the position where the function call was made. So if you do

input = GetChange();

input will be set to the return value of GetChange.

You fixed this after Daves answer, but didn't see that all your Get... functions also have a return value you just ignore in your code.

So by fixing all of them your code should work (or not see edit).

All your functions (except PrintResult and the textinput) are basically oneliners. And

They reduce to

int GetCoins(int cents, int coin)
{
    return cents / coin;
}

int GetNewChange(int cents, int coin)
{
    return cents - coin;
}

So an improved program (in your design) would look like this then:

int main()
{
    int leftover_change;

    printf("Enter an amount to calculate change: ");

    leftover_change= GetChange();

    int FiftyCentAmount = GetCoins(leftover_change, 50);
    leftover_change = GetNewChange(leftover_change, 50);

    int TwentyCentAmount = GetCoins(leftover_change, 20);
    leftover_change = GetNewChange(leftover_change, 20);

    int TenCentAmount = GetCoins(leftover_change, 10);
    leftover_change = GetNewChange(leftover_change, 10);

    int FiveCentAmount = GetCoins(leftover_change, 5);
    leftover_change = GetNewChange(leftover_change, 5);

    PrintResult(FiftyCentAmount, TwentyCentAmount, TenCentAmount, FiveCentAmount);

    if(leftover_change >0)
    {
        printf("there are %d cents left
", leftover_change);
    }

    system("pause");

    return 0;
}

This still vioaltes the DRY concept (Dont repeat yourself) you could fix this (at least partially) by replacing the magic numbers with defines or const variables.

Even better would be a loop that iterates through the different coins. But this needs a bit more thinking and the use of arrays.

edit: I did not check the algorithm in the first place, and my scanfreduction was bullshit but I was in a hurry so here a version that works. To make this answer complete, but as Dave said you should check your algorithm before you implement it.

int GetNewChange(int cents, int coin)
{
    return cents % coin; //this is the modulus operator
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...