Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define cin(v) \
- for (auto &i : v) \
- cin >> i;
- #define cout(v) \
- for (auto &i : v) \
- cout << i << " ";
- #define br cout << '\n';
- #define ll long long
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- void Warding()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- // bool is_prime(int n)
- // {
- // if (n <= 1)
- // return false;
- // for (int i = 2; i <= sqrt(n); i++)
- // if (n % i == 0)
- // return false;
- // return true;
- // }
- void solve()
- {
- int d, test;
- do
- {
- stringstream ss;
- string s, s2;
- cout << "Enter 1 for 1's complement\n";
- cout << "Enter 2 for 2's complement\n";
- cout << "Enter 3 for 9's complement\n";
- cout << "Enter 4 for 10's complement\n";
- cout << "Enter 5 to convert fraction number to binary\n";
- cout << "Enter 0 to exit\n\n";
- cout << "Choose what you want: ";
- cin >> d;
- switch (d)
- {
- case 1:
- cout << "Enter the number you want: ";
- cin >> s;
- cout << "The 1's complement of " << s << " is: ";
- for (int i = 0; i < s.length(); i++)
- s[i] = (s[i] == '1' ? '0' : '1');
- cout << s << "\n\n";
- break;
- case 2:
- cout << "Enter the number you want: ";
- cin >> s;
- cout << "The 2's complement of " << s << " is: ";
- for (int i = s.length() - 1; i >= 0; i--)
- {
- if (s[i] == '1')
- {
- for (int j = 0; j < i; j++)
- s[j] = (s[j] == '1' ? '0' : '1');
- break;
- }
- }
- cout << s << "\n\n";
- break;
- case 3:
- cout << "Enter the number you want: ";
- cin >> s;
- cout << "The 9's Complement of " << s << " is: ";
- for (int i = 0; i < s.length(); i++)
- {
- cout << 9 - (s[i] - '0');
- }
- cout << "\n\n";
- break;
- case 4:
- int holder;
- cout << "Enter the number you want: ";
- cin >> s;
- cout << "The 10's Complement of " << s << " is: ";
- for (int i = 0; i < s.length(); i++)
- {
- s2 += ('9' - (s[i] - '0'));
- }
- ss << s2;
- ss >> holder;
- cout << ++holder << "\n\n";
- break;
- case 5:
- double n;
- cout << "Enter the number you want: ";
- s += "0.";
- cin >> n;
- cout << n << " in binary is: ";
- while (n > 0)
- {
- n *= 2;
- (n >= 1 ? s += '1' : s += '0');
- if (n >= 1)
- n -= 1;
- }
- cout << s << "\n\n";
- break;
- case 0:
- break;
- default:
- cout << "Enter value between 0-5 only please: \n\n";
- }
- cout << "Enter 1 to try again and 0 to exit: ";
- cin >> test;
- system("CLS");
- } while (test != 0);
- }
- int main()
- {
- // Warding();
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment