数据结构 栈的应用 进制转换
【摘要】 #include <stdio.h>#include <stdlib.h> #define STACT_INIT_SIZE 100#define STACTINCREMENT 10#define OK 1#define ERROR 0#define OVERFLOW -2 typedef int SElemType; typedef struct...
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
#define STACT_INIT_SIZE 100
-
#define STACTINCREMENT 10
-
#define OK 1
-
#define ERROR 0
-
#define OVERFLOW -2
-
-
typedef int SElemType;
-
-
-
typedef struct
-
{
-
SElemType *base;
-
SElemType *top;
-
int stacksize;
-
}SqStack;
-
-
SqStack S;
-
-
int InitStack(SqStack &S)
-
{
-
S.base = (SElemType *)malloc(STACT_INIT_SIZE * sizeof(SElemType));
-
if (!S.base)
-
{
-
exit(OVERFLOW);
-
}
-
S.top = S.base ;
-
S.stacksize = STACT_INIT_SIZE;
-
return OK;
-
}
-
-
int Push(SqStack &S, SElemType e)
-
{
-
if (S.top - S.base >= S.stacksize)
-
{
-
S.base = (SElemType*)realloc(S.base , (S.stacksize + STACTINCREMENT) * sizeof(SElemType));
-
if (!S.base)
-
{
-
exit(OVERFLOW);
-
}
-
S.top = S.base + S.stacksize ;
-
S.stacksize += STACTINCREMENT;
-
}
-
*S.top++ = e;
-
return OK;
-
}
-
-
int Pop(SqStack &S, SElemType &e)
-
{
-
if (S.base == S.top)
-
{
-
return ERROR;
-
}
-
e = *--S.top;
-
return OK;
-
}
-
-
-
-
-
-
int StackEmpty(SqStack S)
-
{
-
if (S.top == S.base )
-
{
-
return 1;
-
}
-
return 0;
-
}
-
-
-
void conversion(int n)
-
{
-
int e;
-
while (n != 0)
-
{
-
Push(S,n%8);
-
n = n/8;
-
}
-
while (!StackEmpty(S))
-
{
-
Pop(S,e);
-
printf("%d",e);
-
}
-
printf("\n");
-
}
-
-
-
-
-
-
-
int main()
-
{
-
int n;
-
printf("请输入一个非负的十进制整数:");
-
scanf("%d",&n);
-
printf("十进制数 %d 转换为八进制是:",n);
-
conversion(n);
-
return 0;
-
}
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/17249101
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)