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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

运行结果:

代码:

#include

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

相关文档
最新文档