#include using namespace std; bool isValidInBase(string number, int base); string addNumbersInBase(string num1, string num2, int base); string subtractNumbersInBase(string num1, string num2, int base); void getInputsForBase(string &num1, string &num2, int base); int main (int argc, char * const argv[]) { int base; string number1, number2; int command = 0; cout << "\n\nNew Horizons Governors School Team B\n"; cout << "Enter a base (0 < base < 10): "; cin >> base; while (base <= 0 || base > 10) { cout << "Invalid base; enter a new one\n"; cout << "Enter a base (0 < base < 10): "; cin >> base; } do { cout << "Enter the number representing your command:\n"; cout << "1. Add two base " << base << " numbers\n"; cout << "2. Subtract two base " << base << " numbers\n"; cout << "3. Exit\n"; cout << "> "; cin >> command; if (command == 1) { getInputsForBase(number1, number2, base); cout << "The sum of " << number1 << " and " << number2 << " is " << addNumbersInBase(number1, number2, base); cout << " in base " << base << endl; } else if (command == 2) { getInputsForBase(number1, number2, base); cout << "The difference of " << number1 << " and " << number2 << " is " << subtractNumbersInBase(number1, number2, base); cout << " in base " << base << endl; } else if (command == 3); else cout << "Invalid command\n"; } while (command != 3); return 0; } bool isValidInBase(string number, int base) { for (int i = 0; i < number.length(); i++) if (number[i] < '0' || number[i] > base + '0' - 1) return false; return true; } string addNumbersInBase(string num1, string num2, int base) { int n1Length = num1.length(), n2Length = num2.length(); int n1, n2; int resultLength = (n1Length > n2Length ? n1Length : n2Length) + 1; int result, resultArray[resultLength]; n1 = atoi(num1.c_str()); n2 = atoi(num2.c_str()); result = n1 + n2; int i; for (i = resultLength - 1; i >= 0; i--) { resultArray[i] = result % 10; result /= 10; } for (i = resultLength - 1; i > 0; i--) { while (resultArray[i] > base - 1) { resultArray[i] -= base; resultArray[i - 1]++; } } string stringResult = ""; for (int i = 0; i < resultLength; i++) stringResult += resultArray[i] + '0'; return stringResult; } string subtractNumbersInBase(string num1, string num2, int base) { int n1Length = num1.length(), n2Length = num2.length(); int n1 = atoi(num1.c_str()), n2 = atoi(num2.c_str()); int resultLength = (n1Length > n2Length ? n1Length : n2Length); int resultArray[resultLength]; int n1Array[n1Length], n2Array[n2Length]; bool negative = n1 < n2; //need to swap them if (negative) { int temp = n1; n1 = n2; n2 = temp; temp = n1Length; n1Length = n2Length; n2Length = temp; } for (int i = 0; i < resultLength; i++) { resultArray[i] = 0; } for (int i = resultLength - 1; i >= 0; i--) { if (resultLength - i > n1Length) n1Array[i] = 0; else n1Array[i] = num1[i] - '0'; if (resultLength - i > n2Length) n2Array[i] = 0; else n2Array[i] = num2[i] - '0'; } for (int i = resultLength - 1; i >= 0; i--) { if (n1Array[i] - n2Array[i] < 0) { n1Array[i - 1]--; n1Array[i] += base; } resultArray[i] = n1Array[i] - n2Array[i]; } string stringResult = ""; for (int i = 0; i < resultLength; i++) stringResult += resultArray[i] + '0'; if (negative) stringResult.insert(0, "-"); return stringResult; } void getInputsForBase(string &num1, string &num2, int base) { cout << "Enter the first number: "; cin >> num1; while (!isValidInBase(num1, base)) { cout << "Not valid, try again: "; cin >> num1; } cout << "Enter the second number: "; cin >> num2; while (!isValidInBase(num2, base)) { cout << "Not valid, try again: "; cin >> num2; } }