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

时间:2018-01-14 05:24:38

标签: c

#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){

}

1 个答案:

答案 0 :(得分:0)

首先

const mongoose = require('mongoose'), Schema = mongoose.Schema; var CategorySchema = new Schema({ categories:[{ name: { type: String, required: true, trim: true }, value: { type: Number, required: true }, createdAt: { type: Date, default: Date.now }, updatedAt: { type: Date, default: Date.now }, subCategories: [{ name: { type: String, required: true, trim: true }, value: { type: Number, required: true } }] }] }) 应该 while(changeDue<zeroValue)

实际上,你最好不要跳过外环和内部分支,而是做一些像:

while(changeDue>zeroValue)

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

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