Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使我的支票生成器准确地输出它的内容,而不是“输出”;“零”;在C中_C_Compilation_Generator_Zero_Currency - Fatal编程技术网

如何使我的支票生成器准确地输出它的内容,而不是“输出”;“零”;在C中

如何使我的支票生成器准确地输出它的内容,而不是“输出”;“零”;在C中,c,compilation,generator,zero,currency,C,Compilation,Generator,Zero,Currency,我的支票生成器程序可以完美地处理你给它的任何输入,让它以文字形式输出数字。例如,如果我输入“1234.56”,它将输出“一千二百三十四美元和……56美分”。然而,每当我想输出像“1000”这样的东西时,它就会输出“一千零零美元和……0美分”。很明显,它不是1000零美元,美分是非常好的,但是我希望去掉“零”,但是我需要它在那里,如果你输入“0.01”,它将输出“零美元和1美分” 代码如下: #include <stdio.h> void printNum(int); void pr

我的支票生成器程序可以完美地处理你给它的任何输入,让它以文字形式输出数字。例如,如果我输入“1234.56”,它将输出“一千二百三十四美元和……56美分”。然而,每当我想输出像“1000”这样的东西时,它就会输出“一千零零美元和……0美分”。很明显,它不是1000零美元,美分是非常好的,但是我希望去掉“零”,但是我需要它在那里,如果你输入“0.01”,它将输出“零美元和1美分”

代码如下:

#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 ");
}
#包括
void printNum(int);
无效printNum2(int);
int main()
{
int a=0;
int b=0;
int c=0;
int d=0;
int num=0;
int printcents;//将浮点“美分”转换为整数。
浮动(含浮动);
浮动美分;
printf(“欢迎使用IPC144支票生成器!!\n”);
printf(“按照……amahmood29(018359133)的订单付款”);
printf(“输入从$0.01到$9999.99(含)的货币值:”;
scanf(“%f”,含)(&C);
如果(包括<0.00 | |包括>=10000.00){
printf(“对不起,无法为该金额创建支票,请下次再试!\n”);
}
其他的
{                                             
a=inclusive/1000;//此数据通过将任何变量除以1000、100、10来替换我们的变量。
包含=包含-(a*1000);
b=含/100;
包含=包含-(b*100);
如果(包括>19){
c=含/10;
包含=包含-(c*10);
}
其他的
{
c=包括在内;
d=0;
}
d=包括在内;
num=包括在内;
仙=(包括-num)*100;//用数字计算我们的“仙”。
打印美分=美分;
/*如果变量属于千、百、十或一类,则打印*/
如果(a>0){
printNum(a);
printf(“千”);
}
如果(b>0){
printNum(b);
printf(“百”);
}
printNum2(c);
如果(d>0){
printNum(d);
printf(“美元和…”);
}
else如果(d==0){
printf(“零美元和…”);
}
printf(“%d”,printfents);
printf(“美分\n”);
}
}
void printNum(int x)//创建函数以轻松输出各种if语句。
{
如果(x==1)
printf(“一”);
else如果(x==2)
printf(“两个”);
else如果(x==3)
printf(“三”);
else如果(x==4)
printf(“四”);
else如果(x==5)
printf(“五”);
else如果(x==6)
printf(“六”);
else如果(x==7)
printf(“七”);
else如果(x==8)
printf(“八”);
else如果(x==9)
printf(“九”);
}
void printNum2(int x)
{
如果(x==10)
printf(“十”);
else如果(x==11)
printf(“十一”);
else如果(x==12)
printf(“十二”);
else如果(x==13)
printf(“十三”);
else如果(x==14)
printf(“十四”);
else如果(x==15)
printf(“十五”);
else如果(x==16)
printf(“十六”);
else如果(x==17)
printf(“十七”);
else如果(x==18)
printf(“十八”);
else如果(x==19)
printf(“Ninten”);
else如果(x==2)
printf(“二十”);
else如果(x==3)
printf(“三十”);
else如果(x==4)
printf(“四十”);
else如果(x==5)
printf(“五十”);
else如果(x==6)
printf(“六十”);
else如果(x==7)
printf(“七十”);
else如果(x==8)
printf(“八十”);
else如果(x==9)
printf(“九十”);
}

各种减法,如
inclusive=inclusive-(c*10)
都有舍入误差,因此在执行
if(d>0)
时只剩下少量的减法

建议四舍五入至最接近的0.01。示例:
inclusive=round(100*(inclusive-(c*10)))/100.0
和其他位置


或者以整数分重新执行所有操作。

如果(d==0)更改
else{
-->
如果(d==0&&c==0&&b==0&&a==0)更改
else{
float
中存储货币或任何需要精确精度的值很少是个好主意,因为二进制浮点数不能准确表示小数。
double
可能足以显示支票上实际显示的值,但即使如此,还是最好使用十进制类型(对于此用途,只需使用整数美分,您就可以轻松地制作十进制定点表示法)。正确缩进代码将使其更易于阅读。当金额错误时退出也将避免缩进级别。@chux感谢我使用了您希望我更改的代码,一切都很好,但是当我在scanf中输入“0.01”时,我得到的只是“美元和…1美分”,我能做什么,使它显示“零美元和1美分”?由于典型的浮点问题,您的
a、b、c、d
可能有一个较小的剩余值。我将取消删除以前的asnwer-该建议可能会有所帮助。总之,我建议使用整数数学重新写入。