1. 程式人生 > >實驗二 十進位制轉化為二進位制

實驗二 十進位制轉化為二進位制

#include<iostream>
using namespace std;
const int Stacksize=10;
class SeqStack
{
public:
	SeqStack(){top=-1;}
	~SeqStack(){}
	int top;
	int data[Stacksize];
};


int main()
{
	int x;
	SeqStack s;
	s.top=-1;
	cout<<"請輸入十進位制的數:"<<endl;
	cin>>x;
	cout<<x;
	do
	{
		s.top++;
		s.data[s.top]=x%2;
		x=x/2;
	}while(x!=0);
	cout<<"的二進位制為:";
	do
	{
		cout<<s.data[s.top];
		s.top--;
	}while(s.top!=-1);
	cout<<endl;
	return 0;
}