标签:
/*十进制转八进制*/
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#define SIZE 300
struct Cstruct
{
int *base; //用来指向栈底
int *top; //用来指向栈顶
int size; //用来初始化栈的大小
Cstruct();//初始化栈
~Cstruct();//销毁栈
bool ifempty();//判断是否为空
bool iffull();//判断是否为满
void push(const int &i);//压栈
int pop();//出栈
int gettop();//获取栈顶元素
};
Cstruct::Cstruct()
{
base=new int[SIZE]; //分配栈空间
top=base;//初始化栈顶指针
size=SIZE;//初始化栈的大小
}
Cstruct::~Cstruct()
{
delete[] this->base;
}
bool Cstruct::ifempty()
{
return base==top;
}
bool Cstruct::iffull()
{
return (top-base)==size;
}
void Cstruct::push(const int &i)
{
if(iffull())
cout<<"已经满了"<<endl;
else
*top++=i;
}
int Cstruct::pop()
{
if(ifempty())
cout<<"已经空了"<<endl;
else
return *--top;
}
int Cstruct::gettop()
{
return *top;
}
char* dtox(int i)
{
Cstruct A;
while(i)
{
A.push(i%8);
i=i/8;
}
char* str=new char[10];
char* p=str;
while(!A.ifempty())
{
*p++=A.pop()+‘0‘;
}
*p=‘\0‘;
return str;
}
int main()
{
cout<<dtox(1348)<<endl;
return 0;
}
标签:
原文地址:http://www.cnblogs.com/innocence-XiaoMing/p/4537137.html