十进制转十六进制c语言顺序栈,C语言 顺序栈实现十进制转换为二进制,八进制,十六进制...

#include #define MAX 20

typedef struct

{

int data[MAX];

int top;

}SeqStack;

SeqStack* Init()

{

SeqStack *s;

s = (SeqStack *)malloc(sizeof(SeqStack));

s->top = -1;

return s;

}

void Destroy(SeqStack *s)

{

free(s);

}

bool IsFull(SeqStack *s)

{

return (s->top == MAX - 1) ? true : false;

}

bool IsEmpty(SeqStack *s)

{

return (s->top == -1) ? true : false;

}

void Push(SeqStack *s, int a)

{

if (IsFull(s))

{

printf("The stack is full, failed to push!\n");

return;

}

s->top++;

s->data[s->top] =a;

}

int Pop(SeqStack *s)

{

int e;

if (IsEmpty(s))

{

printf("The stack is empty, failed to pop!\n");

return NULL;

}

e = s->data[s->top];

s->top--;

return e;

}

int ReadTop(SeqStack *s)

{

return s->data[s->top];

}

void Print(SeqStack *s)

{

int temp = s->top;

if (IsEmpty(s))

{

printf("The stack is empty!\n");

return;

}

printf("转换后的结果:\n");

while (temp >= 0)

{

if (s->data[temp]<10)

printf("%d ", s->data[temp]);

else

{

if (s->data[temp] = 10)printf("a");

else if (s->data[temp] = 11)printf("b");

else if (s->data[temp] = 12)printf("c");

else if (s->data[temp] = 13)printf("d");

else if (s->data[temp] = 14)printf("e");

else printf("f");

}

temp--;

}

printf("\n");

}

int main()

{

int m,c,d,n;

SeqStack *s;

s = Init();

printf("请输入要转换的十进制数:");

scanf("%d", &m);

printf("\n");

printf("请输入转换进制:\n");

printf("******************************\n");

printf("* 请选择一个你要转换的进制 *\n");

printf("* 1.二进制 *\n");

printf("* 2.八进制 *\n");

printf("* 3.十六进制 *\n");

printf("******************************\n");

scanf("%d",&d);

printf("\n");

if (d == 1)n = 2;

else if (d == 2)n = 8;

else if (d == 3)n = 16;

else printf("输入有误!");

while (m)

{

c =m%n;

m = m / n;

Push(s, c);

}

Print(s);

Destroy(s);

}

你可能感兴趣的:(十进制转十六进制c语言顺序栈)