我的Check生成器程序可以完美地处理您输入的任何输入,使其输出数字。 例如,如果我输入“1234.56”,它将输出“一千二百三十四美元和…… 56美分”。 然而,每当我想输出类似“1000”的东西时,它将输出“一千零美元和…… 0美分”。 显然它不是1000零美元,美分非常好,但我希望摆脱那个“零”,但我需要在那里等片刻,如果你输入“0.01”它会输出“零美元和1分“。
代码如下:
#include 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 = 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(" Centsn"); } } 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)
时会有很小的余量。
建议舍入到最接近0.01。 示例: inclusive = round(100*(inclusive - (c*10)))/100.0
等地方。
或者以整数分数重新执行所有操作。
以上就是c/c++开发分享如何使我的检查发生器输出完全正确,而不是C中的“零”相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/523122.html