人民币转换
题目描述
考试题目和要点:
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
人民币捌佰玖拾捌元贰分