用顺序表实现十进制转R进制(C语言)

#include <stdio.h>
#include <stdlib.h>

#define M 100
typedef int ElemType;
typedef struct 
{
	ElemType data[M];
	int top;
}Stack;

//初始化栈
void InitStack(Stack *s)
{
	s->top = -1;
}
int Push(Stack *s,ElemType e)
{
	if (s->top == M-1)
	{
		printf("栈满\n");
		return 0;
	}
	s->top++;
	s->data[s->top]=e;
	return 1;
}

//判断是否为空
int Empty(Stack *s)
{
	if (s->top==-1)
		return 1;
	else
		return 0;
}
//出栈
int Pop(Stack *s,ElemType *e)
{
	if(Empty(s))
	{
		printf("\n Stack is free");
		return 0;
	}
	*e=s->data[s->top];
	s->top--;
	return 1;
}

void Conversion(int N)
{
	int e;
	Stack *s = (Stack *)malloc(sizeof(Stack));
	InitStack(s);
	int X;
	printf("请输入转换的进制:");
	scanf("%d",&X);
	while(N)
	{
		Push(s,N%X);
		N = N / X;
	}
	while(!Empty(s))
	{
		Pop(s,&e);
		printf("%d",e);
	}
}

int main()
{

	int n,m;
	while(1)
	{
		printf("1:进行转换,2:退出\n");
		scanf("%d",&n);
		switch(n)
		{
			case 1:	printf("请输入十进制的整数值: ");
					scanf("%d",&m);
					Conversion(m);
					printf("\n");
					break;
			case 2:	exit(0);
			default:	printf("error\n");
		}
	}
}

 

    原文作者:进制转换
    原文地址: https://blog.csdn.net/SJCHEN1221/article/details/83039093
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞