小问题求救,在线急等!

hanxu2008 2004-07-19 01:04:44
请问如何进行数字之间的转换。输入数字输出大写的数字
例如:
输入:1001
输出为:壹千零壹

输入:233
输出:贰佰叁十叁

谢谢!请贴出原码!在线急等!
...全文
153 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
grrrrrr 2004-07-20
  • 打赏
  • 举报
回复
const string NUM = "零壹贰叁肆伍陆柒捌玖";
int num;
cin >> num;

string str;
while (num)
{
str.insert(0, NUM.substr((num%10)*2, 2));
num /= 10;
}

cout << str <<endl;


这种方法写起来简单,但是系统开销比较大
至于系统开销比较小的,我懒得写了~
luotuofen 2004-07-19
  • 打赏
  • 举报
回复
本人想请教下,怎么样把自己要问的问题放在首页啊。我是菜鸟
hellwolf 2004-07-19
  • 打赏
  • 举报
回复
改写了一个不用wcout的,可以处理小数(但精度有些问题)
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

const char* a2h_table[]={"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌","玖"};

void __do_kilo(string& ws,int kilo){
bool get_zero=false;
if(kilo/1000>0){
ws.append(a2h_table[kilo/1000]);
ws.append("千");
kilo%=1000;
get_zero=true;
}

if(kilo/100>0){
ws.append(a2h_table[kilo/100]);
ws.append("百");
kilo%=100;
get_zero=true;
}else {
if(get_zero){ws.append("零");get_zero=false;}
}

if(kilo/10>0 || get_zero){
ws.append(a2h_table[kilo/10]);
ws.append("十");
kilo%=10;
}

if(kilo>0){
ws.append(a2h_table[kilo]);
}
}

void alabo2hanzi(double fl,string& hz){
//The double type contains 64 bits:
//1 for sign, 11 for the exponent, and 52 for the mantissa.
//Its range is +/–1.7E308 with at least 15 digits of precision.
//注意,只处理十五位数字!!
short solved=15;
int in = static_cast<int>(fl);
double after_dot = fl - static_cast<double>(in);
int kilo;
if(in < 0){hz.append("负");in = abs(in);}
//处理整数部分
kilo=in / 100000000;
if(kilo>0){
__do_kilo(hz,kilo);
in%=10000000;
hz.append("亿");
solved-=3;//这里使用比较笨拙且不确切的方法(总是比实际处理位数多)
}

kilo=in / 10000;
if(kilo>0){
__do_kilo(hz,kilo);
in%=10000;
hz.append("万");
solved-=3;
}

__do_kilo(hz,in);
//处理小数部分
if(after_dot>0){
hz.append("点");
after_dot*=10;
for(;solved>0;solved--){
hz.append(a2h_table[static_cast<int>(after_dot)]);
after_dot -= static_cast<int>(after_dot);
after_dot*=10;
}
}
}

int main(){
double j;
string hz;

for (;;hz.clear()){
cin >> j;
alabo2hanzi(j,hz);
cout << j << ":" << hz << endl;
}
}
hellwolf 2004-07-19
  • 打赏
  • 举报
回复
vc7.1可以通过的
lonelyeagle 2004-07-19
  • 打赏
  • 举报
回复
楼上的,编不过,你在什么环境下编译的.
hanxu2008 2004-07-19
  • 打赏
  • 举报
回复
当然加分了,放心吧,一定给你加!
hellwolf 2004-07-19
  • 打赏
  • 举报
回复
我写了一个暂时没有小数功能,加分就写
//范围:-2147483648 ---- 2147483648
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

const wchar_t a2h_table[]={L'零', L'壹', L'贰', L'叁', L'肆', L'伍', L'陆', L'柒', L'捌',L'玖'};

void __do_kilo(wstring& ws,int kilo){
bool get_zero=false;
if(kilo/1000>0){
ws.push_back(a2h_table[kilo/1000]);
ws.push_back(L'千');
kilo%=1000;
get_zero=true;
}

if(kilo/100>0){
ws.push_back(a2h_table[kilo/100]);
ws.push_back(L'百');
kilo%=100;
get_zero=true;
}else {
if(get_zero){ws.push_back(L'零');get_zero=false;}
}

if(kilo/10>0 || get_zero){
ws.push_back(a2h_table[kilo/10]);
ws.push_back(L'十');
kilo%=10;
}

if(kilo>0){
ws.push_back(a2h_table[kilo]);
}
}

void alabo2hanzi(long in,wstring& hz){
int kilo;
if(in < 0){hz.push_back(L'负');in = abs(in);}

kilo=in / 100000000;
if(kilo>0){
__do_kilo(hz,kilo);
in%=10000000;
hz.push_back(L'亿');
}

kilo=in / 10000;
if(kilo>0){
__do_kilo(hz,kilo);
in%=10000;
hz.push_back(L'万');
}

__do_kilo(hz,in);
}

int main(){
wcout.imbue(std::locale("ZHI"));
long j;
wstring hz;

for (;;hz.clear()){
cin >> j;
alabo2hanzi(j,hz);
wcout << j << L':' << hz << endl;
}
}
falcon1210 2004-07-19
  • 打赏
  • 举报
回复
我也是新手,试着写了一个。你看看。
#include <iostream>
#include <string>

using namespace std;

void main()
{
string han[]={"点","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"};
string jin[]={"十","佰","仟","万"};
string no=".123456789";
string input;
cin>>input;
int length1,length2;
for(int y=0;y<=10;y++)
{
if(input[y]=='.')
length1=y;
if(input[y]=='\0')
length2=y;
}
int i=length1;
for(int a=0;a<=length2;a++)
{
if(input[a]=='\0')
break;

for(int k=0;k<=11;k++)
{
if(input[a]==no[k])
cout<<han[k];
}
switch(length1-a)
{
default:
break;
case 5:
cout<<jin[3];break;
case 4:
cout<<jin[2];break;
case 3:
cout<<jin[1];break;
case 2:
cout<<jin[0];break;
}

}
cout<<endl;
}
hanxu2008 2004-07-19
  • 打赏
  • 举报
回复
最好包括小数,本人初学,希望大狭指点一二!谢谢!
beyondtkl 2004-07-19
  • 打赏
  • 举报
回复
还包括小数么....

很简单的哦 还是自己动手吧!!!

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧