源代码,可运行
#include<stdlib.h>
#include<stdio.h>
#include<stddef.h>
#define MAXSIZE 100
typedef char ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SeqStack, * PSeqStack;
PSeqStack Init_SeqStack()
{
PSeqStack s;
s = (PSeqStack)malloc(sizeof(SeqStack));
if (s)
s->top = -1;
return s;
}
int Empty_SeqStack(PSeqStack s)
{
if (s->top == -1)
return 1;
else
return 0;
}
int Push_SeqStack(PSeqStack s, ElemType x)
{
if (s->top == MAXSIZE - 1)
return 0;
else
{
s->top = s->top + 1;
s->data[s->top] = x;
return 1;
}
}
int Pop_SeqStack(PSeqStack s, ElemType * x)
{
if (Empty_SeqStack(s))
return 0;
else
{
*x = s->data[s->top];
s->top = s->top - 1;
return 1;
}
}
int GetTop_SeqStack(PSeqStack s, ElemType * x)
{
if (Empty_SeqStack(s))
return 0;
else
*x = s->data[s->top];
return 1;
}
void Destroy_SeqStack(PSeqStack * s)
{
if (*s)
free(*s);
*s = NULL;
return;
}
void Conversion(int num, int r)
{
PSeqStack s;
ElemType x;
if (!r)
{
printf("基数不能为0");
return;
}
s = Init_SeqStack();
if (!s)
{
printf("初始化栈空间失败");
return;
}
while (num)
{
if (num % r > 9)
Push_SeqStack(s, num % r + 'A' - 10);
else
Push_SeqStack(s, num % r + '0');
num = num / r;
}
while (!Empty_SeqStack(s))
{
Pop_SeqStack(s, &x);
printf("%c", x);
}
}
int main()
{
int r, num;
char again = 'y';
while (again == 'y' || again == 'Y')
{
printf("请输入要转换的数据:");
scanf("%d", &num);
printf("请输入要转换成几进制:");
scanf("%d", &r);
Conversion(num, r);
printf("继续【y/n】?:");
scanf("%s", &again);
}
return 0;
}
运行结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)