Обновить z-9.cpp

This commit is contained in:
oltnd
2024-11-16 00:05:34 +03:00
parent f555cb6f3a
commit 18e5de66f6

18
z-9.cpp
View File

@@ -3,6 +3,7 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
using namespace std; using namespace std;
int convertToDecimal(const string& number, int base) { int convertToDecimal(const string& number, int base) {
int decimalValue = 0; int decimalValue = 0;
int length = number.length(); int length = number.length();
@@ -14,10 +15,10 @@ int convertToDecimal(const string& number, int base) {
if (digit >= '0' && digit <= '9') { if (digit >= '0' && digit <= '9') {
value = digit - '0'; value = digit - '0';
} }
else if (digit >= 'A' && digit <= 'F') { else if (digit >= 'A' && digit <= 'Z') {
value = digit - 'A' + 10; value = digit - 'A' + 10;
} }
else if (digit >= 'a' && digit <= 'f') { else if (digit >= 'a' && digit <= 'z') {
value = digit - 'a' + 10; value = digit - 'a' + 10;
} }
else { else {
@@ -25,7 +26,7 @@ int convertToDecimal(const string& number, int base) {
} }
if (value >= base) { if (value >= base) {
throw invalid_argument("Цифра неверна для сс (осуждаю)"); throw invalid_argument("Цифра неверна для данной системы счисления");
} }
decimalValue += value * pow(base, i); decimalValue += value * pow(base, i);
@@ -43,7 +44,7 @@ string convertFromDecimal(int number, int base) {
result += (remainder + '0'); // Добавляем цифры 0-9 result += (remainder + '0'); // Добавляем цифры 0-9
} }
else { else {
result += (remainder - 10 + 'A'); // Добавляем буквы A-F result += (remainder - 10 + 'A'); // Добавляем буквы A-Z
} }
number /= base; number /= base;
} }
@@ -58,11 +59,16 @@ int main() {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
cout << "Введите число: "; cout << "Введите число: ";
cin >> inputNumber; cin >> inputNumber;
cout << "Введите основание исходной системы счисления (2-16): "; cout << "Введите основание исходной системы счисления (2-36): ";
cin >> oldBase; cin >> oldBase;
cout << "Введите основание новой системы счисления (2-16): "; cout << "Введите основание новой системы счисления (2-36): ";
cin >> newBase; cin >> newBase;
if (oldBase < 2 || oldBase > 36 || newBase < 2 || newBase > 36) {
cerr << "Основания должны быть в диапазоне от 2 до 36." << endl;
return 1;
}
try { try {
// Переводим число в десятичную систему // Переводим число в десятичную систему
int decimalValue = convertToDecimal(inputNumber, oldBase); int decimalValue = convertToDecimal(inputNumber, oldBase);