設計一個演算法,將一個十進位制的數轉化為二進位制
阿新 • • 發佈:2018-12-02
#include<iostream.h>
const int StackSize=10;
class SeqStack
{
public:
SeqStack(){top=-1;}
~SeqStack(){}
voidPush(int x);
void Pop();
private:
intdata[StackSize];
int top;
};
void SeqStack::Push(int x)
{
top=-1;
int k;
int r=2;
while(x!=0)
{
k=x%r;
data[++top]=k;
x=x/r;
}
}
void SeqStack::Pop()
{
if (top == -1)throw "下溢";
while(top!=-1)
{
intx=data[top--];
cout<<x;
}
}
int main()
{
int i=1;
intnumber;
SeqStackn;
cout<<"請輸入一個十進位制整數!"<<endl;
cin>>number;
n.Push(number);
n.Pop();
cout<<endl;
return 0;
}
執行結果: