本帖最后由 helloJohn 于 2016-5-12 19:23 编辑
金额转换,阿拉伯数字转换成中国传统形式。例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
char a[15]={0} ;
printf("请您输入金额") ;
fgets(a, 15, stdin) ;
test21(a) ;
void test21(char *a){
typedef struct{
int _key ;
char *_value[3] ;
} ss ;
unsigned long x=strlen(a)-1 ;
ss qian[15] ={0,{"0","0"}};
for (int n=0; n<x; n++) {
qian[n]._key=a[n]-'0' ;
}
for (int n=0; n<x; n++) {
switch (qian[n]._key) {
case 0:
if((x-n)%4!=1&&qian[n+1]._key!=0){qian[n]._value[0]="零" ;}
else if ((x-n)%4==1&&qian[n+1]._key!=0){ qian[n]._value[2]="零" ;}
break;
case 1:qian[n]._value[0]="壹" ;break;
case 2:qian[n]._value[0]="贰" ;break;
case 3:qian[n]._value[0]="叁" ;break;
case 4:qian[n]._value[0]="肆" ;break;
case 5:qian[n]._value[0]="伍" ; break;
case 6:qian[n]._value[0]="陆" ;break;
case 7:qian[n]._value[0]="柒" ;break;
case 8:qian[n]._value[0]="捌" ;break;
case 9:qian[n]._value[0]="玖" ;break;
default: break;
}
switch ((x-n)%4) {
case 1:
if((x-n)/4==1){
qian[n]._value[1]="万" ;
}
if((x-n)/4==2){
qian[n]._value[1]="亿" ;
}
break;
case 2:if(qian[n]._key!=0)qian[n]._value[1]= "拾" ; break;
case 3:if(qian[n]._key!=0)qian[n]._value[1]="佰" ; break;
case 0:if(qian[n]._key!=0)qian[n]._value[1]= "仟" ; break;
default:
break;
}
}
for (int n=0; n<x; n++) {
if(qian[n]._value[0]==NULL){
}else{printf("%s",qian[n]._value[0]) ;}
if(qian[n]._value[1]==NULL){
}else{printf("%s",qian[n]._value[1]) ;}
if(qian[n]._value[2]==NULL){
}else{printf("%s",qian[n]._value[2]) ;}
}
printf("圆整") ;
} |
|