Advertisement
SmellyBadger

Untitled

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