#include<stdio.h>
#include<stdlib.h>
#define max 100
typedef struct{
int data[max];
int top;
}Stack;
void CreatStack(Stack* &s)//创建栈
{
s=(Stack*)malloc(sizeof(Stack));
s->top=-1;
}
void PushStack(Stack* &s,int n)//入栈
{
int m=n;
while(m!=0)
{
s->top++;
n=n%8;
s->data[s->top]=n;
m=m/8;
n=m;
}
}
void PopStack(Stack* &s)//出栈
{
while(s->top>=0)
{
printf("%d",s->data[s->top]);
s->top--;
}
}
void DestoryStack(Stack* &s)
{
free(s);
}
int main()
{
int num;
Stack* L;
printf("--------十进制数转变八进制数-----------\n");
printf("请输入所需要转换的数字:\n");
scanf("%d",&num);
CreatStack(L);
PushStack(L,num);
PopStack(L);
DestoryStack(L);
return 0;
}
十进制转变为八进制(用栈解决)
最新推荐文章于 2024-05-28 16:23:43 发布