gpt4 book ai didi

c - 如果计算的变量不是整数,如何继续执行下一行代码

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

** 更新:Loc Tran 解决了这个问题!谢谢洛克特兰!! **

我正在编写一个程序(严格使用 C)来计算美国面额的最小变化。当我的程序达到一角硬币(第一个不能被整数整除的面额后,程序似乎不想继续五分硬币和便士。

我尝试使用 if 语句,如果返回零则排除 dimesDue 值,但似乎无法弄清楚。如果您注意到的话,我还必须根据从总找零金额中扣除的先前面额,为每个面额创建一个新的找零变量。我更愿意简化这一点并在每次计算后指定新值,但不能。

// Amount Tendered and Purchase amount converted to pennies

amountDue = 2117;
amountGiven = 10000;

// Creating a new change amount for each denomination, based on each previous computation

change = amountGiven - amountDue;
change10s = change % (20 * 100);
change5s = change % (10 * 100);
change1s = change % (5 * 100);
changeQs = change % (1 * 100);
changeDs = change % 25;
changeNs = change % 10;
changePs = change % 1;

// Using each new change amount to calculate amount of denomination

twentiesDue = (change / 20) / 100;
tensDue = (change10s / 10) / 100;
fivesDue = (change5s / 5) / 100;
onesDue = (change1s / 1) / 100;
quartersDue = (changeQs / 25);
dimesDue = (changeDs / 10);
nickelsDue = (changeNs / 5);
penniesDue = (changePs / 1);

printf("Amount Due: $21.17\nAmount Tendered: $100\n\n");
printf("Change Due:\n(by denomination)\n");
printf("Twenties: %d\n", twentiesDue);
printf("Tens: %d\n", tensDue);
printf("Fives: %d\n", fivesDue);
printf("Ones: %d\n", onesDue);
printf("Quarters: %d\n", quartersDue);
printf("Dimes: %d\n", dimesDue);
printf("Nickels: %d\n", nickelsDue);
printf("Pennies: %d\n", penniesDue);

程序到达一角硬币(第一个不等于整数的面额),并且不会继续计算镍币和便士的数量。因为季度后剩下的零钱是 8 美分,所以它不能被 10 整除。但我不知道如何使用 if 语句指定忽略它!

所以结果是,一旦程序达到一毛钱,所有变量此后都会计算为零。但应该是一分钱三分钱!这是我运行时的结果:

应付金额:21.17 美元投标金额:100 美元

更改到期时间:(按面额)二十岁:3十位:1五:1个数:3宿舍:3一毛钱:0镍:0便士:0

最佳答案

虽然这个解决方案有效,但我无法使用它。必须使用模运算符。

您在计算更改货币值后的剩余金额时遇到问题。获取数字中的所有数字的过程类似。例如,得到 1 后有 198,剩下的是 198-1*100 = 98,那么我们得到 9 ,剩下的是 98-9*10=8,得到 8 就完成了。你用错误的公式计算了。这是我的计算,采用与获取数字中的数字相同的方式,以及整个解决方案:

#include <stdlib.h>
#include <stdio.h>


int main()
{
// Amount Tendered and Purchase amount converted to pennies
int amountDue = 2117;
int amountGiven = 10000;

// Using each new change amount to calculate amount of denomination

// getting 20dollars notes
int change = amountGiven - amountDue;
int twentiesDue = (change / 20) / 100;

// get the remaining after change 20dollar notes, and
// then divide 10*100 for getting 10dollars notes
int change10s = change % (20*100);
int tensDue = (change10s / 10) / 100;

// get the remaining after change 10dollar notes, and
// then divide 5*100 for getting 5dollars notes
int change5s = change10s % (10*100);
int fivesDue = (change5s / 5) / 100;

// get the remaining after change 5dollar notes, and
// then divide 1*100 for getting 1dollars notes
int change1s = change5s % (5*100);
int onesDue = (change1s / 1) / 100;

// get the remaining after change 1dollar notes, and
// then divide 25 for getting quarter coins
int changeQs = change1s % (1*100);
int quartersDue = (changeQs / 25);

// get the remaining after change quarter coins, and
// then divide 10 for getting dime coins
int changeDs = changeQs % 25;
int dimesDue = (changeDs / 10);

// get the remaining after change dime coins, and
// then divide 5 for getting nickel coins
int changeNs = changeDs % 10;
int nickelsDue = (changeNs / 5);

// get the remaining after change nickel coins, and
// then divide 1 for getting 1cent coins
int changePs = changeNs % 5;
int penniesDue = (changePs / 1);

printf("Amount Due: $21.17\nAmount Tendered: $100\n\n");
printf("Change Due:\n(by denomination)\n");
printf("Twenties: %d\n", twentiesDue);
printf("Tens: %d\n", tensDue);
printf("Fives: %d\n", fivesDue);
printf("Ones: %d\n", onesDue);
printf("Quarters: %d\n", quartersDue);
printf("Dimes: %d\n", dimesDue);
printf("Nickels: %d\n", nickelsDue);
printf("Pennies: %d\n", penniesDue);

return 0;
}

关于c - 如果计算的变量不是整数,如何继续执行下一行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718164/

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