c语⾔编程阿拉伯数字转中⽂,阿拉伯数字到中⽂⼤写数字的转
换
1 #include
2 #include
3
4 struct output //存放中⽂⼤写单位或数字的结构体类型
5 {
6 char ch[3];
7 struct output *next;
8 };
9 struct output *head1, *psnew1, *p1;10 typedef structoutput output1;11
12 void Num(output1 *p1, char *N, int wei); //函数注释见main后的函数定义部分
13 void Re(output1 *p1, char *R, intre);14 void Di(output1 *p1, char *D, intdi);15
16 voidmain ()17 {18 intt;19 intsign, flag, mark;20 intre, di;21
22 char N[10][3]={"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; //⽤字符串数组存放中⽂⼤写数字和单位
23 char R[3][3]={"拾", "佰", "仟"};24 char D[3][3]={"万", "亿", "兆"};25
26 struct number //存放每⼀位数字的结构体类型
27 {28 intwei;29 struct number *next;30 };31 struct number *head, *psnew, *p;32 typedef structnumber 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
output的反义词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 inti;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 intj;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 intk, m;307 m=(di-5)/4;308
309 for (k=0; k<=2; k++)310 p1->ch[k]=D[m*3+k];311 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论