Advertisement
SmellyBadger

Untitled

Nov 5th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 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. bool IsvalidNumForBase(int num1, int num2, int base)
  49. {
  50. int digit = 0;
  51. while (num1 > 0)
  52. {
  53. digit = num1 % 10;
  54. num1 /= 10;
  55.  
  56. if (digit >= base)
  57. {
  58. return false;
  59. }
  60. }
  61. while (num2 > 0)
  62. {
  63. digit = num2 % 10;
  64. num2 /= 10;
  65.  
  66. if (digit >= base)
  67. {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. if (base < 2 || base > 9)
  74. {
  75. cout << "The base is not valid. " << endl;
  76. }
  77.  
  78. else if (!IsvalidNum)
  79. {
  80. cout << "The number is not valid for base " << base << endl;
  81. }
  82. else if (op != '+' && op != '-' && op != '*' && op != '/')
  83. {
  84. cout << "The operation is not valid " << endl;
  85. }
  86. else
  87. {
  88. cout << "The number is valid for base " << endl;
  89.  
  90. decinum1 = baseToDecimal(base, num1);
  91. baseToDecimal(base, num1);
  92. cout << "Decimal value for first Number = " << decinum1 << endl;
  93.  
  94. decinum2 = baseToDecimal(base, num2);
  95. baseToDecimal(base, num2);
  96. cout << "Decimal value for the second number = " << decinum2 << endl;
  97.  
  98.  
  99. if (decinum2 == 0 && op == '/')
  100. {
  101. cout << "Divide by zero error!!! " << endl;
  102. cout << "Check the symbol and the operands" << endl;
  103. }
  104. else
  105. {
  106. opResult = decimalOperation(decinum1, decinum2, op);
  107. cout << "Result in decimal = " << opResult << endl;
  108. }
  109. }
  110. cin.get();
  111. cin.get();
  112. return 0;
  113. }
  114. //params: (in, in)
  115. int baseToDecimal(int base, int num)
  116. {
  117. int dec = 0;
  118. int digit = 0; // will hold each digit of the number
  119. int counter = 0;
  120. while (num > 0)
  121. {
  122. digit = num % 10;
  123. dec = (digit * (pow(base, counter))) + dec;
  124. num = num / 10;
  125. counter++;
  126. }
  127.  
  128. return dec;
  129. }
  130. //params: (in, in, in/out, in/out)
  131. void decimalToBase(int opResult,int base,int& resultWithNoTrailingZero, int& numTrailingZeros)
  132. {
  133. int remainder = 0;
  134. while (opResult > 0)
  135. {
  136. remainder = opResult % base;
  137. resultWithNoTrailingZero = resultWithNoTrailingZero * 10 + remainder;
  138. if (resultWithNoTrailingZero == 0)
  139. numTrailingZeros++;
  140. opResult = opResult / base;
  141. }
  142. }
  143.  
  144. //params: (in, in, in)
  145. int decimalOperation(int decinum1, int decinum2, char op)
  146. {
  147. int opResult = 0;
  148. if (op == '+')
  149. {
  150. opResult = decinum1 + decinum2;
  151. }
  152. else if (op == '-')
  153. {
  154. opResult = decinum1 - decinum2;
  155. }
  156. else if (op == '*')
  157. {
  158. opResult = decinum1 * decinum2;
  159. }
  160. else
  161. {
  162. opResult = decinum1 / decinum2;
  163. }
  164. return opResult;
  165. }
  166. // params: (in, in)
  167. bool IsvalidNumForBase(int num1, int num2, int base)
  168. {
  169. if (base > num1 && num2)
  170. {
  171. cout << base << endl;
  172. return true;
  173. }
  174. else
  175. {
  176. cout << "The number is not valid for Base: " << base << endl;
  177. return false;
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement