Python和C語(yǔ)言利用棧分別實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
問(wèn)題描述
利用棧的數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)將十進(jìn)制數(shù)轉(zhuǎn)換成二進(jìn)制數(shù)
C語(yǔ)言實(shí)現(xiàn)
順序表的存儲(chǔ)結(jié)構(gòu)實(shí)現(xiàn)棧
代碼:
#include <stdlib.h>
#include <stdio.h>
#define STACK_INIT_SIZE 100 //棧初始開(kāi)辟空間大小
#define STACK_INCREMENT 10 //棧追加空間大小
//棧的結(jié)構(gòu)體
typedef struct stack{
int *base;
int *top;
int size;
}binStack;
//棧初始化
binStack stack_init()
{
binStack bs;
bs.base = (int *)malloc(sizeof(int)*STACK_INIT_SIZE);
bs.top = bs.base;
bs.size = STACK_INIT_SIZE;
return bs;
}
//入棧
void push(binStack *bs, int e)
{
if(bs->top - bs->base >= bs->size)
{
bs->size += STACK_INCREMENT;
bs->base = realloc(bs->base, bs->size);
}
*(bs->top++) = e;
}
//出棧
int pop(binStack *bs)
{
if(bs->top != bs->base)
{
bs->top--;
return *bs->top;
}
return -1;
}
//主函數(shù)
void main()
{
int dec;
binStack bs = stack_init();
printf("請(qǐng)輸入十進(jìn)制整數(shù):\n");
scanf("%d", &dec);
while(dec)
{
push(&bs, dec%2);
dec /= 2;
}
printf("轉(zhuǎn)換后的二進(jìn)制數(shù)是:\n");
while(bs.top != bs.base)
{
printf("%d", pop(&bs));
}
printf("\n\n");
system("date /T");
system("TIME /T");
system("pause");
exit(0);
}
運(yùn)行結(jié)果:

Python實(shí)現(xiàn)
對(duì)于stack我們可以使用python內(nèi)置的list實(shí)現(xiàn)(也可以用鏈表實(shí)現(xiàn)),因?yàn)閘ist是屬于線性數(shù)組,在末尾插入和刪除一個(gè)元素所使用的時(shí)間都是O(1),這非常符合stack的要求。
代碼:
import datetime //顯示時(shí)間引入的庫(kù)
import time //
from pip._vendor.distlib.compat import raw_input //使命令窗口不立即關(guān)閉引入的庫(kù)
//棧類
class BinStack:
def __init__(self):
self.bs = []
//入棧
def push(self, e):
self.bs.append(e)
//出棧
def pop(self):
if self.bs:
return self.bs.pop()
else:
raise LookupError("stack is empty!")
//檢查棧是否為空,是返回False,不是返回True
def isEmpty(self):
return bool(self.bs)
if __name__ == '__main__':
binStack = BinStack()
dec = int(input("請(qǐng)輸入十進(jìn)制整數(shù):\n"))
print("轉(zhuǎn)換后的二進(jìn)制數(shù)是:")
while dec != 0:
binStack.push(dec%2)
dec //= 2
while binStack.isEmpty() == True:
print("{}".format(binStack.pop()), end="")
else:
print("\n")
//打印時(shí)間
datetime = datetime.datetime.now()
print(datetime.strftime("%Y-%m-%d\n%H:%M:%S"))
//使命令窗口不立即關(guān)閉
input("Press Enter to exit…")
運(yùn)行結(jié)果:

以上就是Python和C語(yǔ)言利用棧分別實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的詳細(xì)內(nèi)容,更多關(guān)于Python進(jìn)制轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)字/字符串補(bǔ)零操作實(shí)例代碼
我們?cè)陂_(kāi)發(fā)中為了排版方便或者是輸出文件命名整潔,通常需要給數(shù)字前面補(bǔ)0來(lái)做統(tǒng)一,這篇文章主要給大家介紹了關(guān)于Python數(shù)字/字符串補(bǔ)零操作的相關(guān)資料,需要的朋友可以參考下2021-07-07
Python實(shí)現(xiàn)定時(shí)精度可調(diào)節(jié)的定時(shí)器
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)定時(shí)精度可調(diào)節(jié)的定時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
python sort、sort_index方法代碼實(shí)例
這篇文章主要介紹了python sort、sort_index方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
對(duì)python中的*args與**kwgs的含義與作用詳解
今天小編就為大家分享一篇對(duì)python中的*args與**kwgs的含義與作用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python實(shí)現(xiàn)Decorator模式實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)Decorator模式實(shí)例代碼,簡(jiǎn)單介紹了裝飾器的含義和語(yǔ)法,分享了相關(guān)實(shí)例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別
這篇文章主要介紹了詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-06-06
Python實(shí)現(xiàn)字符串與數(shù)組相互轉(zhuǎn)換功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)字符串與數(shù)組相互轉(zhuǎn)換功能,結(jié)合具體實(shí)例形式分析了Python字符串與數(shù)組相關(guān)轉(zhuǎn)換功能的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-09-09

