币值转换

7-1 币值转换 (20 分)

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

输入格式:

输入在一行中给出一个不超过9位的非负整数。

输出格式:

在一行中输出转换后的结果。注意“零”的用法必须符合中文习惯。

输入样例1:

813227345

输出样例1:

iYbQdBcScWhQdBeSf

输入样例2:

6900

输出样例2:

gQjB

实验思路:

定义一个数组和字符数组,前者用来储存数字,后者用来储存结果。
自定义2个函数,分别处理单位和数字。
然后提取出需要的数据,用多段if-else进行判断并输入字符数组。
最后把字符数组的字符一个个打印出来。

实验代码:

#include<stdio.h>
char shu(int x);
char wei(int count);
int main()
{	
int count,number,n,i,j,k,x,y,d;
int a[9];  //储存数字的数组 
char b[16];  //储存字符的数组 
i=0;
scanf("%d",&number);
if(number==0){  //判断是否为0,是的话直接输出 
	printf("a");
}
else
do{	n=number%10;
	number=number/10; 
	a[i]=n;
	i++;
}while(number!=0&&i<=9);  //把各位数储存进数组,并统计位数 
k=i;
i=0;
d=0;
count=1;
for(j=0;j<k;j++){  //把数组里的数判定并输入字符数组,并把位数输入 
	x=a[i];  //把数组中数字赋值给x,进行判定 
    if(count==1&&x!=0){  //位数为1的时候只输出数字 
	b[d++]=shu(x);
}
	if(count!=1&&count!=5&&count!=9&&x!=0){  //非特殊情况的正常输出 
	b[d++]=wei(count);
	b[d++]=shu(x);
}
    if (x==0&&(count%4)>1){  //如果前一位不为0,且不是1位和5位和9位这种特殊位置,则直接输出0 
    if (y!=0) {
    b[d++]=shu(x);    
    }
}
    if(count==5){  //当位数达到5位,进行判定,如果只为0,只输出位数 
    if(x==0){
	b[d++]=wei(count);
	}
    else
    {
	b[d++]=wei(count);
	b[d++]=shu(x);
	}
	}
	if(count==9){  //当位数达到9位可以直接输出这个位数和单位 
	if(x!=0);
	b[d++]=wei(count);
	b[d++]=shu(x);
}
	y=x;  //记录这次的数字,用于处理多个0 
	i++;
	count++;  //位数增加 
}
d=d-1;
for(d;d>=0;d--){  //逐渐输出字符数组里的字符
	printf("%c",b[d]);   
}
return 0;
}

char shu(int x)  //数字转换成字母 
{
	if(x==0)
	return 'a';
	if(x==1)
	return 'b';
	if(x==2)
	return 'c';
	if(x==3)
	return 'd';
	if(x==4)
	return 'e';
	if(x==5)
	return 'f';
	if(x==6)
	return 'g';
	if(x==7)
	return 'h';
	if(x==8)
	return 'i';
	if(x==9)
	return 'j';
}

char wei(int count)  //输出位数
{
	if(count==9)
	return 'Y';
	if(count==8)
	return 'Q';
	if(count==7)
	return 'B';
	if(count==6)
	return 'S';
	if(count==5)
	return 'W';
	if(count==4)
	return 'Q';
	if(count==3)
	return 'B';
	if(count==2)
	return 'S';
	else
	return 0;
}

实验截图:

原文地址:https://www.cnblogs.com/zhuwanxing/p/10388976.html