华为OJ——人民币转换

人民币转换

题目描述

考试题目和要点:

1、中文大写金额数字前应标明人民币字样。中文大写金额数字应用壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整等字样填写。(30分) 

2、中文大写金额数字到为止的,在之后,应写整字,如¥ 532.00应写成人民币伍佰叁拾贰元整。在后面不写整字。(30分) 

3、阿拉伯数字中间有“0”时,中文大写要写字,阿拉伯数字中间连续有几个“0”时,中文大写金额中间只写一个字,如¥6007.14,应写成人民币陆仟零柒元壹角肆分。(

输入描述:

输入一个double

输出描述:

输出人民币格式

输入例子:

151121.15

输出例子:

人民币拾伍万壹仟壹佰贰拾壹元壹角伍分

解答代码:

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;

string table[11]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
string table2[4]={"仟","佰","拾"};
int flag=1;
string change(string s)
{
	int i,index=0;
        string result="";
	char ss[4]="";
	//过滤无效的前导0
	for(i=0;i<s.length()&&s[i]=='0';i++);
        for(;i<s.length();i++)
		ss[index++]=s[i];
	ss[index]='\0';
	int length=strlen(ss);
	//记录后导0
	int record=0;
	for(i=length-1;i>=0;i--)
		if(ss[i]=='0')
			record++;
		else 
			break;
	if(record>0)
		flag=0;
	int k=4-length;
        //2位含十的有点特殊
	if(length==2)
	{
                if(ss[0]=='1' && ss[1]=='0')
			return "拾";
		if(ss[0]=='1'  && ss[1]!='0')
			return "拾"+table[ss[1]-'0'];
		if(ss[0]!='1' && ss[1]=='0')
			return table[ss[0]-'0']+"拾";
		if(ss[0]!='1' && ss[1]!='0')
			return table[ss[0]-'0']+"拾"+table[ss[1]-'0'];
	}

	for(i=0;i<length-record;i++,k++)
	{
		if(ss[i]!='0')
		{
			result+=table[ss[i]-'0']+table2[k];
		}
		else if(i!=0 && ss[i]!=ss[i-1])
			result+="零";
	}
	return result;
}

int main()
{ 
	string data="";
	string s1="",s2="";
	
	while(cin>>data)
	{
		int pos=data.find('.');
		if(pos!=string::npos)
		{
			s1=data.substr(0,pos);
			s2=data.substr(pos+1);
		}
		else
		{
			s1=data;
		}
		cout<<"人民币";
		if(s1.length()>8)
		{
			string temp1=s1.substr(0,s1.length()-8);
			s1=s1.substr(s1.length()-8,s1.length());
			string result1=change(temp1);
			if(result1.length()!=0)
				cout<<result1<<"亿";
		}
		if(s1.length()>4)
		{
			string temp2=s1.substr(0,s1.length()-4);
			s1=s1.substr(s1.length()-4,s1.length());
			string result2=change(temp2);
			if(result2.length()!=0)
				cout<<result2<<"万";
		}
		if(s1.length()>0)
		{
			string result3=change(s1);
			if(result3.length()!=0)
				cout<<result3<<"元";
		}
		if(s2.length()==0 || (s2[0]=='0' && s2[1]=='0') )
			cout<<"整"<<endl;
		else
		{
			if(s2[0]=='0'&&s2[1]!='0')
			{
				cout<<table[s2[1]-'0']<<"分"<<endl;
				continue;
			}
			if(s2[0]!='0'&&s2[1]=='0')
			{
				cout<<table[s2[0]-'0']<<"角"<<endl;
				continue;
			}
			if(s2[0]!='0'&&s2[1]!='0')
			{
				cout<<table[s2[0]-'0']<<"角"<<table[s2[1]-'0']<<"分"<<endl;
			}
		}
	}
	
	return 0;
}
测试输出(输入数据包含2位小数!):
1.00
人民币壹元整
1.01
人民币壹元壹分
1.10
人民币壹元壹角
45.78
人民币肆拾伍元柒角捌分
898.02
人民币捌佰玖拾捌元贰分

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值