IMohammedNasr

Untitled

Apr 9th, 2022 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define cin(v) \
  4. for (auto &i : v) \
  5. cin >> i;
  6. #define cout(v) \
  7. for (auto &i : v) \
  8. cout << i << " ";
  9. #define br cout << '\n';
  10. #define ll long long
  11. #define all(v) v.begin(), v.end()
  12. #define rall(v) v.rbegin(), v.rend()
  13.  
  14. void Warding()
  15. {
  16. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  17. #ifndef ONLINE_JUDGE
  18. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  19. #endif
  20. }
  21. // bool is_prime(int n)
  22. // {
  23. // if (n <= 1)
  24. // return false;
  25. // for (int i = 2; i <= sqrt(n); i++)
  26. // if (n % i == 0)
  27. // return false;
  28. // return true;
  29. // }
  30.  
  31. void solve()
  32. {
  33. int d, test;
  34. do
  35. {
  36. stringstream ss;
  37. string s, s2;
  38. cout << "Enter 1 for 1's complement\n";
  39. cout << "Enter 2 for 2's complement\n";
  40. cout << "Enter 3 for 9's complement\n";
  41. cout << "Enter 4 for 10's complement\n";
  42. cout << "Enter 5 to convert fraction number to binary\n";
  43. cout << "Enter 0 to exit\n\n";
  44. cout << "Choose what you want: ";
  45. cin >> d;
  46. switch (d)
  47. {
  48. case 1:
  49. cout << "Enter the number you want: ";
  50. cin >> s;
  51. cout << "The 1's complement of " << s << " is: ";
  52. for (int i = 0; i < s.length(); i++)
  53. s[i] = (s[i] == '1' ? '0' : '1');
  54. cout << s << "\n\n";
  55. break;
  56. case 2:
  57. cout << "Enter the number you want: ";
  58. cin >> s;
  59. cout << "The 2's complement of " << s << " is: ";
  60. for (int i = s.length() - 1; i >= 0; i--)
  61. {
  62. if (s[i] == '1')
  63. {
  64. for (int j = 0; j < i; j++)
  65. s[j] = (s[j] == '1' ? '0' : '1');
  66. break;
  67. }
  68. }
  69. cout << s << "\n\n";
  70. break;
  71. case 3:
  72. cout << "Enter the number you want: ";
  73. cin >> s;
  74. cout << "The 9's Complement of " << s << " is: ";
  75. for (int i = 0; i < s.length(); i++)
  76. {
  77. cout << 9 - (s[i] - '0');
  78. }
  79. cout << "\n\n";
  80. break;
  81. case 4:
  82. int holder;
  83. cout << "Enter the number you want: ";
  84. cin >> s;
  85. cout << "The 10's Complement of " << s << " is: ";
  86. for (int i = 0; i < s.length(); i++)
  87. {
  88. s2 += ('9' - (s[i] - '0'));
  89. }
  90. ss << s2;
  91. ss >> holder;
  92. cout << ++holder << "\n\n";
  93. break;
  94. case 5:
  95. double n;
  96. cout << "Enter the number you want: ";
  97. s += "0.";
  98. cin >> n;
  99. cout << n << " in binary is: ";
  100. while (n > 0)
  101. {
  102. n *= 2;
  103. (n >= 1 ? s += '1' : s += '0');
  104. if (n >= 1)
  105. n -= 1;
  106. }
  107. cout << s << "\n\n";
  108. break;
  109. case 0:
  110. break;
  111. default:
  112. cout << "Enter value between 0-5 only please: \n\n";
  113. }
  114. cout << "Enter 1 to try again and 0 to exit: ";
  115. cin >> test;
  116. system("CLS");
  117. } while (test != 0);
  118. }
  119.  
  120. int main()
  121. {
  122. // Warding();
  123. int t = 1;
  124. // cin >> t;
  125. while (t--)
  126. {
  127. solve();
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment