习题5的第3小题

来源:互联网 发布:网络运维与管理 编辑:程序博客网 时间:2024/05/19 02:00

设计算法,把十进制的整数转换为二至九进制之间的任一进制输出。

算法如下:

const int StackSize=10;
#include<iostream>
using namespace std;
class SeqStack
{
private:
int data[StackSize];
int top;
public:
SeqStack(){top=-1;}
void Push(int x);
void Pop();
};
void SeqStack::Push(int x)
{
data[++top]=x;
}
void SeqStack::Pop()
{
while(top!=-1)
{
int x=data[top--];
cout<<x;
}
}
void main()
{
int a,b;
cout<<"输入整数a:";
cin>>a;
cout<<"转换的进制为:";
cin>>b;
SeqStack s;
while(a!=0)
{
int c=a%b;
s.Push(c);
a=a/b;
}
cout<<"转换后:";
s.Pop();
}

测试结果如下:


原创粉丝点击