将十进制数转换为八进制,并输出。
#include <iostream>
#include <fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
typedef struct StackNode
{
SElemType data;
struct StackNode *next;
} StackNode, *LinkStack;
// 算法3.5 链栈的初始化
void InitStack(LinkStack &s) // 构造一个空栈 S,栈顶指针置空
{
s = NULL;
}
void DestroyStack(LinkStack &s)
{
LinkStack next;
while (s)
{
next = s->next;
delete s;
s = next;
}
}
// 算法3.6 链栈的入栈
void Push(LinkStack &s, SElemType e) // 在栈顶插入元素e
{
LinkStack p;
p = new StackNode;
p->data = e;
p->next = s;
s = p;
}
void PrintStack(LinkStack s)
{
for (LinkStack p = s; p; p = p->next)
{
cout << p->data;
}
}
int main()
{
int e;
while (cin >> e)
{
LinkStack s;
InitStack(s);
while (e)
{
Push(s, e%8);
e=e/8;
}
PrintStack(s);
cout <<endl;
DestroyStack(s);
}
return 0;
}