使用系統(tǒng)環(huán)境:
1:win10,使用工具dev
2:使用系統(tǒng)win10
3:參考書籍?dāng)?shù)據(jù)結(jié)構(gòu)(C語言版——嚴(yán)蔚敏 吳偉民)
( 注意:此文章默認(rèn),學(xué)習(xí)者擁有一定的數(shù)據(jù)機(jī)構(gòu)棧,C語言的知識,書籍第20頁,2.1算法的代碼進(jìn)行一個簡化。)文章來源地址http://www.zghlxwxcb.cn/news/detail-743991.html
//順序棧的定義,基本操作
#include<stdio.h>
#define MaxSize 100
#define Error 0
#define ok 1
typedef struct sqSatck{
int data[MaxSize]; //棧頂
int top; //棧低
}SqStack;
void initStack(SqStack &s){
s.top=0;
}
int push(SqStack &s,int &e){
if(MaxSize == s.top)return Error;
s.data[s.top]=e;
s.top++;
return ok;
}
int pop(SqStack &s,int &e){
if(s.top==0) return Error;
s.top--;
e=s.data[s.top];
return ok;
}
int isEmpty(SqStack s){
if(s.top==0)return 1;
else return 0;
}
//使用棧,實現(xiàn)十進(jìn)制到二進(jìn)制的轉(zhuǎn)換
int main(){
int N;
SqStack s;
initStack(s);
printf("inpurt N=");
scanf("%d",&N);
while(N){
push(s,N%2);
N/=2;
}
int e;
while(!isEmpty(s)){
pop(s,e);
printf("%d",e);
}
return 1;
}
//實現(xiàn)十進(jìn)制到十六進(jìn)制的轉(zhuǎn)換
int main(){
int N;
SqStack s;
initStack(s);
printf("inpurt N=");
scanf("%d",&N);
while(N){
push(s,N%16);
N/=16;
}
int e;
while(!isEmpty(s)){
pop(s,e);
if(e>=10)e='A'+e-10; //十六進(jìn)制的基本元素是0~9,A~F,此處將余數(shù)10~15轉(zhuǎn)換成字符A~F
else e='O'+e; //這里是將數(shù)字0~9,轉(zhuǎn)換為字符0~9;
printf("%d",e);
}
return 1;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-743991.html
到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu):使用順序棧的基本操作,實現(xiàn)十進(jìn)制轉(zhuǎn)為二進(jìn)制,十六進(jìn)制的轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!