將阿拉伯數字轉化為中文大寫是很簡單很實用的功能,但由於0這個特殊數字的存在使得實現起來並非那麼容易,實現這一功能的關鍵就是對0的正確處理。該程式是我幾個月之前寫成的,當時沒有加註釋,現在程式的實現細節基本忘光了,難以寫出註釋。只能憑自己模糊的印象大致部分地介紹一下思路和方法,當初思路中的細節已經無法 ...
將阿拉伯數字轉化為中文大寫是很簡單很實用的功能,但由於0這個特殊數字的存在使得實現起來並非那麼容易,實現這一功能的關鍵就是對0的正確處理。該程式是我幾個月之前寫成的,當時沒有加註釋,現在程式的實現細節基本忘光了,難以寫出註釋。只能憑自己模糊的印象大致部分地介紹一下思路和方法,當初思路中的細節已經無法回憶了,沒有註釋的代碼大家只能將就看一下,能看懂最好看不懂也沒辦法
我當初的想法是現將輸入的阿拉伯數字的每一位單獨分離出來,按從低位到高位的順序存放線上性鏈表裡,然後從低位到高位掃描鏈表。將數字從低位至高位每四位分為一組,最左邊的組可以不足位。用Re代表每組中某數字相對於該組最低位的偏移量,di代表每組中最低起始位從數字最低位數起的的位數,從低位至高位從1起以4為間隔依次增大。用mark表示前一位是否為0,用sign表示某數右邊低位部分是否全為0,flag表示每組中某數右邊在該組中的低位部分是否全為0。程式中用字元型二維數組存放中文大寫數字單位,並用三個函數分別完成單位阿拉伯數字到中文大寫數字,每組內的數字單位到中文大寫,以及每組的最低起始位的數字單位到中文大寫的轉化
程式的主體部分就是用迴圈結構從左到右掃描存放數字各位的鏈表,掃描過程中把轉化出的中文大寫數字按由高位至低位的順序插入另一個鏈表,掃描完畢後新鏈表中存放的就是轉換結果,可以直接輸出。
C語言代碼如下:
1 #include <stdio.h> 2 #include <malloc.h> 3 4 struct output //存放中文大寫單位或數字的結構體類型 5 { 6 char ch[3]; 7 struct output *next; 8 }; 9 struct output *head1, *psnew1, *p1; 10 typedef struct output output1; 11 12 void Num(output1 *p1, char *N, int wei); //函數註釋見main後的函數定義部分 13 void Re(output1 *p1, char *R, int re); 14 void Di(output1 *p1, char *D, int di); 15 16 void main () 17 { 18 int t; 19 int sign, flag, mark; 20 int re, di; 21 22 char N[10][3]={"零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"}; //用字元串數組存放中文大寫數字和單位 23 char R[3][3]={"拾", "佰", "仟"}; 24 char D[3][3]={"萬", "億", "兆"}; 25 26 struct number //存放每一位數字的結構體類型 27 { 28 int wei; 29 struct number *next; 30 }; 31 struct number *head, *psnew, *p; 32 typedef struct number number1; 33 34 printf ("please input the number which you want to convert\n"); 35 scanf ("%d", &t); //輸入要轉換的阿拉伯數字 36 37 head=(number1 *)malloc (sizeof(number1)); 38 psnew=head; 39 head->next=NULL; 40 41 head1=(output1 *)malloc (sizeof(output1)); 42 psnew1=head1; 43 head1->next=NULL; 44 45 while (t!=0) //將輸入的阿拉伯數字的各位分離,按從低位至高位的順序從左至右存放線上性鏈表中 46 { 47 p=(number1 *)malloc (sizeof(number1)); 48 p->wei=t%10; 49 p->next=NULL; 50 psnew->next=p; 51 psnew=p; 52 t=t/10; 53 } 54 55 psnew=head->next; 56 sign=0; 57 flag=0; 58 59 if (psnew->wei) 60 mark=1; //重要變數的必要初始化 61 else 62 mark=0; 63 64 re=0; 65 di=1; 66 67 while (psnew!=NULL) //從左到右掃描鏈表,進行到中文大寫的轉化,轉換結果存放在ouput類型的鏈表中 68 { 69 if (re==0) 70 { 71 if (psnew->wei==0) 72 { 73 if (sign==1) 74 { 75 if (flag==1) 76 { 77 p1=(output1 *)malloc (sizeof(output1)); 78 Num(p1, &N[0][0], 0); 79 p1->next=head1->next; 80 head1->next=p1; 81 } 82 } 83 84 flag=0; 85 if (mark==1) 86 mark=0; 87 re=re+1; 88 } 89 else 90 { 91 if (sign==0) 92 { 93 if (di==1) 94 { 95 p1=(output1 *)malloc (sizeof(output1)); 96 Num(p1, &N[0][0], psnew->wei); 97 p1->next=NULL; 98 head1->next=p1; 99 } 100 else 101 { 102 p1=(output1 *)malloc (sizeof(output1)); 103 Di(p1, &D[0][0], di); 104 p1->next=NULL; 105 head1->next=p1; 106 107 p1=(output1 *)malloc (sizeof(output1)); 108 Num(p1, &N[0][0], psnew->wei); 109 p1->next=head1->next; 110 head1->next=p1; 111 } 112 } 113 else 114 { 115 if (flag==0) 116 { 117 p1=(output1 *)malloc (sizeof(output1)); 118 Di(p1, &D[0][0], di); 119 p1->next=head1->next; 120 head1->next=p1; 121 122 p1=(output1 *)malloc (sizeof(output1)); 123 Num(p1, &N[0][0], psnew->wei); 124 p1->next=head1->next; 125 head1->next=p1; 126 } 127 else 128 { 129 if (mark==0) 130 { 131 p1=(output1 *)malloc (sizeof(output1)); 132 Num(p1, &N[0][0], 0); 133 p1->next=head1->next; 134 head1->next=p1; 135 136 p1=(output1 *)malloc (sizeof(output1)); 137 Di(p1, &D[0][0], di); 138 p1->next=head1->next; 139 head1->next=p1; 140 141 p1=(output1 *)malloc (sizeof(output1)); 142 Num(p1, &N[0][0], psnew->wei); 143 p1->next=head1->next; 144 head1->next=p1; 145 } 146 else 147 { 148 p1=(output1 *)malloc (sizeof(output1)); 149 Di(p1, &D[0][0], di); 150 p1->next=head1->next; 151 head1->next=p1; 152 153 p1=(output1 *)malloc (sizeof(output1)); 154 Num(p1, &N[0][0], psnew->wei); 155 p1->next=head1->next; 156 head1->next=p1; 157 } 158 } 159 } 160 sign=1; 161 flag=1; 162 163 if (mark==0) 164 mark=1; 165 166 re=re+1; 167 } 168 } 169 else 170 { 171 if (psnew->wei==0) 172 { 173 if (mark==1) 174 mark=0; 175 176 re=(re+1)%4; 177 if (re==0) 178 di=di+4; 179 } 180 else 181 { 182 if (sign==0) 183 { 184 if (di==1) 185 { 186 p1=(output1 *)malloc (sizeof(output1)); 187 Re(p1, &R[0][0], re); 188 p1->next=NULL; 189 head1->next=p1; 190 191 p1=(output1 *)malloc (sizeof(output1)); 192 Num(p1, &N[0][0], psnew->wei); 193 p1->next=head1->next; 194 head1->next=p1; 195 } 196 else 197 { 198 p1=(output1 *)malloc (sizeof(output1)); 199 Di(p1, &D[0][0], di); 200 p1->next=NULL; 201 head1->next=p1; 202 203 p1=(output1 *)malloc (sizeof(output1)); 204 Re(p1, &R[0][0], re); 205 p1->next=head1->next; 206 head1->next=p1; 207 208 p1=(output1 *)malloc (sizeof(output1)); 209 Num(p1, &N[0][0], psnew->wei); 210 p1->next=head1->next; 211 head1->next=p1; 212 } 213 } 214 else 215 { 216 if (flag==0) 217 { 218 p1=(output1 *)malloc (sizeof(output1)); 219 Di(p1, &D[0][0], di); 220 p1->next=head1->next; 221 head1->next=p1; 222 223 p1=(output1 *)malloc (sizeof(output1)); 224 Re(p1, &R[0][0], re); 225 p1->next=head1->next; 226 head1->next=p1; 227 228 p1=(output1 *)malloc (sizeof(output1)); 229 Num(p1, &N[0][0], psnew->wei); 230 p1->next=head1->next; 231 head1->next=p1; 232 } 233 else 234 { 235 if (mark==0) 236 { 237 p1=(output1 *)malloc (sizeof(output1)); 238 Num(p1, &N[0][0], 0); 239 p1->next=head1->next; 240 head1->next=p1; 241 242 p1=(output1 *)malloc (sizeof(output1)); 243 Re(p1, &R[0][0], re); 244 p1->next=head1->next; 245 head1->next=p1; 246 247 p1=(output1 *)malloc (sizeof(output1)); 248 Num(p1, &N[0][0], psnew->wei); 249 p1->next=head1->next; 250 head1->next=p1; 251 } 252 else 253 { 254 p1=(output1 *)malloc (sizeof(output1)); 255 Re(p1, &R[0][0], re); 256 p1->next=head1->next; 257 head1->next=p1; 258 259 p1=(output1 *)malloc (sizeof(output1)); 260 Num(p1, &N[0][0], psnew->wei); 261 p1->next=head1->next; 262 head1->next=p1; 263 } 264 } 265 } 266 sign=1; 267 flag=1; 268 269 if (mark==0) 270 mark=1; 271 272 re=(re+1)%4; 273 if (re==0) 274 di=di+4; 275 } 276 } 277 278 psnew=psnew->next; 279 } 280 281 psnew1=head1->next; 282 while (psnew1!=NULL) //輸出轉換結果 283 { 284 printf("%s", &psnew1->ch[0]); 285 psnew1=psnew1->next; 286 } 287 printf("\n"); 288 } 289 290 void Num(output1 *p1, char *N, int wei) //將wei表示的阿拉伯數字轉化為中文大寫,存放在p1指向的ouput1類型的節點中 291 { 292 int i; 293 for (i=0; i<=2; i++) 294 p1->ch[i]=N[wei*3+i]; 295 } 296 297 void Re(output1 *p1, char *R, int re) //將re表示的組內數字單位轉換為中文大寫,存放在p1指向的ouput1類型的節點中 298 { 299 int j; 300 for (j=0; j<=2; j++) 301 p1->ch[j]=R[(re-1)*3+j]; 302 } 303 304 void Di(output1 *p1, char *D, int di) //將di表示的每組的最低起始位的數字單位轉換為中文大寫 305 { 306 int k, m; 307 m=(di-5)/4; 308 309 for (k=0; k<=2; k++) 310 p1->ch[k]=D[m*3+k]; 311 }
運行結果: