我试图让我的c程序运行你输入的任何值将作为单词出现,例如,如果我想输入值1234.56它应该出来“一千二百三十四美元......” 56分“请注意分数只是数字。到目前为止,我已经正确编写了能够将数字转换为单词的代码,但我不知道如何只留下单独的分数。请帮忙!!
目前,对于小数点,我在否定数字中得到了一些令人发指的数字!
代码如下:
#include <stdio.h>
void printNum(int);
void printNum2(int);
void printNum3(int);
void printNum4(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0, e=0;
float inclusive;
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.01 || inclusive > 9999.99) {
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 value 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;
e = inclusive; //the variable "e" is for the decimal//
inclusive = inclusive - a - b - c
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.//
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printNum4(e);
printf("Cents\n");
}
}
void printNum(int x)
{
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 printNum3(int x)
{
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 printNum4(int x)
{
printf("%d, e");
}
答案 0 :(得分:1)
为避免int
和float
之间的混淆,并避免两种类型之间的算术运算问题,我建议
答案 1 :(得分:0)
您可以使用FLOOR功能(舍入到最接近实际值的最接近的整数值),单独打印并从原始数字中减去它 - &gt;然后你将得到美分值,你可以乘以100得到它们的整数值。
答案 2 :(得分:0)
您正在隐式地将浮点数转换为int。分配e =包含时。这就是为什么你得到奇怪的数字,不要将整数转换为浮点数,反之亦然,永远不要!
实际上你可以使用modulo获得小数部分。
也许你可以试试这个:
void printNum4(float inclusive)
{
//Decimal part * 100 to get cents
float decimal = fmod(inclusive, 1) * 100;
//Print a float with a 0 decimal precision
printf("%.0f", decimal);
}