| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3700 人关注过本帖
标题:[求助]编一个把数字转化成汉字念法的程序
只看楼主 加入收藏
bluesky1322
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-10-11
收藏
得分:0 

我是新手,这是我昨晚编的程序,编译环境是MS_VC++6.0,请大家多多指教啊:

#include<iostream>
#include<string>
using namespace std;

int n[5];

//检测数字
int check(int a)
{
if(a>=100000)
{
cout<<"\n输入的数字不能超过5个!没看见吗?\n\n";
return 0;
}
else return 1;
}

//提取数字
int get(int num)
{
int i;

for(i=0;i<5;i++)
{
n[i]=num%10;
num=(num-n[i])/10;
}

return n[i];
}

//转换成汉字
string convert(int a)
{
string c1[10];
switch (a)
{
case 0:
c1[0]="零";
break;
case 1:
c1[1]="一";
break;
case 2:
c1[2]="二";
break;
case 3:
c1[3]="三";
break;
case 4:
c1[4]="四";
break;
case 5:
c1[5]="五";
break;
case 6:
c1[6]="六";
break;
case 7:
c1[7]="七";
break;
case 8:
c1[8]="八";
break;
case 9:
c1[9]="九";
break;
}

return c1[a];
}

//显示单位

string danwei(int a)
{
string c2[5];
switch(a)
{
case 0:
c2[0]="";
break;
case 1:
c2[1]="十";
break;
case 2:
c2[2]="百";
break;
case 3:
c2[3]="千";
break;
case 4:
c2[4]="万";
break;
}
return c2[a];
}

//打印结果
void print()
{
int i,flag1=-1,flag2=-1;

for(i=0;i<5;i++)
if(n[i]!=0) flag1=i;
for(i=4;i>=0;i--)
if(n[i]!=0) flag2=i;

if(flag1!=-1&&flag2!=-1)
{
cout<<"\n转换为汉字为:";
for(i=flag1;i>=flag2;i--)
{
if(n[i]!=0) cout<<convert(n[i])<<danwei(i);
else
if(n[i+1]!=0)
cout<<convert(n[i]);
}
cout<<endl<<endl;
}
else
cout<<"\n转换为汉字为:零\n\n";
}

int main()
{
cout<<"请输入阿拉伯数字(不超过5个):";
int num;
cin>>num;

if(check(num))
{
get(num);
print();
}

return 0;
}


2006-10-12 10:21
半神巫妖
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-7-14
收藏
得分:0 

不用写这么长吧?我也写了一个,还有点缺陷,不过基本可用了
[CODE]#include <iostream>
using namespace std;
char * converion(unsigned _int64 ulData)
{
char sz[128];
static char szResult[128];
unsigned int i, count = 0, ulLength = 0;
static char *szByte[]= {"零", "一","二","三","四","五","六","七","八","九"};
static char *szDigit[] = {NULL, "十", "百", "千", "万", "十", "百", "千", "亿"};
static unsigned int ulByteLen = sizeof(szByte) / sizeof(char*);
static unsigned int ulDigitLen = sizeof(szDigit) / sizeof(char*);

memset(szResult, 0, sizeof(szResult));
_ui64toa(ulData, sz, 10);
ulLength = strlen(sz);
for (i = 0; i < ulLength; i ++)
{
count = sz[i] - 48;
if (0 == count)
{
count = ( ulLength - i - 1) % (ulDigitLen - 1);
if (count == 4 || count == 8)
strcat(szResult, szDigit[count]);
continue;
}
strcat(szResult, szByte[count]);
count = ( ulLength - i - 1) % (ulDigitLen - 1);
if (0 == count)
{
count = (( ulLength - i - 1) / (ulDigitLen - 1) > 0) ? 8 : 0;
}
if (szDigit[count])
{
strcat(szResult, szDigit[count]);
}
}
return szResult;
}
int main(void)
{
unsigned _int64 ulInput;
char * sz;
ulInput = 5764607523134234879;
sz = converion(ulInput);
cout<<sz<<endl;

return 0;
}[/CODE]


[此贴子已经被作者于2006-10-12 18:07:04编辑过]


2006-10-12 18:01
bluesky1322
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-10-11
收藏
得分:0 


楼上的程序,如果把输入的数字改为:4205(四千二百零五),输出则是:四千二百五(4250),把零忽略了。


2006-10-12 21:15
半神巫妖
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-7-14
收藏
得分:0 
楼上的朋友说对了,昨天正好下班了,没来得及改,刚修改了一下,应该没什么错误了

[CODE]char * converion(unsigned _int64 ulData)
{
char sz[128];
static char szResult[128];
unsigned int i, count = 0, ulLength = 0, ulPre = 0xFFFFFFFF;
static char *szByte[]= {"零", "一","二","三","四","五","六","七","八","九"};
static char *szDigit[] = {NULL, "十", "百", "千", "万", "十", "百", "千", "亿"};
static unsigned int ulByteLen = sizeof(szByte) / sizeof(char*);
static unsigned int ulDigitLen = sizeof(szDigit) / sizeof(char*);

memset(szResult, 0, sizeof(szResult));
_ui64toa(ulData, sz, 10);
ulLength = strlen(sz);
for (i = 0; i < ulLength; i ++)
{
count = sz[i] - 48;
if (0 == count)
{
count = ( ulLength - i - 1) % (ulDigitLen - 1);
if (count == 4 || count == 8)
{
strcat(szResult, szDigit[count]);
ulPre = 1;
}
else
{
ulPre = 0;
}
continue;
}
if ( ulPre == 0 )
{
strcat(szResult, szByte[0]);
}
ulPre = count;
strcat(szResult, szByte[count]);
count = ( ulLength - i - 1) % (ulDigitLen - 1);
if (0 == count)
{
count = (( ulLength - i - 1) / (ulDigitLen - 1) > 0) ? 8 : 0;
}
if (szDigit[count])
{
strcat(szResult, szDigit[count]);
}
}
return szResult;
}[/CODE]

2006-10-13 17:30
bluesky1322
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-10-11
收藏
得分:0 

呵呵,这个程序有意思阿。


2006-10-14 20:22
快速回复:[求助]编一个把数字转化成汉字念法的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012271 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved