gpt4 book ai didi

C程序,while循环不接受小数

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

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void) {
double tendered;
double changeDue;
double price;
int hundred;
int fivty=0;
int twenty=0;
int ten=0;
int five=0;
int toonoe=0;
int loonie=0;
int quarter=0;
int dime=0;
int nickle=0;
int penny=0;
double zeroValue=0.001;

/* Statements to be executed */
printf("Total purchase price and tendered amount\n");
scanf("%lf %lf ", &price, &tendered);
printf(" %lf and %lf is \n", tendered,price);
changeDue=tendered-price;
printf("%lf \n", changeDue);

if(tendered<price){
printf("Not enough money recieved as payment \n");
}

if(tendered==price){
printf("Exact payment, no change given \n");
}

if(tendered>price){
printf("%lf Amount to be paid is ", changeDue);
}


while(changeDue<zeroValue){
if(changeDue>=100.00){
changeDue=changeDue-100.00;
hundred=hundred+1;
}
if((changeDue>=50.00)&&(changeDue<100)){
changeDue=changeDue-20.00;
twenty=twenty+1;
}

if((changeDue>=20.00)&&(changeDue<50)){
changeDue=changeDue-20.00;
twenty=twenty+1;
}
if((changeDue>=10)&&(changeDue<20)){
changeDue=changeDue-10.00;
ten=ten+1;
}
if((changeDue>=5)&&(changeDue<10)){
changeDue=changeDue-5.00;
five=five+1;
}
if((changeDue>=2)&&(changeDue<5)){
changeDue=changeDue-2.00;
toonoe=toonoe+1;
}
if
((changeDue>=1)&&(changeDue<2)){
changeDue=changeDue-1.00;
loonie=loonie+1;
}

if((changeDue>=0.25)&&(changeDue<1)){
changeDue=changeDue-0.25;
quarter=quarter+1;
}
if((changeDue>=0.10)&&(changeDue<0.25)){
changeDue=changeDue-0.10;
dime=dime+1;
}
if((changeDue>=0.05)&&(changeDue<0.05)){
changeDue=changeDue-0.05;
nickle=nickle+1;
}
if((changeDue>=0.01)&&(changeDue<0.05)){
changeDue=changeDue-0.01;
penny=penny+1;
}
if((changeDue==0)||(changeDue==0.000000)){
break;
}

}


if(hundred!=0){
printf("%d hundred$ bills given as change \n",hundred);
}
if(twenty!=0){
printf("%d twenty$ bills given as change \n",twenty);
}
if(ten!=0){
printf("%d ten$ bills given as change \n",ten);
}
if(five!=0){
printf("%d five$ bills given as change \n",five);
}
if(toonoe!=0){
printf("%d toonie coins given as change \n",toonoe);
}
if(loonie!=0){
printf("%d loonie coins given as change \n",loonie);
}
if(quarter!=0){
printf("%d quarter coins given as change \n",quarter);
}
if(dime!=0){
printf("%d dime coins given as change \n",dime);
}
if(nickle!=0){
printf("%d nicke coins given as change \n",nickle);
}
if(penny!=0){
printf("%d penny coins given as change \n",penny);
}

return 0;
}

我对 while 循环有疑问,如果除了零之外没有对应的值,它适用于小数。我的 while 循环看起来像这样。 change Due 变量被初始化为 double 型。

我添加了以下代码作为示例以进一步说明。我还是很困惑。

while(changeDue!=0){

}

最佳答案

首先

while(changeDue<zeroValue)应该 while(changeDue>zeroValue)

实际上,您最好跳过外循环和内分支,而是执行以下操作:

while(changeDue>=0.25){
changeDue=changeDue-0.25;
quarter=quarter+1;
}
while(changeDue>=0.10){
changeDue=changeDue-0.10;
dime=dime+1;
}

否则,请考虑当changeDue 是例如时会发生什么。 51 美分。

正如评论中所说,当然,像这样的问题应该作为除法和余数问题来解决,但我认为这应该是控制结构的练习。

关于C程序,while循环不接受小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48246940/

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