人民币大小写
代码写得有些乱 不准备修改了。
注释仅供参考,可能表述错误。
1 /* 2 **程序通过控制MAX,X的大小&c[]的内容实现扩展程序的整数位转换上限 c[]要和MAX的对应,否则出错 3 **小数3位有效,四舍五入。由于程序限制不能更改长度,转换两位 4 **a,b,d数组不可变 5 */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <stdlib.h> 10 #define MAX 13 /* 四舍五入后的上限{13个9}.994 10的13次幂溢出 ★★可变 当前最大32*/ 11 #define X 1024 /* 存储数字的数组长度 1024个数字字符 ★★可变*/ 12 char *a[10] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; 13 char *b[5] = { "", "", "拾", "佰", "仟" }; 14 char *c[8] = { "", "万", "亿", "兆", "京", "垓", "秭", "穰" }; /* 穰=10的28次幂 ★★可变 */ 15 char *d[4] = { "整", "元", "角", "分" }; 16 17 int main() 18 { 19 int i = 0; 20 int j = 0; 21 int zero = 0; 22 int ge = 0; 23 int shu = 0; 24 int yu = 0; 25 int pos = 0; 26 int shang = 0; 27 int dot_ge = 0; 28 int zheng_ge= 0; 29 int xiao_ge = 0; 30 char num[X] = "0"; /* 预留一位用来进位 */ 31 char result[X * 5]= "人民币"; 32 33 printf( "请输入" ); 34 scanf( "%s", num + 1 );/* 预留一位用来进位 */ 35 36 /**过滤输入**/ 37 for (; i < X && num[i] != 0; i++ ) { 38 39 if ( num[i] >= '0' && num[i] <= '9' ) 40 if ( dot_ge == 0 ) 41 zheng_ge++; 42 else 43 xiao_ge++; 44 else if ( num[i] == '.' ) { 45 dot_ge++; 46 47 } else { 48 puts( "非法输入" ); 49 return(main() ); 50 } 51 if ( dot_ge >= 2 || xiao_ge >3) { 52 puts( "非法输入" ); 53 return(main() ); 54 } 55 } 56 57 /**格式化&取小数点2位**/ 58 i = -1; 59 if ( num[zheng_ge + 3] >= '5' && xiao_ge > 2 ) { 60 num[zheng_ge + 2]++; 61 if ( num[zheng_ge + 2] > '9' ) { 62 num[zheng_ge + 1]++; 63 num[zheng_ge + 2] = '0'; 64 } 65 if ( num[zheng_ge + 1] > '9' ) { 66 num[zheng_ge + 1] = '0'; 67 i = zheng_ge - 1; 68 } 69 70 for (; i >= 0; i-- ) { 71 num[i]++; 72 if ( num[i] > '9' ) { 73 num[i] = '0'; 74 } else { 75 break; 76 } 77 } 78 } 79 num[zheng_ge + 3] = 0; /* 小数点截取两位 */ 80 81 /**溢出判断**/ 82 i = 0; 83 while ( num[i++] == '0' ) 84 ; 85 /* printf("%s %d %d\n",num, zheng_ge,i); */ 86 if ( zheng_ge - i >= MAX ) { 87 puts( "数值溢出" ); 88 return(main() ); 89 } 90 91 /**转换整数部分**/ 92 i = 0; 93 j = 0; 94 zero= 0; 95 ge = 0; 96 shu = 0; 97 98 99 while ( num[i++] == '0' && i<zheng_ge) 100 ; /* 去首部0 */ 101 if(i==zheng_ge) 102 strcat( result, a[0] ); 103 //printf("1 %s i %d j%d ge%d shang%d pos%d %d\n",num,i,j,ge,shang,pos,zheng_ge-pos); 104 pos = --i; /* 对数组*/ 105 j = 0; /* 重新*/ 106 i = 0; /* 定位 */ 107 yu = (zheng_ge - pos) % 4; /*不足四位的那一组*/ 108 shang = (zheng_ge - pos) / 4; /*几组4*/ 109 //printf("1 %s i %d j%d ge%d shang%d pos%d %d\n",num,i,j,ge,shang,pos,zheng_ge-pos); 110 for (; j <= shang; j++ ) {/*周期*/ 111 ge = j * 4 + yu; 112 // printf("i %d j%d ge%d shang%d pos%d\n",i,j,ge,shang,pos); 113 zero = 0; 114 for (; i < ge; i++ ) {/*四位一循环周期*/ 115 shu = (num + pos)[i] - '0'; 116 if ( shu == 0 ) { 117 if ( (num + pos)[i + 1] == '0' || i == ge - 1 ) { 118 zero++; 119 continue; 120 } else 121 strcat( result, a[0] ); 122 } else { 123 strcat( result, a[shu] ); 124 strcat( result, b[ge - i] ); 125 } 126 } 127 if ( zero != 4 && ge > 0 ) 128 strcat( result, c[shang - j] ); 129 } 130 strcat( result, d[1] ); 131 132 /**转换小数部分**/ 133 if ( atoi( num + zheng_ge + 1 ) == 0 ) 134 strcat( result, d[0] ); 135 else { 136 if ( num[zheng_ge + 1] != 0 ) { 137 strcat( result, a[num[zheng_ge + 1] - '0'] ); 138 strcat( result, d[2] ); 139 } else 140 strcat( result, a[0] ); 141 142 143 if ( num[zheng_ge + 2] != 0 ) { 144 strcat( result, a[num[zheng_ge + 2] - '0'] ); 145 strcat( result, d[3] ); 146 } 147 } 148 149 /**输出结果**/ 150 i=0; 151 while ( num[i++] == '0' && i<zheng_ge) 152 ; 153 printf( "¥%s ", num + i-1 ); 154 puts( result ); 155 printf("\n\n\n"); 156 main(); 157 return 0; 158 }
本文来自博客园,作者:一支小白,转载请注明原文链接:https://www.cnblogs.com/startnow/p/6621952.html