Python和C语言利用栈分别实现进制转换
作者:观察者555 时间:2022-10-02 15:43:15
问题描述
利用栈的数据结构实现将十进制数转换成二进制数
C语言实现
顺序表的存储结构实现栈
代码:
#include <stdlib.h>
#include <stdio.h>
#define STACK_INIT_SIZE 100 //栈初始开辟空间大小
#define STACK_INCREMENT 10 //栈追加空间大小
//栈的结构体
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;
}
//主函数
void main()
{
int dec;
binStack bs = stack_init();
printf("请输入十进制整数:\n");
scanf("%d", &dec);
while(dec)
{
push(&bs, dec%2);
dec /= 2;
}
printf("转换后的二进制数是:\n");
while(bs.top != bs.base)
{
printf("%d", pop(&bs));
}
printf("\n\n");
system("date /T");
system("TIME /T");
system("pause");
exit(0);
}
运行结果:
Python实现
对于stack我们可以使用python内置的list实现(也可以用链表实现),因为list是属于线性数组,在末尾插入和删除一个元素所使用的时间都是O(1),这非常符合stack的要求。
代码:
import datetime //显示时间引入的库
import time //
from pip._vendor.distlib.compat import raw_input //使命令窗口不立即关闭引入的库
//栈类
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("请输入十进制整数:\n"))
print("转换后的二进制数是:")
while dec != 0:
binStack.push(dec%2)
dec //= 2
while binStack.isEmpty() == True:
print("{}".format(binStack.pop()), end="")
else:
print("\n")
//打印时间
datetime = datetime.datetime.now()
print(datetime.strftime("%Y-%m-%d\n%H:%M:%S"))
//使命令窗口不立即关闭
input("Press Enter to exit…")
运行结果:
来源:https://blog.csdn.net/guanchazhe55/article/details/125653716
标签:Python,C语言,栈,进制转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
图文详解OpenCV中光流以及视频特征点追踪
2022-12-30 01:45:08
![](https://img.aspxhome.com/file/2023/6/112466_0s.png)
各种Python库安装包下载地址与安装过程详细介绍(Windows版)
2021-01-27 14:00:05
![](https://img.aspxhome.com/file/2023/9/89089_0s.png)
微信 用脚本查看是否被微信好友删除
2021-12-30 19:59:42
![](https://img.aspxhome.com/file/2023/6/123626_0s.jpg)
Mysql两表联合查询的四种情况总结
2024-01-26 20:58:49
![](https://img.aspxhome.com/file/2023/4/109824_0s.png)
JS实现匀速与减速缓慢运动的动画效果封装示例
2024-06-07 15:27:01
![](https://img.aspxhome.com/file/2023/6/122596_0s.gif)
python 实现识别图片上的数字
2021-06-17 13:09:31
![](https://img.aspxhome.com/file/2023/6/127796_0s.jpg)
ASP判断E-Mail的合法性,以及过滤邮箱字符
2010-05-27 12:23:00
使用css2.1实现多重背景、多重边框效果[译]
2010-08-23 16:32:00
![](https://img.aspxhome.com/file/UploadPic/20108/23/css2-multiple-background-border-model-12s.png)
利用pyuic5将ui文件转换为py文件的方法
2023-03-20 05:01:43
![](https://img.aspxhome.com/file/2023/4/67804_0s.jpg)
Python代码覆盖率统计工具coverage.py用法详解
2021-02-02 22:55:51
浅谈keras中的batch_dot,dot方法和TensorFlow的matmul
2023-04-08 07:21:28
![](https://img.aspxhome.com/file/2023/1/75951_0s.jpg)
Python解析器Cpython的GIL解释器锁工作机制
2021-07-11 18:15:10
![](https://img.aspxhome.com/file/2023/4/118654_0s.png)
Python动态演示旋转矩阵的作用详解
2022-08-21 02:35:15
![](https://img.aspxhome.com/file/2023/9/70079_0s.png)
Python实例解析图像形态学运算技术
2021-04-14 10:34:24
![](https://img.aspxhome.com/file/2023/3/135143_0s.png)
python清除字符串里非数字字符的方法
2023-08-12 02:47:32
彻底解决MySQL使用中文乱码的方法
2024-01-22 02:05:28
![](https://img.aspxhome.com/file/2023/9/68129_0s.jpg)
禁用JavaScript控制台调试的方法
2024-06-22 03:09:08
OpenCV特征提取与检测之Shi-Tomasi角点检测器
2023-12-17 09:41:35
![](https://img.aspxhome.com/file/2023/8/131018_0s.png)
Spring数据库连接池实现原理深入刨析
2024-01-12 17:16:53
![](https://img.aspxhome.com/file/2023/4/71234_0s.png)
python简单实现旋转图片的方法
2021-06-09 08:29:11