7-1 币值转换 (20 分)
输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。
我的代码:
include <stdio.h>
include <string.h>
void printNumber(char *s, int i, int len);
void printPlace(char *s, int, int);
int main()
{
char s[11];
int i, strLen;
gets(s);
strLen = (int)strlen(s);
if (strLen == 1 && s[0] == '0') {
printf("a
");
return 0;
}
for (i=0; i<strLen; i++) {
if (s[i] == '0') {
if (strLen-i == 5) {
if (s[1]!='0' || s[2]!='0' || s[3]!='0' || s[4]!='0' || strLen < 9) {
printf("W");
}
}
if (s[i+1] != '0' && s[i+1] != ' ' && (strLen-i != 5)) {
printf("a");
}
continue;
}else {
printNumber(s, i, strLen);
if (strLen-i == 5) {
if (s[1]!='0' || s[2]!='0' || s[3]!='0' || s[4]!='0' || strLen < 9) {
printf("W");
}
}
}
if (strLen-i == 9) {
printf("Y");
}
}
return 0;
}
void printNumber(char *s, int i, int len)
{
int num = s[i] - '0';
switch (num) {
case 0:
printf("a");
if (s[i] != '0') {
printPlace(s, len-i, len);
}
break;
case 1: printf("b"); printPlace(s, len-i, len); break;
case 2: printf("c"); printPlace(s, len-i, len); break;
case 3: printf("d"); printPlace(s, len-i, len); break;
case 4: printf("e"); printPlace(s, len-i, len); break;
case 5: printf("f"); printPlace(s, len-i, len); break;
case 6: printf("g"); printPlace(s, len-i, len); break;
case 7: printf("h"); printPlace(s, len-i, len); break;
case 8: printf("i"); printPlace(s, len-i, len); break;
case 9: printf("j"); printPlace(s, len-i, len); break;
}
}
void printPlace(char *s, int i, int len)
{
switch (i) {
case 1: break;
case 2: printf("S"); break;
case 3: printf("B"); break;
case 4: printf("Q"); break;
case 5: break;
case 6: printf("S"); break;
case 7: printf("B"); break;
case 8: printf("Q"); break;
}
}
我的思路:从最大数位开始逐个取出各数字 ,然后逐个输出,在记录下来,然后与同学讨论,请教问题,当有0时要单独判断,若前一位不为零,则输出零,不为0的时候可以直接规律输出,当数位大于万位时是特殊情况,需要调整大写字母的输出反之则直接按规律输出。