您的位置:首页 > 编程语言 > C语言/C++

十进制转换为任意进制(栈 c++版)

2013-07-02 23:12 316 查看
//十进制转换为任意进制
#include <iostream>
using namespace std;
#define MAXSIZE 20
class Transform
{
private:
int* Buffer;
int Top;
int End;
public:
Transform()
{
Buffer = new int[MAXSIZE+1];
Top = End = 0;
}
~Transform()
{
delete [] Buffer;
}
void Push(int i);
int Pop();
bool Empty();

};
void Transform::Push(int i)
{
if(Top < MAXSIZE)
{
Top = Top + 1;
Buffer[Top] = i;
}
else
{
cout<<"溢出!"<<endl;
}
}

int Transform::Pop()
{
if(Top > 0)
{
int i = Buffer[Top];
Top = Top - 1;
return i;
}
else
{
cout<<"栈空!"<<endl;
return NULL;
}
}
bool Transform::Empty()
{
if(Top == End)
{
return true;
}
else
{
return false;
}
}
int main()
{

int a[2] = {7856,1348},b;
int r[4] = {8,6,4,2};

for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 4; j++)
{
int temp = a[i];
Transform trans;
cout<<"将10进制"<<a[i]<<"转换为"<<r[j]<<"进制为:";
while(temp != 0)
{
b = temp % r[j];
temp = temp / r[j];

trans.Push(b);
}
while(!trans.Empty())
{
cout<<trans.Pop();
}
cout<<endl;
}
cout<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: