Advertisement
SmellyBadger

Untitled

Nov 5th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. bool IsvalidNumForBase(int, int, int);
  9. int baseToDecimal(int, int);
  10. void decimalToBase(int, int, int &, int &);
  11. int decimalOperation(int decinum1, int decinum2, char o);
  12.  
  13. // Variables
  14. int base = 0; // the base number the user will enter
  15. int num1, num2; // integers that must be less than the base
  16. int decinum1, decinum2; // the decimal value for nums 1 and 2
  17. bool IsvalidNum = true;
  18. char op;
  19. int resultWithNoTrailingZero, numTrailingZeros;
  20. int main()
  21. {
  22. int opResult;
  23. cout << "Enter the base: " << endl;
  24. cin >> base;
  25.  
  26.  
  27. cout << "Enter the first number: " << endl;
  28. cin >> num1;
  29.  
  30. cout << "Enter the second number: " << endl;
  31. cin >> num2;
  32.  
  33. cout << "List of operations: " << endl;
  34. cout << "+" << endl;
  35.  
  36. cout << "-" << endl;
  37.  
  38. cout << "*" << endl;
  39.  
  40. cout << "/" << endl;
  41.  
  42. cout << "Enter your choice of operation: " << endl;
  43. cin >> op;
  44.  
  45. cout << endl;
  46. cout << "OUTPUT: " << endl;
  47.  
  48. IsvalidNum = IsvalidNumForBase(num1, num2, base);
  49. IsvalidNumForBase(num1, num2, base);
  50.  
  51. if (base < 2 || base > 9)
  52. {
  53. cout << "The base is not valid. " << endl;
  54. }
  55.  
  56. else if (!IsvalidNum)
  57. {
  58. cout << "The number is not valid for base " << base << endl;
  59. }
  60. else if (op != '+' && op != '-' && op != '*' && op != '/')
  61. {
  62. cout << "The operation is not valid " << endl;
  63. }
  64. else
  65. {
  66. cout << "The number is valid for base " << base <<endl;
  67.  
  68. decinum1 = baseToDecimal(base, num1);
  69. baseToDecimal(base, num1);
  70. cout << "Decimal value of the first number = " << decinum1 << endl;
  71.  
  72. decinum2 = baseToDecimal(base, num2);
  73. baseToDecimal(base, num2);
  74. cout << "Decimal value of the second number = " << decinum2 << endl;
  75.  
  76.  
  77. if (decinum2 == 0 && op == '/')
  78. {
  79. cout << "Divide by zero error!!! " << endl;
  80. cout << "Check the symbol and the operands" << endl;
  81. }
  82. else
  83. {
  84. opResult = decimalOperation(decinum1, decinum2, op);
  85. cout << "Result in decimal = " << opResult << endl;
  86.  
  87. decimalToBase(opResult, base, resultWithNoTrailingZero, numTrailingZeros);
  88. cout << "Result in base " << base << " = " << resultWithNoTrailingZero;
  89.  
  90. while (numTrailingZeros > 0)
  91. cout << "0";
  92. numTrailingZeros--;
  93. }
  94. cout << endl;
  95. }
  96. return 0;
  97. }
  98. //params: (in, in)
  99. int baseToDecimal(int base, int num)
  100. {
  101. int dec = 0;
  102. int digit = 0; // will hold each digit of the number
  103. int counter = 0;
  104. while (num > 0)
  105. {
  106. digit = num % 10;
  107. dec = (digit * (pow(base, counter))) + dec;
  108. num = num / 10;
  109. counter++;
  110. }
  111.  
  112. return dec;
  113. }
  114. //params: (in, in, in/out, in/out)
  115. void decimalToBase(int opResult,int base,int& resultWithNoTrailingZero, int& numTrailingZeros)
  116. {
  117. int remainder = 0;
  118. while (opResult > 0)
  119. {
  120. remainder = opResult % base;
  121. resultWithNoTrailingZero = resultWithNoTrailingZero * 10 + remainder;
  122. if (resultWithNoTrailingZero == 0)
  123. numTrailingZeros++;
  124. opResult = opResult / base;
  125. }
  126. }
  127.  
  128. //params: (in, in, in)
  129. int decimalOperation(int decinum1, int decinum2, char op)
  130. {
  131. int opResult = 0;
  132. if (op == '+')
  133. {
  134. opResult = decinum1 + decinum2;
  135. }
  136. else if (op == '-')
  137. {
  138. opResult = decinum1 - decinum2;
  139. }
  140. else if (op == '*')
  141. {
  142. opResult = decinum1 * decinum2;
  143. }
  144. else
  145. {
  146. opResult = decinum1 / decinum2;
  147. }
  148. return opResult;
  149. }
  150. // params: (in, in)
  151. bool IsvalidNumForBase(int num1, int num2, int base)
  152. {
  153. int digit = 0;
  154. while (num1 > 0)
  155. {
  156. digit = num1 % 10;
  157. num1 /= 10;
  158.  
  159. if (digit >= base)
  160. {
  161. return false;
  162. }
  163. }
  164. while (num2 > 0)
  165. {
  166. digit = num2 % 10;
  167. num2 /= 10;
  168.  
  169. if (digit >= base)
  170. {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement