Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- bool IsvalidNumForBase(int, int, int);
- int baseToDecimal(int, int);
- void decimalToBase(int, int, int &, int &);
- int decimalOperation(int decinum1, int decinum2, char o);
- // Variables
- int base = 0; // the base number the user will enter
- int num1, num2; // integers that must be less than the base
- int decinum1, decinum2; // the decimal value for nums 1 and 2
- bool IsvalidNum = true;
- char op;
- int resultWithNoTrailingZero, numTrailingZeros;
- int main()
- {
- int opResult;
- cout << "Enter the base: " << endl;
- cin >> base;
- IsvalidNum = IsvalidNumForBase(num1, num2, base);
- if (IsvalidNumForBase == false)
- {
- return false;
- }
- else
- {
- cout << "Enter the first number: " << endl;
- cin >> num1;
- cout << "Enter the second number: " << endl;
- cin >> num2;
- cout << "List of operations" << endl;
- cout << "+" << endl;
- cin >> op;
- cout << "-" << endl;
- cin >> op;
- cout << "*" << endl;
- cin >> op;
- cout << "/" << endl;
- cin >> op;
- cout << endl;
- decinum1 = baseToDecimal(base, num1);
- baseToDecimal(base, num1);
- cout << "Decimal value for first Number: " << decinum1 << endl;
- decinum2 = baseToDecimal(base, num2);
- baseToDecimal(base, num2);
- cout << "Decimal value for the second number" << decinum2 << endl;
- if (decinum2 == 0 && op == '/')
- cout << "Error Message " << endl;
- else
- {
- opResult = decimalOperation(decinum1, decinum2, op);
- }
- }
- return 0;
- }
- //params: (in, in)
- int baseToDecimal(int base, int num)
- {
- int dec = 0;
- int digit = 0; // will hold each digit of the number
- int counter = 0;
- while (num > 0)
- {
- digit = num % 10;
- dec = (digit * (pow(base, counter))) + dec;
- num = num / 10;
- counter++;
- }
- return dec;
- }
- //params: (in, in, in/out, in/out)
- void decimalToBase(int opResult,int base,int& resultWithNoTrailingZero, int& numTrailingZeros)
- {
- int remainder = 0;
- while (opResult > 0)
- {
- remainder = opResult % base;
- resultWithNoTrailingZero = resultWithNoTrailingZero * 10 + remainder;
- if (resultWithNoTrailingZero == 0)
- numTrailingZeros++;
- opResult = opResult / base;
- }
- }
- //params: (in, in, in)
- int decimalOperation(int decinum1, int decinum2, char op)
- {
- int opResult = 0;
- if (op == '+')
- {
- opResult = decinum1 + decinum2;
- }
- else if (op == '-')
- {
- opResult = decinum1 - decinum2;
- }
- else if (op == '*')
- {
- opResult = decinum1 * decinum2;
- }
- else
- {
- opResult = decinum1 / decinum2;
- }
- return opResult;
- }
- // params: (in, in)
- bool IsvalidNumForBase(int num1, int num2, int base)
- {
- if (base > num1 && num2)
- {
- cout << base << endl;
- return true;
- }
- else
- {
- cout << "The number is not valid for Base: " << base << endl;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement