题目标题:金额转换(谌海军) 题目描述:将一个小写的金额数字转换为大写输出 提示: 1、注意数字的取值范围; 2、注意连续的多个零,如:60021应表示为六万零二十一,而不是六万零千零百二十一; 3、金额的各位大写采用简体中文:十百千万; 4、数字的大写采用简体中文:零一二三四五六七八九; 输入描述:输入为一个5位数的正整数; 输出描述:输出为大写的数字; 样式输入:30001 样式输出:三万零一 |
#include <stdio.h>
#include <string.h>
void strlcat(char *dest, char *source)
{
char temp[10000];
strcpy(temp, dest);
strcpy(dest, source);
strcat(dest, temp);
}
int main()
{
char input[100];
char *je[]= {" 元","十","百","千","万","十","百","千","亿","十","百","千","万","十","百","千","万","亿"};
char *sz[]= {"零","一","二","三","四","五","六","七","八","九"};
scanf("%s",input);
char result[1000] = {0};
char *p = strstr(input, "."), *q = p;
int pos = 0, k, b = -1, last = 0;
if (!q)
{
q = input;
while(*q) q++;
}
q--;
while(q!=input)
{
k = *q - '0';
if (0 == k)
{
if (!b)
{
strlcat(result, sz[0]);
}
if (b!=-1)
b = 1;
}
else
{
if (pos>4 && last <4)
strlcat(result, je[4]);
if (pos>8 && last <8)
strlcat(result, je[8]);
if (pos != 0) strlcat(result, je[pos]);
strlcat(result, sz[k]);
b = 0;
last = pos;
}
pos++;
q--;
}
k = *q - '0';
if (k != 0)
{
if (pos < 8 && pos>4 && last <4)
strlcat(result, je[4]);
if (pos>8 && last <8)
strlcat(result, je[8]);
if (pos != 0) strlcat(result, je[pos]);
if (k != 1 || pos != 1) strlcat(result, sz[k]);
}
if (p != NULL)
{
pos = 0;
p++;
while(*p)
{
k = *p - '0';
if (k)
{
strcat(result, sz[k]);
}
p++;
pos++;
}
}
printf("%s", result);
return 0;
}