資源描述:
《C語言 順序棧實(shí)現(xiàn)十進(jìn)制轉(zhuǎn)換為二進(jìn)制,八進(jìn)制,十六進(jìn)制》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、運(yùn)行結(jié)果:代碼:#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)?true:false;}boolIsEmpty(SeqSta
2、ck*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!");returnNULL;}e=s->data[s->top];s->top--;returne;}intReadTo
3、p(SeqStack*s){returns->data[s->top];}voidPrint(SeqStack*s){inttemp=s->top;if(IsEmpty(s)){printf("Thestackisempty!");return;}printf("轉(zhuǎn)換后的結(jié)果:");while(temp>=0){if(s->data[temp]<10)printf("%d",s->data[temp]);else{if(s->data[temp]=10)printf("a");elseif(s->data[temp]=11)printf("b");elseif(s->
4、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();printf("請輸入要轉(zhuǎn)換的十進(jìn)制數(shù):");scanf("%d",&m);printf("");printf("請輸入轉(zhuǎn)換進(jìn)制:");printf("******************************"
5、);printf("*請選擇一個(gè)你要轉(zhuǎn)換的進(jìn)制*");printf("*1.二進(jìn)制*");printf("*2.八進(jìn)制*");printf("*3.十六進(jìn)制*");printf("******************************");scanf("%d",&d);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);}