This program might seem lazy, I also tried iterating through the vector but performance was poor so I used this lazy approach that has more performance
#ifndef BINARY_HH
#define BINARY_HH
/**************************************************************************
* Name: Binary.hh
* Author: Samuel Oseh
* Date: 27/09/2020
* File Info: This file contains class method-function prototypes
* Purpose: Binary Converter is a program that converts a decimal
* to its binary equivalents and also performs other calculations
**************************************************************************/
#include <vector>
#include <string>
class Binary {
public:
Binary( const int &, const char & );
~Binary() {}
Binary( const Binary & );
void printBinary() const;
void toBinary();
char getType() const;
int getDecimal() const;
Binary &operator+=( const Binary &binaryToAdd );
Binary &operator-=( const Binary &binaryToSub );
Binary &operator*=( const Binary &binaryToMul );
Binary &operator/=( const Binary &binaryToDiv );
bool operator==( const Binary &binaryToCompare ) const;
inline bool operator!=( const Binary &binaryToCompare ) const {
return !( *this == binaryToCompare );
}
const Binary &operator=( const Binary &binaryToCopy );
private:
char type;
int decimal;
std::vector< int > binary{};
};
#endif
/**************************************************************************
* Name: Binary.cc
* Author: Samuel Oseh
* Date: 27/09/2020
* File Info: This file contains class method-function definitions
* Purpose: Binary Converter is a program that converts a decimal
* to its binary equivalents and also performs other calculations
**************************************************************************/
#include <iostream>
#include <stdexcept>
#include "Binary.hh"
Binary::Binary( const int &d, const char &t ) {
if ( tolower( t ) == 'd' )
type = t;
else
throw std::invalid_argument( "type must be 'd' only." );
if ( d < 0 )
throw std::invalid_argument( "decimal value must be greater than 0." );
decimal = d;
}
Binary::Binary( const Binary &binaryToCopy ) {
decimal = binaryToCopy.decimal;
type = binaryToCopy.type;
for ( unsigned int counter = 0; counter < binaryToCopy.binary.size(); ++ counter ) {
binary.push_back( binaryToCopy.binary[ counter ] );
}
}
void Binary::toBinary() {
if ( type == 'd' ) {
int val = decimal;
while ( val != 0 ) {
binary.insert( binary.begin(), val % 2 );
val /= 2;
}
}
else {
throw std::invalid_argument( "Invalid type conversion" );
}
}
void Binary::printBinary() const {
if ( binary.size() == 0 )
throw std::invalid_argument( "Cannot print binary object without converting it" );
for ( int number : binary ) {
std::cout << number << "";
}
}
char Binary::getType() const {
return type;
}
int Binary::getDecimal() const {
return decimal;
}
Binary &Binary::operator+=( const Binary &binaryToAdd ) {
if ( binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
if ( binaryToAdd.binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
int decimalSum = decimal + binaryToAdd.decimal;
Binary *b = new Binary( decimalSum, 'd' );
b->toBinary();
*this = *b;
delete b;
return *this;
}
Binary &Binary::operator-=( const Binary &binaryToAdd ) {
if ( binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
if ( binaryToAdd.binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
int decimalSum = decimal - binaryToAdd.decimal;
if ( decimalSum < 0 ) {
throw std::invalid_argument( "Can not perform subtraction from a lesser binary" );
}
Binary *b = new Binary( decimalSum, 'd' );
b->toBinary();
*this = *b;
delete b;
return *this;
}
Binary &Binary::operator*=( const Binary &binaryToMul ) {
if ( binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
if ( binaryToMul.binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
int decimalSum = decimal * binaryToMul.decimal;
Binary *b = new Binary( decimalSum, 'd' );
b->toBinary();
*this = *b;
delete b;
return *this;
}
Binary &Binary::operator/=( const Binary &binaryToDiv ) {
if ( binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
if ( binaryToDiv.binary.size() == 0 )
throw std::invalid_argument( "Cannot add binary object without converting it" );
int decimalSum = decimal / binaryToDiv.decimal;
Binary *b = new Binary( decimalSum, 'd' );
b->toBinary();
*this = *b;
delete b;
return *this;
}
bool Binary::operator==( const Binary &binaryToCompare ) const {
if ( decimal == binaryToCompare.decimal )
return true;
return false;
}
const Binary &Binary::operator=( const Binary &binaryToCopy ) {
decimal = binaryToCopy.decimal;
type = binaryToCopy.type;
binary.clear();
for ( unsigned int counter = 0; counter < binaryToCopy.binary.size(); ++ counter ) {
binary.push_back( binaryToCopy.binary[ counter ] );
}
return *this;
}
std::bitset
andstd::vector<bool>
, aren't you? \$\endgroup\$std::bitset
already supports all thoseoperator
functions you need, including ato_string()
function to get a printable output, andto_long()
to provide conversion to a "normal decimal". Ah, and a constructor that takes a string consisting of'0'
and'1'
characters. Simila forstd::vector<bool>
, which is a space optimized implementation ofstd::vector<T>
, probably the better choice than astd::vector<int>
. \$\endgroup\$