改变计算器//用C和函数苦苦挣扎

时间:2015-02-04 03:46:59

标签: c function

我刚开始学习C所以请耐心等待。我正在试图弄清楚如何使用函数编程更改计算器。我只需要帮助学习如何构建它。我想出了下面需要使用的公式:

int dollars = float money / 1

float quarters = money – dollars

int quartercount = quarters / .25

float dimes = quarters - (quartercount*.25)

int dimescount = dimes / .10

float nickels = dimes – (dimescount*.10)

int nickelcount = nickels / .05

float pennies = nickels – (nickelcount*.05)

int pennycount = pennies / .01
编辑:我应该更清楚地表达对不起。我们将输入更改金额,它将显示进行更改所需的金额和金币。

非常感谢任何数量的帮助!

1 个答案:

答案 0 :(得分:1)

基本结构是这样的。但这取决于你是否需要返回值,或者只是打印它们(如本例所示)。

void changeCalculator(float money) {
    int dollars = money / 1;
    printf("Dollars = %d",dollars);
    float quarters = money – dollars;
    int quartercount = quarters / .25;
    printf("Quarters = %d",quartercount );
    float dimes = quarters - (quartercount*.25);
    int dimescount = dimes / .10;
    printf("Dimes= %d",dimescount);
    float nickels = dimes – (dimescount*.10);
    int nickelcount = nickels / .05;
    printf("Nickels= %d",nickelcount );
    float pennies = nickels – (nickelcount*.05);
    int pennycount = pennies / .01;
    printf("Pennies= %d",pennycount );
}