如何使我的支票生成器输出确切的输出,而不是“零".在C中 [英] How to make my cheque generator output exactly what it is instead of "Zero" in C

查看:75
本文介绍了如何使我的支票生成器输出确切的输出,而不是“零".在C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Check生成器程序可以完美地处理您输入的任何内容,以使其输出以字为单位的数字.例如,如果我输入"1234.56",它将输出一千三百二十四美元和... 56美分".但是,每当我要输出"1000"之类的内容时,它都会输出一千零美元和... 0美分".显然不是1000零美元,美分完全可以,但是我希望摆脱那个零",但是我需要在那儿输入一些时间,例如如果您键入"0.01",它将输出零美元和1分".

My Cheque generator program has worked flawlessly for any input you give it to make it output the numerals in words. for example if I were to input "1234.56" it will out put "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents". However whenever I want to output something like "1000" it will output "One Thousand Zero Dollars and ... 0 Cents". Obviously its not 1000 Zero dollars, The cents are perfectly fine, however I wish to get rid of that "Zero" but I need that in there for moments such as if you were to type in "0.01" It would output "Zero Dollars and 1 Cent".

代码如下:

#include <stdio.h>

void printNum(int);
void printNum2(int);
int main()
{

int a = 0;
int b = 0;
int c = 0;
int d = 0; 
int num = 0;
int printcents; //To convert the float "cents" to an integer.

float inclusive;
float cents;

printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);

if(inclusive < 0.00 || inclusive >= 10000.00) {
    printf("Sorry, cannot create cheque for that amount, try again next time!\n");
       }
else
{                                             
    a = inclusive / 1000;                          //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
    inclusive = inclusive - (a*1000);
    b = inclusive / 100; 
    inclusive = inclusive - (b*100);
if ( inclusive > 19 ){
    c = inclusive / 10; 
    inclusive = inclusive - (c*10);
}
else
{
    c = inclusive;
    d = 0;
}
    d = inclusive;
    num = inclusive;
    cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
    printcents = cents;


          /*Printing if the variables are in the thousands, hundreds, tens or ones categories.*/
if (a > 0){ 
    printNum(a);  
    printf("Thousand ");
}
if (b > 0){
    printNum(b);
    printf("Hundred ");
}
    printNum2(c);   
if (d > 0){
    printNum(d);
    printf("Dollars and ... ");
}
else if (d == 0){
    printf("Zero Dollars and ... ");
}
    printf("%d", printcents);
    printf(" Cents\n");
    }
}

void printNum(int x)  //Created functions to easily output various if statements.
{

if ( x == 1)
    printf("One ");
else if ( x == 2)
    printf("Two ");
else if (x == 3)
    printf("Three ");
else if (x == 4) 
    printf("Four ");
else if (x == 5)
    printf("Five ");
else if (x == 6)
    printf("Six ");
else if (x == 7)
    printf("Seven ");
else if (x == 8)
    printf("Eight ");
else if (x == 9)
    printf("Nine ");

}
void printNum2(int x)
{
 if ( x == 10)
     printf("Ten ");
 else if ( x == 11)
     printf("Eleven ");
 else  if ( x == 12)
     printf("Twelve ");
 else if ( x == 13)
     printf("Thirteen ");
 else if (x == 14)
     printf("Fourteen ");
 else if (x == 15)
     printf("Fifteen ");
 else if (x == 16)
     printf("Sixteen ");
 else if (x == 17)
     printf("Seventeen ");
 else if (x == 18)
     printf("Eighteen ");
 else if (x == 19)
     printf("Ninteen ");
 else if (x == 2)
     printf("Twenty ");
 else if (x == 3)
     printf("Thirty ");
 else if (x == 4)
     printf("Forty ");
 else if (x == 5)
     printf("Fifty ");
 else if (x == 6)
     printf("Sixty ");
 else if (x == 7)
     printf("Seventy ");
 else if (x == 8)
     printf("Eighty ");
 else if (x == 9)
     printf("Ninety ");
}

推荐答案

inclusive = inclusive-(c * 10)之类的各种减法都出现舍入误差,因此,当做 if(d> 0).

Various subtractions like inclusive = inclusive - (c*10) are suffering round-off error and therefore have a tiny amount left when doing if (d > 0).

建议四舍五入到最接近的0.01.例如: inclusive = round(100 *(inclusive-(c * 10)))/100.0 等位置.

Suggest rounding to nearest 0.01. Example: inclusive = round(100*(inclusive - (c*10)))/100.0 and other places.

或全部以整数分做.

这篇关于如何使我的支票生成器输出确切的输出,而不是“零".在C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆