将一个10进制数,转换为2进制或8进制
#include<iostream>
#include <stack>
using namespace std;
void transform(int n,int b)
{
int i;
stack<int> mystack;//建立一个栈
while(n)
{
mystack.push(n%b);//入栈
n=n/b;
}
while(!mystack.empty())//当栈不为空时
{
i=mystack.top();//取栈顶元素
mystack.pop();//移除一个栈顶元素
cout<<i;
}
}
int main()
{
int figure,d;//figure是一个数字,d表示进制
cout<<"Input the integer you want to transform:";
cin>>figure;//输入数字
cout<<"Input the integer of the system:";
cin>>d;//输入进制
transform(figure,d);//转换
system("pause");
return 0;
}