#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main()
{
double x, y;
char *ch[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
char *ch1[] = { "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" };
char num[256];
long i, n, j, m, y1;
/* Maximum value that can be returned by the rand function. */
/* #define RAND_MAX 0x7fff */
/*上面的定义是从文档中取得,
他定义了RAND_MAX = 0x7fff(也就是10进制的32767)*/
/*这句话的意思是rand()得到的随机数与1000000000进行求余,
即x的最大值为 32767 % 1000000000,还是等于原数呀!
所以可以这样理解:
(x = rand() % 1000000000;) == (x = rand();)*/
/*如果你要随机0~100000000的数则可以这样写:*/
double number[3] = {0};/*number[0]和number[1]作为乘数,number[3]作为常系数*/
srand((unsigned)time(NULL));
number[0] = rand();
printf("Number[0]: %lf\n", number[0]);
number[1] = rand();
printf("Number[1]: %lf\n", number[1]);
number[2] = (RAND_MAX*RAND_MAX) % 100000000;
if (number[0] * number[1] > number[2])
x = number[0] * number[1] - number[2];
else x = number[0] * number[1];
printf("Input: %lf\n", x);
/*这是我自己改的*/
/*
srand((unsigned)time(NULL));
x = rand() % 1000000000;
*/
/*.....................*/
n = (long)x;
y = x - n;/*得小数部分*/
for (i = 0; n != 0; i++)
{
/*ERROR num是字符串不能这样复制需要num[i]的形式*/
/*num = (char)(n % 10);*/
num[i] = (char)(n % 10);
n /= 10;
}
m = i;
/*num = '.';*/
num[i] = '.';
for (y = y * 10; (y - (long)y) * 10 > 0;)
y *= 10;/*小数转化为整数如0.11111转为11111.00...*/
y1 = (long)y;
for (i = m + 1; y1 != 0; i++)
{
/*num = (char)(y1 % 10);*/
num[i] = (char)(y1 % 10);
y1 = y1 / 10;
}/*取各位上的数字*/
printf("Output: ");
for (n = 0;; n++)
{
if (num[n] == '.')
{
for (j = n - 1; j >= 0; j--)/*判断是否是万位,亿位..如是再判断是否是0如是就不输出零.*/
{
if (m <= 5)
if (m == 5 && (int)num[j] == 0)
;
else
printf("%s", ch[(int)num[j]]);/*输出大写壹..*/
else
if (m % 4 == 0 && (int)num[j] == 0)
;
else
printf("%s", ch[(int)num[j]]);
if (m >= 2)
{
printf("%s", ch1[m - 2]);/*输出拾佰仟..如有2位就输出拾*/
m = m--;
}
}
printf("点");
break;
}
}
for (i = i - 1; num[i] != '.'; i--)
/*printf("%s", ch[(int)num]);*//*输出小数部分*/
printf("%s", ch[(int)num[i]]);
printf("零\n");
getchar();
}
以下是三次运行结果,number[0]和number[1]是生成的两个随机数:
|