Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
#include <iostream>
#include <string>
using namespace std;

// defining a class
class bin_checker
{

  string bin;
  void check_bin();
  void ones_compliment();
  void display_bin();

public:
  void get_bin();
};

// taking the bin from the user
void bin_checker::get_bin()
{
  cout << "Enter the binary number : ";
  cin >> bin;
  check_bin(); // pushing the binary to next fx
}

// checking the if the number is actually a bin or not
void bin_checker::check_bin()
{
  for (int i = 0; i < bin.length(); i++)
  {
    if (bin.at(i) != '1' && bin.at(i) != '0')
    {
      cout << bin << " is not a binary number !!" << endl;
      exit(0);
    }
  }
  // void test();
  // void test()
  // {
  //   cout << "I am the king";
  // }
  // test();
  ones_compliment();
}

// doing one's compliment of it
void bin_checker::ones_compliment()
{
  for (int i = 0; i < bin.length(); i++)
  {
    if (bin.at(i) == '1')
    {
      bin.at(i) = '0';
    }
    else
    {
      bin.at(i) = '1';
    }
  }
  display_bin();
}

// final displaying of it
void bin_checker::display_bin()
{
  cout << "After doing its compliment the binary number is " << bin << endl;
}
int main()
{
  bin_checker num_1;
  num_1.get_bin();
  return 0;
}