我正在为我的计算机科学课程开发一个专注于C语言的项目,我几乎差不多完成了,我只是想不通为什么底部的货币转换功能会丢掉一些零。它打印的数字。我对C很新,并且在我学习的过程中学习,所以如果这段代码没有经过优化和/或对于有经验的人来说很漂亮,我深表歉意。欢迎任何有关变化的建议。下面是示例打印,以显示我的问题和我的代码。
Please enter seed number: 5
Running 26 Times
7374270.50 to Dollars: $7,374,270
-643185.56 to Dollars: -$643,185
7907977.50 to Dollars: $7,907,977
-810537.94 to Dollars: -$810,537
-3670141.50 to Dollars: -$3,670,141
-4847146.50 to Dollars: -$4,847,146
-1805439.50 to Dollars: -$1,805,439
-7718499.50 to Dollars: -$7,718,499
209791.62 to Dollars: $209,791
-3349182.00 to Dollars: -$3,349,182
7803616.50 to Dollars: $7,803,616
7607553.50 to Dollars: $7,607,553
-232233.81 to Dollars: -$232,233
4807206.50 to Dollars: $4,807,206
-4712548.00 to Dollars: -$4,712,548
-253349.20 to Dollars: -$253,349
-7257441.50 to Dollars: -$7,257,441
-5146261.50 to Dollars: -$5,146,261
-324208.03 to Dollars: -$324,208
3437480.75 to Dollars: $3,437,480
-295008.59 to Dollars: -$295,8
118086.98 to Dollars: $118,86
1243459.88 to Dollars: $1,243,459
5947147.50 to Dollars: $5,947,147
3280724.00 to Dollars: $3,280,724
-2176277.75 to Dollars: -$2,176,277
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
> #include <time.h> /*extra includes are allowed (and required)*/
>
> void currencyConvert(int money, int cents); void
> PrettyPrintMoney(float money, int cents); /*extra functions are
> allowed to be declared*/
>
> const int MAX_NUMBER_OF_LOOPS = 30; //do not change this line const
> int MIN_DOLLAR_AMOUNT = -8000000; //do not change this line const int
> MAX_DOLLAR_AMOUNT = 8000000; //do not change this line const int
> TOTAL_LENGTH = 15; //do not change this line
>
> int main(void) { unsigned int intSeed = 0; unsigned int intLoops =
> 0; int randGenerated = 0; int money = 0; int cents = 0; time_t
> t; /*extra variables are allowed to be defined*/ printf("Please
> enter seed number: "); scanf("%u", &intSeed);
>
> srand(intSeed);
> intLoops = rand()%30 + 1; printf("\n Running %d Times", intLoops);
> //printf("\n%u", intLoops); srand(t + intLoops);
>
> while (intLoops > 0){
> randGenerated = rand() % 16000001 + (-8000000); cents = rand() % 100; money = randGenerated; PrettyPrintMoney(money, cents);
> currencyConvert(money, cents); intLoops = intLoops -1;
> } return 0; } //end main void PrettyPrintMoney(float money, int cents)//float fltMoneyIn) { ; if (money < 0 && money < -999999){
> printf("\n%15.f.%02d to Dollars: -$", money, cents); } if (money < 0
> && money > -999999){ printf("\n%15.f.%02d to Dollars: -$", money,
> cents); } if (money > 0 && money > 999999){ printf("\n%15.f.%02d to
> Dollars: $", money, cents); } if (money > 0 && money < 999999){
> printf("\n%15.f.%02d to Dollars: $", money, cents); } return; }
> //end PrettyPrintMoney
>
> // Change money to currency (commas) void currencyConvert (int
> money,int cents){
> int order_of_magnitude = (money == 0) ? 1 : (int)pow( 10, ((int)floor(log10(abs(money))) / 3) * 3 ) ;
>
> printf( "%d", abs(money / order_of_magnitude ), cents) ;
>
> for( money = abs( money ) % order_of_magnitude, order_of_magnitude /= 1000;
> order_of_magnitude > 0;
> money %= order_of_magnitude, order_of_magnitude /= 1000 )
> {
> printf( ",%03d", abs(money / order_of_magnitude) ) ;
>
> } printf(".%02d", cents); }
>
>
>
> /*extra functions are allowed to be defined*/