C++ Program to Convert Decimal to Binary Using Stack

Today I’m going to explain that how you can convert decimal to binary using stack class in c++. Logic is clear, first, we’ll divide the decimal by 2 and remainder is pushed onto stack and our new decimal will be decimal/2. This is carried out until the decimal becomes 0. Then we’ll pop all remainders into a integer array and display it on the screen. Let’s see codes below;

I’ve stack header file and .cpp file;

Stack.h:

#ifndef STACK_H
#define STACK_H

class Stack
{
public:
	Stack(int size);
	virtual ~Stack();

	bool Pop(int *pData);
	bool Push(int data);
	bool IsFull();
	bool IsEmpty();
	bool Peek(int *pData);

private:
	int m_capacity;
	int m_size;
	int *m_pData;

};

#endif

Stack.cpp:

#include "Stack.h"
#include iostream

using namespace std;

Stack::Stack(int size)
:m_capacity(size),
m_size(0)
{
	m_pData = new int[size];
}

Stack::~Stack()
{
	delete[] m_pData;
}

bool Stack::IsEmpty()
{
	if (m_size == 0)
		return true;
	else
		return false;
}
bool Stack::IsFull()
{
	if (m_size == m_capacity)
		return true;
	else
		return false;
}

bool Stack::Push(int data)
{
	if (IsFull())
		return false;
	else{
		m_pData[m_size] = data;
		m_size++;
		return true;
	}

}

bool Stack::Pop(int* pData)
{
	if (IsEmpty())
		return false;
	else{
		*pData = m_pData[m_size - 1];
		m_size--;
		return true;
	}
}

bool Stack::Peek(int* pData)
{
	if (IsEmpty())
		return false;
	else{
		*pData = m_pData[m_size - 1];
		return true;
	}
}

Now, in my main.cpp I’ll make the convert.

main.cpp:

#include iostream
#include "Stack.h"

#define SIZE 15

using namespace std;

int main(int argc, char** argv)
{
	Stack stack(SIZE);
	int decimal;
	int remainder;
	int count=0;
	int binary[SIZE];

	cout << "Enter decimal: "; 	
	cin >> decimal;

	int dec = decimal;

	while (((double)decimal / 2.0) != 0) {
		remainder = decimal % 2;
		decimal = decimal / 2;
		if (stack.Push(remainder)){
			cout << "Pushed: " << remainder << endl;
			count++;
		}
		else
			cout << "Not Pushed: " << remainder << endl;

	}

	cout << "****************" << endl;
	cout << "Binary of " << dec << " : ";

	for (int i = 0; i < count; i++){
		if (stack.Pop(&binary[i]))
			cout << binary[i];

	}

	 cout << endl << "****************" << endl;

	return 0;
}

The int type can represent integers in the range from negative 2,147,483,648 to positive 2,147,483,647. Binary of 2,147,483,647 is 111111111111111 (15 digits). Thats why I defined SIZE 15. Stack and binary array holds maximum 15 digits.

If you don’t casting in while loop, you won’t get the right result. Why? Let me trace it. I want to convert “2” to binary. It must be “10”.

while ((decimal / 2) != 0) {
		remainder = decimal % 2;
		decimal = decimal / 2;
		if (stack.Push(remainder)){
			cout << "Pushed: " << remainder << endl;
			count++;
		}
		else
			cout << "Not Pushed: " << remainder << endl;

	}

1st case:
decimal = 2;
2/2 = 1, enters the loop.
remainder = 0;
decimal = 1;

2nd case:
1/2 = 0 (we are using int types). Opps what happened? loop ends and the result will “0”.

But, if we would type casting, 1.0/2.0 = 0.5 which is not 0, thereby loop continues. And our 2nd case would be;
remainder = 1;
decimal = 1/2 = 0.
Loop would end and the result would be “10” after pop all remainders.

After debugging;

stack

Reklamlar

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Connecting to %s