如何使我的输出仅显示我的 c 支票生成器程序所需的内容
How to make my output show only whats needed with my cheque generator program for c
我制作了一个程序,可以输出输入的任何数字,但它会将其转换为单词,而不仅仅是数字,例如,如果您输入“1234.56”,它会将其转换为 "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents"。美分应始终以数字表示。到目前为止,一切都很好,但是如果我输入的金额少于一千,我会在其中得到多余的词,例如 "Thousand" 或 "Hundred"。例如,如果我输入“15.77”,我的输出将是 "Thousand Hundred Fifteen Dollars and ... 77 Cents"。
我不希望有千位或百位,没有它们就完美了!
代码如下:
#include <stdio.h>
void printNum(int);
void printNum2(int);
void printNum3(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 [=10=].01 to 99.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.01 || 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;
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 ... ");
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 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 ");
}
我已经编码整整一个月了,所以如果看起来我犯了一些简单的错误,那就是原因。
您需要在 printfs 周围添加条件:
if (a > 0)
{
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
}
问题是你在无条件打印"Thousand"、"Hundred"等...
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 ... ");
printf("%d", printcents);
printf(" Cents\n");
如果您发送给 printNum 的数字为零,您不想打印出您的文本字符串,您必须检查该条件:
/* call to printNum x */
if ( /* check if the parameter to printNum matches any case, seems to be if not zero){
printf(/* whatever string is appropriate */);
}
举个例子,假设输入是 512.26
512.26/1000 <<< 应该是 512.26/ 1000.f 所以除法的两个操作数都是浮点数
将导致浮点值:0.51226f NOT 0.0f
使用 floor(0.51226f) 将导致 0.0f
建议始终应用 floor() 函数产生 0.0f
即使使用浮点数,也可以进行比较,如 if( value != 0.0f ) 然后打印它
将在应用 floor() 函数后工作。
我制作了一个程序,可以输出输入的任何数字,但它会将其转换为单词,而不仅仅是数字,例如,如果您输入“1234.56”,它会将其转换为 "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents"。美分应始终以数字表示。到目前为止,一切都很好,但是如果我输入的金额少于一千,我会在其中得到多余的词,例如 "Thousand" 或 "Hundred"。例如,如果我输入“15.77”,我的输出将是 "Thousand Hundred Fifteen Dollars and ... 77 Cents"。 我不希望有千位或百位,没有它们就完美了!
代码如下:
#include <stdio.h>
void printNum(int);
void printNum2(int);
void printNum3(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 [=10=].01 to 99.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.01 || 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;
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 ... ");
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 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 ");
}
我已经编码整整一个月了,所以如果看起来我犯了一些简单的错误,那就是原因。
您需要在 printfs 周围添加条件:
if (a > 0)
{
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
}
问题是你在无条件打印"Thousand"、"Hundred"等...
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 ... ");
printf("%d", printcents);
printf(" Cents\n");
如果您发送给 printNum 的数字为零,您不想打印出您的文本字符串,您必须检查该条件:
/* call to printNum x */
if ( /* check if the parameter to printNum matches any case, seems to be if not zero){
printf(/* whatever string is appropriate */);
}
举个例子,假设输入是 512.26
512.26/1000 <<< 应该是 512.26/ 1000.f 所以除法的两个操作数都是浮点数
将导致浮点值:0.51226f NOT 0.0f
使用 floor(0.51226f) 将导致 0.0f
建议始终应用 floor() 函数产生 0.0f
即使使用浮点数,也可以进行比较,如 if( value != 0.0f ) 然后打印它
将在应用 floor() 函数后工作。