欢迎来到天天文库
浏览记录
ID:55666373
大小:29.50 KB
页数:3页
时间:2020-05-23
《C语言 顺序栈实现十进制转换为二进制,八进制,十六进制.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、运行结果:代码:#include#include#defineMAX20typedefstruct{intdata[MAX];inttop;}SeqStack;SeqStack*Init(){SeqStack*s;s=(SeqStack*)malloc(sizeof(SeqStack));s->top=-1;returns;}voidDestroy(SeqStack*s){free(s);}boolIsFull(SeqStack*s){return(s->top==MAX-1)?t
2、rue:false;}boolIsEmpty(SeqStack*s){return(s->top==-1)?true:false;}voidPush(SeqStack*s,inta){if(IsFull(s)){printf("Thestackisfull,failedtopush!");return;}s->top++;s->data[s->top]=a;}intPop(SeqStack*s){inte;if(IsEmpty(s)){printf("Thestackisempty,failedtopop!")
3、;returnNULL;}e=s->data[s->top];s->top--;returne;}intReadTop(SeqStack*s){returns->data[s->top];}voidPrint(SeqStack*s){inttemp=s->top;if(IsEmpty(s)){printf("Thestackisempty!");return;}printf("转换后的结果:");while(temp>=0){if(s->data[temp]<10)printf("%d",s->data[tem
4、p]);else{if(s->data[temp]=10)printf("a");elseif(s->data[temp]=11)printf("b");elseif(s->data[temp]=12)printf("c");elseif(s->data[temp]=13)printf("d");elseif(s->data[temp]=14)printf("e");elseprintf("f");}temp--;}printf("");}intmain(){intm,c,d,n;SeqStack*s;s=Init
5、();printf("请输入要转换的十进制数:");scanf("%d",&m);printf("");printf("请输入转换进制:");printf("******************************");printf("*请选择一个你要转换的进制*");printf("*1.二进制*");printf("*2.八进制*");printf("*3.十六进制*");printf("******************************");scanf("%d",&d
6、);printf("");if(d==1)n=2;elseif(d==2)n=8;elseif(d==3)n=16;elseprintf("输入有误!");while(m){c=m%n;m=m/n;Push(s,c);}Print(s);Destroy(s);}
此文档下载收益归作者所有