如何计算和打印;美元面额; C

时间:2017-10-24 06:28:34

标签: c

#include < stdio.h >

int getAmount(); //Asks for amount
int convert(); //This will divide the amount into how many 50's 20's and ten's
const int MAXAMOUNT = 95; //Question states max amount entered can be 95
const int MINAMOUNT = 5; // and min amount can be 5

//getting the amount
int getAmount(amount) {
    printf("Enter the amount: %d\n", amount);
    scanf("%d*c", & amount);
    if (amount < MINAMOUNT || amount > MAXAMOUNT) {
        printf("Enter an amount b/w 5-95");
        return (amount);
    }
    return (amount);
}


//this will convert the amount to how many cents
int convert(int amount) {

}

int main() {
    int amount = 0;
    //types of amount
    const int fiftycents = 50;
    const int twentycents = 20;
    const int tencents = 10;
    const int fivecents = 5;

    amount = getAmount(amount);

    return (0);
}

所以有一个问题的解决方案,但我不完全理解它,并且非常确定我班上的每个人都会使用相同的代码,不想被抄袭,我想知道怎么做它不同 How it should look like (this is from the code everyone is looking to use)

2 个答案:

答案 0 :(得分:0)

试试这段代码:

#include "stdio.h"

const int MAXAMOUNT = 95; //Question states max amount entered can be 95
const int MINAMOUNT = 5; // and min amount can be 5

const int fiftycents = 50;
const int twentycents = 20;
const int tencents = 10;
const int fivecents = 5;

//getting the amount
int getAmount() {
    int amount;
    printf("Enter the amount: ");
    scanf("%d*c", & amount);
    if (amount < MINAMOUNT || amount > MAXAMOUNT) {
        printf("Enter an amount b/w 5-95");
        return -1;
    }
    return amount;
}


//this will convert the amount to how many cents
void convert(int amount) 
{
    int allfifty = (int)(amount/fiftycents);
    amount = amount - allfifty * fiftycents;
    int alltwenty = (int)(amount/twentycents);
    amount = amount - alltwenty * twentycents;
    int allten = (int)(amount/tencents);
    amount = amount - allten * tencents;
    int allfive = (int)(amount/fivecents);
    amount = amount - allfive * fivecents;
    printf("50 - %d, 20 - %d, 10 - %d, 5 - %d", allfifty, alltwenty, allten, allfive);
}

int main() {
    int amount = getAmount();
    if (amount != -1)
    {
        convert(amount);
    }
    return 0;
}

函数convert计算amount中有多少50美分,毕竟50美分可以计算所有20美分,等等......

答案 1 :(得分:0)

我只是从最高值开始使用除法和提醒。

如果金额为$,则首先乘以100。 然后:

fifties = amount/50;
amount = amount % 50; // put back in amount the rest of the fist division. 
twenties = amount / 20;
amount =  amount % 20;
tens = amount / 10;
amount =  amount % 10;
fives = amount / 5;
cents =  amount % 5;  // finally the rest is in cents.
然后五十,二十,几十,五和美分是你的硬币数量