如何让我的if语句输出正确的printf C程序
How to make my if statement output the correct printf C program
在下面的代码中我做了一个程序,可以把你投入的钱数转换成文字。例如,“1234.56”变成"One Thousand Two Hundred Thirty Four and ... 56 Cents"。问题是,当我尝试将金额“0.01”输入我的 scanf
时,输出变为 "Dollars and ... 1 Cents" 而它应该输出 "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 [=10=].01 to 99.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 (c == 0 && b == 0 && a == 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("Nineteen ");
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 ");
}
很快看,但是:
if (d > 0){
.....
同样,这是一个快速的外观,我没有测试它。
您可以使用嵌套 if else
。首先检查它是否为零美元,否则执行您的过程。尝试这样的事情:
if (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... ");
}
else{
if (a > 0){
printNum(a);
printf("Thousand ");
}
if (b > 0){
printNum(b);
printf("Hundred ");
}
printNum2(c);
if (d >= 0){
printNum(d);
printf("Dollars and ... ");
}
}
而不是
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 (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... ");
因为在您的代码中 if(d>=0)
满足,它永远不会进入 else if
语句(如果前面的任何语句为真,else if
将跳过)。
在下面的代码中我做了一个程序,可以把你投入的钱数转换成文字。例如,“1234.56”变成"One Thousand Two Hundred Thirty Four and ... 56 Cents"。问题是,当我尝试将金额“0.01”输入我的 scanf
时,输出变为 "Dollars and ... 1 Cents" 而它应该输出 "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 [=10=].01 to 99.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 (c == 0 && b == 0 && a == 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("Nineteen ");
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 ");
}
很快看,但是:
if (d > 0){
.....
同样,这是一个快速的外观,我没有测试它。
您可以使用嵌套 if else
。首先检查它是否为零美元,否则执行您的过程。尝试这样的事情:
if (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... ");
}
else{
if (a > 0){
printNum(a);
printf("Thousand ");
}
if (b > 0){
printNum(b);
printf("Hundred ");
}
printNum2(c);
if (d >= 0){
printNum(d);
printf("Dollars and ... ");
}
}
而不是
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 (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... ");
因为在您的代码中 if(d>=0)
满足,它永远不会进入 else if
语句(如果前面的任何语句为真,else if
将跳过)。