#include<stdio.h>
#include<stdlib.h>
typedef struct StackNode{
int data;
struct StackNode *next;
}StackNode, *StackNodePtr;
typedef struct{
StackNodePtr top;
int count;
}LinkStack;
//初始化
bool InitStack(LinkStack *S){
S->top = NULL;
S->count = 0;
}
//入栈
int Push(LinkStack *S, int e){
StackNodePtr p = (StackNodePtr)malloc(sizeof(StackNode));
p->data = e;
p->next = S->top;
S->top = p;
S->count++;
return 0;
}
//出栈
int Pop(LinkStack *S){
int e;
StackNodePtr p;
p = S->top;//p指针指向的结点为栈顶结点
e = p->data;
S->top = p->next;
free(p);
return e;
}
//进制转换函数,num为十进制数,i为i进制
int Change(LinkStack *S,int num, int i){
while(num){
Push(S, num % i);//余数存入链栈
num = num / i;
}
printf("%d进制为:", i);
while(S->top != NULL){//当链栈不为空,输出栈顶元素
printf("%d", Pop(S));
}
printf("\n");
}
int main(){
int num;
LinkStack S;
InitStack(&S);
printf("请输入一个十进制数(正整数):");
scanf("%d", &num);
Change(&S, num, 2);
Change(&S, num, 8);
Change(&S, num, 16);
}
将十进制数进制转换,并存入链栈
最新推荐文章于 2022-10-04 11:26:14 发布