币值转换

7-1 币值转换 (20 分)
输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。
思路:先判断亿位有没有数,依次判断所有位数有没有数字。

include <stdio.h>

include <stdlib.h>

char item[11]="abcdefghij";
char loca[5]="QBSA";
void transport(int num[],int n)
{
int location=0;
while(num[location]0)
location++;
if(num[location-1]
0&&location!=0)
printf("%c",item[0]);
while(location<=n)
{
if(num[location]!=0&&location<=n)
{
if(location!=n)
printf("%c%c",item[num[location]],loca[4-n-1+location]);
else
printf("%c",item[num[location]]);
}
else if(num[location]0&&num[location+1]!=0&&location<=n-1)
printf("%c",item[0]);
location++;
}
return;
}
int main()
{
char p[10];
char ch;
int num[9];
int i=0;
while((ch=getchar())!=' ')
{
p[i]=ch;
i++;
}
p[i]='';
int length=strlen(p);
for(i=0;i<=length-1;i++)
num[i]=p[i]-'0';
if(length<=4)
transport(num,length-1);
else if(length<=8)
{
transport(num,length-5);
printf("W");
transport(num+length-4,3);
}
else
{
printf("%cY",item[num[0]]);
transport(num+1,3);
if(num[1]!=0||num[2]!=0||num[3]!=0||num[4]!=0)
printf("W");
if(num[length-4]
0)
printf("%c",item[0]);
transport(num+5,3);
}
return 0;
}

这道题看不太懂,这是从网上借鉴的代码模仿写的。

原文地址:https://www.cnblogs.com/wangdian1/p/10410250.html