题解 | #人民币转换#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
#include <stdio.h>
#include <string.h>
char hanzi[19][4]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾","佰","仟","万","亿","元","角","分","整"};
char wei[19][4]={"整","分","角","元","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"};
char zhengwei[4],zhengweiflag=0,lingflag=0;
int main(){
char str[100];
int i,len,ling;
while(scanf("%s",str)!=-1){
len=strlen(str);
ling=0;
zhengweiflag=0;lingflag=0;
printf("人民币");
if(str[0]=='0'){
for(i=0;i<len;i++)
if((str[i]!='0')&&(str[i]!='.')) printf("%s%s",hanzi[str[i]-'0'],wei[len-i]);
}
else
{ for(i=0;i<len;i++){
if((str[i]=='0')&&((len-i)!=2)) lingflag=1;
if(str[i]!='0'){
if(zhengweiflag) {printf("%s",zhengwei);zhengweiflag=0;}
if(lingflag) {printf("%s",hanzi[0]);lingflag=0;}
if((str[i]=='1')&&(((len-i)==5)||((len-i)==9)||((len-i)==13))) printf("拾");
else {
if(((len-i)==4)||((len-i)==8)||((len-i)==12)||((len-i)==3)) printf("%s",hanzi[str[i]-'0']);
else
printf("%s%s",hanzi[str[i]-'0'],wei[len-i]);
}
}
if(((len-i)==4)||((len-i)==8)||((len-i)==12)){
lingflag=0;
if(zhengweiflag==0){
zhengweiflag=1;
memcpy(zhengwei,wei[len-i],4);
}
}
}}
if((str[len-1]=='0')&&(str[len-2]=='0')) printf("整");
printf("\n");
}
return 0;
}