Advertisement
SmellyBadger

Untitled

Nov 5th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 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);
  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, opResult; // the decimal value for nums 1 and 2
  17. bool IsvalidNum = true;
  18. char op;
  19. int resultWithNoTrailingZero, numTrailingZeros;
  20. int main()
  21. {
  22.  
  23. cout << "Enter the base: " << endl;
  24. cin >> base;
  25.  
  26. IsvalidNum = IsvalidNumForBase(num1, num2);
  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.  
  49. }
  50. return 0;
  51. }
  52. //params: (in, in)
  53. int baseToDecimal(int base, int num)
  54. {
  55. int dec = 0;
  56. int digit = 0; // will hold each digit of the number
  57. int counter = 0;
  58. while (num > 0)
  59. {
  60. digit = num % 10;
  61. digit = (digit * (pow(base, counter))) + dec;
  62. num = num / 10;
  63. counter++;
  64. }
  65.  
  66. return dec;
  67. }
  68. //params: (in, in, in/out, in/out)
  69. void decimalToBase(int opResult,int base,int& resultWithNoTrailingZero, int& numTrailingZeros)
  70. {
  71. int remainder = 0;
  72. while (opResult > 0)
  73. {
  74. remainder = opResult % base;
  75. resultWithNoTrailingZero += resultWithNoTrailingZero * 10 + remainder;
  76. if (resultWithNoTrailingZero == 0)
  77. numTrailingZeros++;
  78. opResult = opResult / base;
  79. }
  80. }
  81.  
  82. //params: (in, in, in)
  83. opResult = decimalOperation(decinum1, decinum2, op);
  84. int decimalOperation(int decinum1, int decinum2, char op)
  85. {
  86.  
  87. if (op == '+')
  88. {
  89. opResult = decinum1 + decinum2;
  90. }
  91. else if (op == '-')
  92. {
  93. opResult = decinum1 - decinum2;
  94. }
  95. else if (op == '*')
  96. {
  97. opResult = decinum1 * decinum2;
  98. }
  99. else
  100. {
  101. opResult = decinum1 / decinum2;
  102. }
  103. return opResult;
  104. }
  105. // params: (in, in)
  106. bool IsvalidNumForBase(int num1, int num2)
  107. {
  108. if (base > num1 && num2)
  109. {
  110. cout << base << endl;
  111. return true;
  112. }
  113. else
  114. {
  115. cout << "The number is not valid for Base: " << base << endl;
  116. return false;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement