数据结构与算法分析——c语言描述 习题1.3
#include <stdio.h>
void printOut(int N);
void printReal(double N, int decPlace);
void printDecimal(double decimal, int decPlace);
int main(void)
{
printReal(1002.94, 2);
return 0;
}
void printReal(double N, int decPlace)
{
int integer = ((int)N);
double decimal = N - (double)integer;
if (N < 0)
{
N = -N;
putchar('-');
}
printOut(integer);
printDecimal(decimal, decPlace);
return;
}
void printDecimal(double decimal, int decPlace)
{
int i = 0;
double roundOffRadix = 0.5;
if (decPlace == 0)
{
return;
}
putchar('.');
for (i = 0; i < decPlace; i++)
{
roundOffRadix /= 10;
}
decimal += roundOffRadix;
for (i = 0; i < decPlace; i++)
{
decimal *= 10;
printOut((int)decimal);
decimal -= (int)decimal;
}
return;
}
void printOut(int N)
{
if ((N / 10) != 0)
{
printOut(N / 10);
}
printf("%d", N % 10);
return;
}
|