#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ConvertN(int n,char* &p,bool IsSequece=true);
bool ConvertSegment(int number,int n,char* &p,bool IsLastZiero);
bool ChangeNumber(double dbNumber,char* lpszvalues);
void main()
{
double db=-1200008.9876;
printf("%lf\n",db);
char buf[100];
if(ChangeNumber(db,buf))
printf("%s\n",buf);
db=1000000.98;
printf("%lf\n",db);
if(ChangeNumber(db,buf))
printf("%s\n",buf);
db=10000008.0987;
printf("%lf\n",db);
if(ChangeNumber(db,buf))
printf("%s\n",buf);
db=10200408.09007;
printf("%lf\n",db);
if(ChangeNumber(db,buf))
printf("%s\n",buf);
db=10020400.007;
printf("%lf\n",db);
if(ChangeNumber(db,buf))
printf("%s\n",buf);
}
void ConvertN(int n,char* &p,bool IsSequece)
{
if(n<0 || n>9)
{
printf("Error:数组超界!\n");
exit(0);
}
char num[]="零壹贰叁肆伍陆柒捌玖";
if(IsSequece)
{
*p++=num[2*n];
*p++=num[2*n+1];
}
else
{
*p++=num[2*n+1];
*p++=num[2*n];
}
}
bool ConvertSegment(int number,int n,char* &p,bool IsLastZiero)
{
char num1[]=" 拾佰仟";
char num2[]="点万亿兆";
if(number==0)
{
if(n==0)
{
*p++=num2[2*n+1];
*p++=num2[2*n];
}
return IsLastZiero;
}
if(IsLastZiero)
{
ConvertN(0,p,false);
}
*p++=num2[2*n+1];
*p++=num2[2*n];
bool flag=true;
for(int i=0;number!=0 && i<4;i++)
{
int m;
m=number%10;
if(m==0 && !flag)
{
flag=true;
ConvertN(0,p,false);
}
else if(m!=0)
{
flag=false;
if(i==0)
{
ConvertN(m,p,false);
}
else
{
*p++=num1[2*i+1];
*p++=num1[2*i];
ConvertN(m,p,false);
}
}
number/=10;
}
if(i>=4)
return false;
else
return true;
}
bool ChangeNumber(double dbNumber,char* lpszvalues)
{
if(dbNumber<0.0000001 && -dbNumber<0.0000001) //数位太小
{
strcmp(lpszvalues,"零");
return true;
}
if(dbNumber>100000000L || -dbNumber>100000000L) //数位太大
return false;
char buf1[50],buf2[50],*p1=buf1,*p2=buf2;
double c=dbNumber;
c=c>0?c:-c;
unsigned long a=(unsigned long)c;
double b=c-a;
for(int i=0;i<6;i++)
{
b*=10;
int n=int(b);
ConvertN(n,p2);
b-=n;
}
*p2='\0';
int count=0;
bool flag=false;
while(a!=0)
{
int buf=a%10000;
flag=ConvertSegment(buf,count++,p1,flag);
a/=10000;
}
char *p=lpszvalues;
if(dbNumber<0)
{
strcpy(p,"负");
p+=2;
}
p1--;
while(p1>=buf1)
*p++=*p1--;
*p='\0';
strcat(lpszvalues,buf2);
return true;
}
运行时老是错误,不知道哪里出错了,希望高人修改一下。