gpt4 book ai didi

c - 我需要帮助让这个程序正确打印我的输出

转载 作者:行者123 更新时间:2023-11-30 15:58:16 26 4
gpt4 key购买 nike

#include <stdio.h>              /*printf and scanf option*/
#include <math.h>

void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies); /*function protype*/

int main(void)
{
int fifties = 0, twenties = 0, tens = 0, fives = 0, dollars = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0;
double amt_paid = 0, amt_due = 0, amt_change = 0, coin_change = 0; /*declared avriables*/

printf("Enter the amount paid> "); /*Prompt user to enter amount paid*/
scanf("%lf", &amt_paid);

printf("Enter the amount due> "); /*Prompt user to enter amount due*/
scanf("%lf", &amt_due);

amt_change = amt_paid - amt_due; /*Formula for amount of change to be given*/
dollars = (amt_change);

coin_change = (int)((amt_change - (amt_change)) * 100 + 0.5);
coin_change = coin_change * 100;
printf("\n%f\n", coin_change);

change(coin_change, &quarters, &dimes, &nickels, &pennies);

printf("Change is fifties: %d$, twenties: %d$, tens: %d$, fives: %d$, dollars: %d$, quarters: %d, dimes: %d, nickels: %d,\
pennies: %d", fifties, twenties, tens, fives, dollars, quarters, dimes, nickels, pennies);
return(0);
}

void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies)
{
int q = 1, d = 1, n = 1, p = 1;
do {
if(coin_change >= 25){
*quarters = *quarters + q;
coin_change = coin_change - 25;
}
else if (coin_change >= 10) {
*dimes = *dimes + d;
coin_change = coin_change - 10;
}
else if (coin_change >= 5) {
*nickels = *nickels + n;
coin_change = coin_change - 5;
}
else if (coin_change >= 1) {
*pennies = *pennies + p;
coin_change = coin_change - 1;
}
} while (coin_change >= 1);
}

抱歉我第一次没说清楚。我需要的是创建一个基本上是收银机程序的东西。当给出应付金额以及用户支付的金额时,我应该收到输出,告诉我我应该收到多少张 50 美元的钞票、20 美元、10 美元、5 美元、1 美元、25 美分、10 美分、5 美分和便士作为找零。由于我是编程新手,因此您所看到的代码是据我所知的。我确实需要改进甚至彻底改变它。我真正想做的是找出我的错误并改正它们。我希望尽快完成这段代码。我觉得我很接近,但只是错过了。也许我错了,但这就是我来找你们的目的。

最佳答案

有几件事:

  1. 您有许多未使用的变量:五十二十美元
  2. 此处的行:

    coin_change = (int)((amt_change - (amt_change)) * 100 + 0.5);
    coin_change = coin_change * 100;

    错了。它们应该替换为如下内容:

    coin_change = (100 * amt_change)

  3. 您听说过 +=/-= 运算符吗?他们会改变这些行:

    *季度 = *季度 + q;
    coin_change = coin_change - 25;

    进入此:

    *四分之一+= q;
    coin_change -= 25;

在我修复这些问题之后,你的代码工作正常。

关于c - 我需要帮助让这个程序正确打印我的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com