c language: stack training---number conversion (base conversion)

c language: stack training-number conversion (base conversion)

Convert the input decimal to any hexadecimal
basic idea: the Decimal to octal conversion process
following is the code area:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct SqStack{
	int data[100];
	int top;
}SqStack;
void Init(SqStack *S) //��ʼ����ջ 
{	
	if(S==NULL)
		exit(1);
	S->top = -1;	
}
void Push(SqStack *S,int part)
{
	if(S==NULL) 
		exit(1);
	S->top++;
	S->data[S->top] = part;
}
void conversion (int N, int d, SqStack *S) 
{ 
	/* ������Nת����d����*/ 
	while (N) 
	{
		Push(S,N % d);
		N = N/d; 
	} 
} // conversion

void Output(SqStack *S)
{
	int i;int out=0;
	if(S->top==-1)
	{
		printf("(无数据)\n");
		exit(0);
	}
	for(i=0;i<=S->top;i++)
	{
		out=out+S->data[i]*pow(10,i);
	}
	printf("(%d)",out);
	
}
void main()
{
	//(1348)10 = (2504)8 //案例
	int N,d;
	SqStack S;
	Init(&S);
	printf("输入十进制数:");
	scanf("%d",&N);
	printf("\n"); 
	printf("输入要转换的进制:");
	scanf("%d",&d); 
	conversion(N,d,&S);
	Output(&S);
	printf("%d\n",d);
} 

Guess you like

Origin blog.csdn.net/weixin_46490003/article/details/105332167