c语言编程阿拉伯数字转中文,阿拉伯数字到中文大写数字的转换

这篇博客介绍了一个使用C语言编程的方法,将输入的阿拉伯数字转换成对应的中文大写数字。通过创建特定的数据结构和一系列的转换函数,实现了从低位到高位的数字转换,并将结果存储在线性链表中。最后,程序遍历链表并输出转换后的中文大写数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值