Check the number is binary or not and if binary then its one's compliment.
#include <iostream>
#include <string>
using namespace std;
class binary
{
private:
string s;
public:
void input();
void check_binary();
void ones_compliment();
void display();
};
void binary::input()
{
cout << "Enter a number : ";
cin >> s;
}
void binary::check_binary()
{
for (int i = 0; i < s.length(); i++)
{
if (s.at(i) != '0' && s.at(i) != '1')
{
cout << "Not binary ";
exit(0);
}
}
}
void binary::ones_compliment()
{
for (int i = 0; i < s.length(); i++)
{
if (s.at(i) == '0')
{
s.at(i) = '1';
}
else
{
s.at(i) = '0';
}
}
}
void binary::display()
{
cout << "The ones compliment is ";
for (int i = 0; i < s.length(); i++)
{
cout << s.at(i);
}
}
int main()
{
binary a;
a.input();
a.check_binary();
a.ones_compliment();
a.display();
return 0;
}
Comments
Post a Comment