Advertisement
SmellyBadger

Untitled

Nov 5th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 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.  
  47.  
  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 decimalOperation(int decinum1, int decinum2, char op)
  89. {
  90. int opResult = 0;
  91. if (op == '+')
  92. {
  93. opResult = decinum1 + decinum2;
  94. }
  95. else if (op == '-')
  96. {
  97. opResult = decinum1 - decinum2;
  98. }
  99. else if (op == '*')
  100. {
  101. opResult = decinum1 * decinum2;
  102. }
  103. else
  104. {
  105. opResult = decinum1 / decinum2;
  106. }
  107. return opResult;
  108. }
  109. // params: (in, in)
  110. bool IsvalidNumForBase(int num1, int num2, int base)
  111. {
  112. if (base > num1 && num2)
  113. {
  114. cout << base << endl;
  115. return true;
  116. }
  117. else
  118. {
  119. cout << "The number is not valid for Base: " << base << endl;
  120. return false;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement