BUPT数据结构第二次大作业第一题进制转换(链式栈

代码仅供参考,希望读者先自行思考

如下:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node {
	struct node* bottom;
	int size;
	char top_ele;
}stack;
stack* createstack()
{
	stack* a = (stack *)malloc(sizeof(stack));
	a->bottom = NULL;
	a->size = 0;
	return a;
}
stack* create_new_ele(char data)
{
	stack* a=(stack*)malloc(sizeof(stack));
	a->bottom = NULL;
	a->top_ele = data;
	return a;
}
void push(stack* a, stack* b)
{
	b->bottom = a->bottom;
	a->bottom = b;
	a->size++;
}
void pop(stack* a)
{
	stack* current = a->bottom;
	stack* prev = a;
	a->size--;
	if (a->size < 0)
	{
		printf("栈为空,无法出栈\n");
	}
	else
	{
		prev->bottom = current->bottom;
		free(current);
	}
}
void printstack(stack* a)
{
	while (a->bottom != NULL)
	{
		printf("%c", a->bottom->top_ele);
		a = a->bottom;
	}
}
int main()
{	
	stack *two_stack=createstack();
	stack *eight_stack= createstack();
	char t;
	while ((t = getchar()) != '#')
	{
		push(two_stack, create_new_ele(t));
	}
	int cent = 3;
	int number = 0;
	int count = two_stack->size;
	while (count > 2)
	{
		cent = 3;
		number = 0;
		while (cent > 0)
		{
			number = number + (two_stack->bottom->top_ele - 48) * pow(2, 3 - cent);
			cent--;
			two_stack = two_stack->bottom;
			count--;
		}
		push(eight_stack, create_new_ele(number%8+48));
	}
	if (count> 0)
	{
		cent = 3;
		number = 0;
		while (count > 0)
		{
			number = number + (two_stack->bottom->top_ele - 48) * pow(2, 3 - cent);
			cent--;
			two_stack = two_stack->bottom;
			count--;
		}
		push(eight_stack, create_new_ele(number%8+48));
	}
	printstack(eight_stack);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BUPT(北京邮电大学)大一下学期的数据结构课程是计算机专业非常重要的一门课程。数据结构是计算机科学的基础之一,是对数据的存储、检索、组织和处理的方式和原则的学习。在这门课程中,我们学习了数组、链表、栈、队列、树、图等常见的数据结构,并掌握了它们的基本原理、实现方法以及应用场景。通过学习数据结构,我们能够更加深入地理解算法和程序设计,并且能够在实际的软件开发中更加高效地运用数据结构来解决问。 在数据结构课程中,我们除了理论知识的学习,也进行了大量的实践操作,通过编程实践来加深对数据结构的理解和掌握。我们使用C++语言来实现不同的数据结构,并且进行了相关的调试和测试。通过实际编程的训练,我们不仅能够加深对数据结构的理解,还可以提高自己的编程能力和问解决能力。 这门课程的学习不仅仅是为了考试,更重要的是为了将来的学习和工作打下坚实的基础。数据结构是计算机科学的核心知识之一,对于我们作为计算机专业的学生来说,精通数据结构是至关重要的。通过学习数据结构课程,我们能够为以后的专业课程和实际工作奠定坚实的基础,为我们以后的发展打下坚实的基础。因此,我们会全力以赴地学习这门课程,努力掌握数据结构的核心知识和编程技能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值