//
// main.m
// 盖世天地
//
// Created by Ibokan on 12-12-21.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
//void StrClear(char *str);
//void StrClear(char *str)
//{
// char *s=str;
// while(*s)
// {
// *s=NULL;
// s++;
// }
//}
int main (int argc, const char * argv[])
{
@autoreleasepool {
int temp=100902304;//随机的一个小于20亿的数字。
char *a[19]={"零","一","二","三","四","五","六","七","八","九","十","百","千","万","十万","百万","千万","亿","十亿"};//输出时候用的。
char s[30]; //求长度时候用的。
sprintf(s, "%d", temp);//整型转化为字符串。
int len=strlen(s);//随机数字的长度。
int i=0;//从个位数字开始。
char *result[100];//存储每位数字结果的字符串数组。
//循环从个位数开始把结果存入result数组中。
int flag=0;//判断万的时候。
int flag2=0;//判断亿的时候。
while (len>0) {
//生成个位数字并放在结果数组中。
if (i==0) {
result[i]=a[temp%10];//13651281564
}
//判断不是个位数的时候。需要一个拼接并放到数组中。
if (i>0) {
char *res=malloc(100);//字符串拼接的时候用的。
char *res2=malloc(100);//字符串拼接的时候用的。
strcpy(res2, "");
strcpy(res, "");
strcat(res, a[temp%10]);
strcpy(res2, res);
if ((strcmp(res, "零")!=0)&&i<8) {
if (flag!=1) {
strcat(res2, a[9+i]);
}
if (flag==1) {//如果前面有万,后面判断的时候把万去掉。
strcat(res2, a[5+i]);
}
if (strstr(a[9+i], "万")) {
flag=1;
}
}
else {
if ((strcmp(res, "零")!=0)) {
if (flag2!=1) {
strcat(res2, a[9+i]);
}
if (flag2==1) {//如果前面有亿,后面判断的时候把万去掉。
strcat(res2, a[1+i]);
}
if (strstr(a[9+i], "亿")) {
flag2=1;
}
}
}
result[i]=res2;
printf("%s======\n",result[i]);//此时输出的是正常的:五十,二百,四千。
}
i++;
len--;
temp=temp/10;
}
if (i==1) {
printf("%s",result[i-1]);
}
else{
//输出结果。
while(i>1) {
if ((strcmp(result[i-1], "零")==0)&&(strcmp(result[i-2], "零")!=0)) {
printf("%s",result[i-1]);
}
else if((strcmp(result[i-1], "零")!=0)) {
printf("%s",result[i-1]);
}
i--;
}
if ((i==1)&&(strcmp(result[0], "零")!=0)) {
printf("%s",result[0]);
}
}
}
return 0;
}