Description
利用顺序栈的基本操作算法,编写满足下列要求的数制转换程序:对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数。
输入格式
第一行:输入一个非负的十进制整数
输出格式
第一行:与输入等值的八进制数
输入样例
15
输出样例
17
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int SElemType;
typedef int status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}Sqstack;
status Initstack(Sqstack &S)
{
S.base=new SElemType[MAXSIZE];
if(!S.base)
return ERROR;
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
status Push(Sqstack &S,SElemType e)
{
if(S.top-S.base==MAXSIZE)
return ERROR;
else
{
*S.top=e;
S.top++;
}
return OK;
}
status Loadstack(Sqstack S)
{
SElemType *p;
p=S.top;
while(p>S.base)
{
p--;
printf("%d",*p);
}
printf("\n");
return OK;
}
int main()
{
int n;
Sqstack S;
Initstack(S);
scanf("%d",&n);
while(n)
{
Push(S,n%8);
n/=8;
}
Loadstack(S);
return OK;
}
但是其实不用栈也可以通过...(懒)
#include<stdio.h>
int a[1000];
int main()
{
int n;
scanf("%d",&n);
int i=0,j=0;
while(n)
{
a[i++]=n%8;
n/=8;
j++;
}
for(j=j-1;j>=0;j--)
{
printf("%d",a[j]);
}
return 0;
}