利用链表模拟实现将十进制数2015转换为对应的八进制数。

利用链表模拟实现将十进制数2015转换为对应的八进制数。程序设计思路(包括各题的算法思想、程序中类函数定义说明和功能描述)

 主要代码及注释(各题代码及注释书写工整)

#include"stdio.h"
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node *next;
}*linkstack;
 
//入栈
int Push(linkstack *top,datatype x)
{
	linkstack s=(linkstack)malloc(sizeof(struct node));
	if(s==NULL)
		return 0;
	s->data=x;
	s->next=(*top);
	(*top)=s;
	return 1;
}
 
//判空
int Empty(linkstack top)//判断栈是否为空
{
	if(top==NULL)
		return 1;
	return 0;
}
//出栈
int Out(linkstack *top,datatype *x)//出栈
{
	if(top!=NULL)
	{
		linkstack p=(*top);
		(*x)=(*top)->data;
		(*top)=(*top)->next;
		free(p);
		return 1;
	}
	return 0;
}
//十进制整数转换为其他进制数
void Transform(int num,int mode)//实现进制转换
{
	int h;
	linkstack top=NULL;
	printf("转化结果:");
	if(num>0)
	{
		while(num!=0)
		{
			h=num%mode;//取余
			Push(&top,h);
			num=num/mode;//取整
		}
		while(!Empty(top))
		{
			Out(&top,&h);
			printf("%d",h);
		}
		printf("\n");
	}
	else if(num<0)//当输入数字小于0
	{
		printf("-");
		num=num*(-1);//将负数装换为正数
		while(num!=0)
		{
			h=num%mode;
			Push(&top,h);
			num=num/mode;
		}
		while(!Empty(top))
		{
			Out(&top,&h);
			printf("%d",h);
		}
		printf("\n");
	}
	else
		printf("%d\n",0);
}
void main()
{
	int num,mode;
	printf("请输入要转化的数:");
	scanf("%d",&num);
	printf("输入要转换的进制:");
	scanf("%d",&mode);
	if(mode==0||mode==1)//判断要转换的进制是否合法
	{
		printf("转换进制无效\n\n");
		return;
	}
	Transform(num,mode);
}

https://blog.csdn.net/qq_43637080/article/details/88973299