Advertisement
SmellyBadger

Untitled

Nov 5th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8.  
  9. bool IsvalidNumForBase(int, int);
  10. int baseToDecimal(int, int);
  11. float someDecimals(float, float);
  12. void decimalToBase(int, int, int &, int &);
  13. int decimalOperation(int decinum1, int decinum2, char o);
  14.  
  15. // Variables
  16. int base = 0; // the base number the user will enter
  17. int num1, num2; // integers that must be less than the base
  18. int decinum1, decinum2, opResult; // the decimal value for nums 1 and 2
  19. bool IsvalidNum = true;
  20. char op;
  21. int resultWithNoTrailingZero, numTrailingZeros;
  22. int main()
  23. {
  24.  
  25. cout << "Enter the base: " << endl;
  26. cin >> base;
  27.  
  28. IsvalidNum = IsvalidNumForBase(num1, num2);
  29. if (IsvalidNumForBase == false)
  30. {
  31. return false;
  32. }
  33.  
  34. else
  35. {
  36. cout << "Enter the first number: " << endl;
  37. cout << "Enter the second number: " << endl;
  38. cout << endl;
  39.  
  40. decinum1 = baseToDecimal(base, num1);
  41. baseToDecimal(base, num1);
  42. cout << "Decimal value for first Number: " << decinum1 << endl;
  43.  
  44. decinum2 = baseToDecimal(base, num2);
  45. baseToDecimal(base, num2);
  46. cout << "Decimal value for the second number" << decinum2 << endl;
  47.  
  48. opResult = decimalOperation(decinum1, decinum2, op);
  49. decimalOperation(decinum1, decinum2, op)
  50.  
  51.  
  52. decimalToBase(opResult, base, resultWithNoTrailingZero, numTrailingZeros)
  53. {
  54. int remainder = 0;
  55. while (opResult > 0)
  56. {
  57. remainder = opResult % base;
  58. resultWithNoTrailingZero += resultWithNoTrailingZero * 10 + remainder;
  59. if (resultWithNoTrailingZero == 0)
  60. numTrailingZeros++;
  61. opResult = opResult / base;
  62. }
  63. return;
  64. }
  65. }
  66. }
  67. //params: (in, in)
  68. int baseToDecimal(int base, int dec)
  69. {
  70. int dec = 0;
  71. int digit = 0; // will hold each digit of the number
  72. int counter = 0;
  73. while (dec > 0)
  74. {
  75. digit = dec % 10;
  76. (digit * (pow(base, counter))) + dec
  77. dec = dec / 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 'o')
  99. {
  100. if (op == '+')
  101. {
  102. opResult = decinum1 + decinum2;
  103. }
  104. else if (op == '-')
  105. {
  106. opResult = decinum1 - decinum2;
  107. }
  108. else if (op == '*')
  109. {
  110. opResult = decinum1 * decinum2;
  111. }
  112. else
  113. {
  114. opResult = decinum1 / decinum2;
  115. }
  116. return;
  117. }
  118. // params: (in, in)
  119. bool IsvalidNumForBase(int num1, int num2)
  120. {
  121. if (base > num1 && num2)
  122. cout << base << endl;
  123. return true;
  124. else
  125. {
  126. cout << "The number is not valid for Base: " << base << endl;
  127. return false;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement