设计算法,把十进制的整数转换为二至九进制之间的任一进制输出。
算法如下:
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();
}
测试结果如下: