Реализовать калькулятор систем счисления в С++ Реализовать калькулятор систем счисления На вход подается число, основание системы счисления, действие. Если это арифметическое действие, то вводится второе число и его основание системы счисления (в общем случае отличное от основания первого числа). Основание системы счисления результата Среди действий реализовать основные: 1) Перевод системы счисления (#) 2) Сложение чисел в различных СС (+) 3) Вычитание чисел в различных СС (-) 4) Умножение чисел в различных СС (*) 5) Деление чисел в различных СС (/) Дополнительные: 6-10) Функции на ваше усмотрение. (в скобках указан пример значка действия для считывания) Пример работы программы: Ввод: 1101 2 + 2 3 10 Вывод: 15
int baseToInt(char c) { if (c >= '0' && c <= '9') { return c - '0'; } else if (c >= 'A' && c <= 'Z') { return c - 'A' + 10; } else { return -1; } }
char intToBase(int n) { if (n >= 0 && n <= 9) { return '0' + n; } else if (n >= 10 && n <= 35) { return 'A' + n - 10; } else { return '\0'; } }
int toDecimal(std::string num, int base) { int result = 0; for (int i = num.length() - 1, j = 0; i >= 0; i--, j++) { int digit = baseToInt(num[i]); result += digit * pow(base, j); } return result; }
std::string toBase(int num, int base) { std::string result = ""; while (num > 0) { int remainder = num % base; result = intToBase(remainder) + result; num /= base; } return result; }
int main() { std::string num1; int base1, base2; char operation;
std::cout << "Enter the first number: "; std::cin >> num1; std::cout << "Enter the base of the first number: "; std::cin >> base1; std::cout << "Enter the operation (+, -, *, /) or # for conversion: "; std::cin >> operation; if (operation == '#') { std::cout << "Enter the base you want to convert to: "; std::cin >> base2; int decimalNum = toDecimal(num1, base1); std::string result = toBase(decimalNum, base2); std::cout << "Result: " << result << std::endl; } else { std::string num2; std::cout << "Enter the second number: "; std::cin >> num2; std::cout << "Enter the base of the second number: "; std::cin >> base2; int numDecimal1 = toDecimal(num1, base1); int numDecimal2 = toDecimal(num2, base2); int resultDecimal; std::string result; switch(operation) { case '+': resultDecimal = numDecimal1 + numDecimal2; result = toBase(resultDecimal, base1); break; case '-': resultDecimal = numDecimal1 - numDecimal2; result = toBase(resultDecimal, base1); break; case '*': resultDecimal = numDecimal1 * numDecimal2; result = toBase(resultDecimal, base1); break; case '/': resultDecimal = numDecimal1 / numDecimal2; result = toBase(resultDecimal, base1); break; default: std::cout << "Invalid operation!" << std::endl; return 1; } std::cout << "Result: " << result << std::endl; } return 0;
int baseToInt(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'Z') {
return c - 'A' + 10;
} else {
return -1;
}
}
char intToBase(int n) {
if (n >= 0 && n <= 9) {
return '0' + n;
} else if (n >= 10 && n <= 35) {
return 'A' + n - 10;
} else {
return '\0';
}
}
int toDecimal(std::string num, int base) {
int result = 0;
for (int i = num.length() - 1, j = 0; i >= 0; i--, j++) {
int digit = baseToInt(num[i]);
result += digit * pow(base, j);
}
return result;
}
std::string toBase(int num, int base) {
std::string result = "";
while (num > 0) {
int remainder = num % base;
result = intToBase(remainder) + result;
num /= base;
}
return result;
}
int main() {
std::cout << "Enter the first number: ";std::string num1;
int base1, base2;
char operation;
std::cin >> num1;
std::cout << "Enter the base of the first number: ";
std::cin >> base1;
std::cout << "Enter the operation (+, -, *, /) or # for conversion: ";
std::cin >> operation;
if (operation == '#') {
std::cout << "Enter the base you want to convert to: ";
std::cin >> base2;
int decimalNum = toDecimal(num1, base1);
std::string result = toBase(decimalNum, base2);
std::cout << "Result: " << result << std::endl;
} else {
std::string num2;
std::cout << "Enter the second number: ";
std::cin >> num2;
std::cout << "Enter the base of the second number: ";
std::cin >> base2;
int numDecimal1 = toDecimal(num1, base1);
int numDecimal2 = toDecimal(num2, base2);
int resultDecimal;
std::string result;
switch(operation) {
case '+':
resultDecimal = numDecimal1 + numDecimal2;
result = toBase(resultDecimal, base1);
break;
case '-':
resultDecimal = numDecimal1 - numDecimal2;
result = toBase(resultDecimal, base1);
break;
case '*':
resultDecimal = numDecimal1 * numDecimal2;
result = toBase(resultDecimal, base1);
break;
case '/':
resultDecimal = numDecimal1 / numDecimal2;
result = toBase(resultDecimal, base1);
break;
default:
std::cout << "Invalid operation!" << std::endl;
return 1;
}
std::cout << "Result: " << result << std::endl;
}
return 0;
}