c言語:スタックトレーニング---数値変換(ベース変換)

c言語:スタックトレーニング-数値変換(ベース変換)

入力された10進数を任意の16進数の
基本的な考え方に変換します10進数から8進数への変換プロセス
以下はコード領域です。

#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);
} 

おすすめ

転載: blog.csdn.net/weixin_46490003/article/details/105332167