Advertisement
SmellyBadger

Untitled

Nov 5th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 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. IsvalidNum = IsvalidNumForBase(num1, num2, base);
  27. if (IsvalidNumForBase == false)
  28. {
  29. return false;
  30. }
  31.  
  32. else
  33. {
  34. cout << "Enter the first number: " << endl;
  35. cout << "Enter the second number: " << endl;
  36. cout << endl;
  37.  
  38. decinum1 = baseToDecimal(base, num1);
  39. baseToDecimal(base, num1);
  40. cout << "Decimal value for first Number: " << decinum1 << endl;
  41.  
  42. decinum2 = baseToDecimal(base, num2);
  43. baseToDecimal(base, num2);
  44. cout << "Decimal value for the second number" << decinum2 << endl;
  45.  
  46. opResult = decimalOperation(decinum1, decinum2, op);
  47. decimalOperation(decinum1, decinum2, op);
  48. if (decinum2 == 0 && op == '/')
  49. cout << "Error Message " << endl;
  50. else
  51. {
  52. opResult = decimalOperation(decinum1, decinum2, op);
  53. }
  54. }
  55. return 0;
  56. }
  57. //params: (in, in)
  58. int baseToDecimal(int base, int num)
  59. {
  60. int dec = 0;
  61. int digit = 0; // will hold each digit of the number
  62. int counter = 0;
  63. while (num > 0)
  64. {
  65. digit = num % 10;
  66. dec = (digit * (pow(base, counter))) + dec;
  67. num = num / 10;
  68. counter++;
  69. }
  70.  
  71. return dec;
  72. }
  73. //params: (in, in, in/out, in/out)
  74. void decimalToBase(int opResult,int base,int& resultWithNoTrailingZero, int& numTrailingZeros)
  75. {
  76. int remainder = 0;
  77. while (opResult > 0)
  78. {
  79. remainder = opResult % base;
  80. resultWithNoTrailingZero = resultWithNoTrailingZero * 10 + remainder;
  81. if (resultWithNoTrailingZero == 0)
  82. numTrailingZeros++;
  83. opResult = opResult / base;
  84. }
  85. }
  86.  
  87. //params: (in, in, in)
  88. int opResult = decimalOperation(decinum1, decinum2, op);
  89. int decimalOperation(int decinum1, int decinum2, char op)
  90. {
  91. int opResult = 0;
  92. if (op == '+')
  93. {
  94. opResult = decinum1 + decinum2;
  95. }
  96. else if (op == '-')
  97. {
  98. opResult = decinum1 - decinum2;
  99. }
  100. else if (op == '*')
  101. {
  102. opResult = decinum1 * decinum2;
  103. }
  104. else
  105. {
  106. opResult = decinum1 / decinum2;
  107. }
  108. return opResult;
  109. }
  110. // params: (in, in)
  111. bool IsvalidNumForBase(int num1, int num2)
  112. {
  113. if (base > num1 && num2)
  114. {
  115. cout << base << endl;
  116. return true;
  117. }
  118. else
  119. {
  120. cout << "The number is not valid for Base: " << base << endl;
  121. return false;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement