Advertisement
SmellyBadger

Untitled

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