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

This commit is contained in:
oltnd
2024-11-02 17:27:11 +03:00
parent aedbefafeb
commit 67e2138aa8

53
z-9.cpp
View File

@@ -2,8 +2,8 @@
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
using namespace std;
int convertToDecimal(const std::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();
@@ -13,60 +13,65 @@ int convertToDecimal(const std::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 <= 'F') {
value = digit - 'A' + 10; value = digit - 'A' + 10;
} else if (digit >= 'a' && digit <= 'f') { }
else if (digit >= 'a' && digit <= 'f') {
value = digit - 'a' + 10; value = digit - 'a' + 10;
} else { }
throw std::invalid_argument("Invalid digit in the number."); else {
throw invalid_argument("Недействительная цифра в числе");
} }
if (value >= base) { if (value >= base) {
throw std::invalid_argument("Digit is not valid for the base."); throw invalid_argument("Цифра неверна для сс (осуждаю)");
} }
decimalValue += value * std::pow(base, i); decimalValue += value * pow(base, i);
} }
return decimalValue; return decimalValue;
} }
std::string convertFromDecimal(int number, int base) { string convertFromDecimal(int number, int base) {
std::string result; string result;
while (number > 0) { while (number > 0) {
int remainder = number % base; int remainder = number % base;
if (remainder < 10) { if (remainder < 10) {
result += (remainder + '0'); // Добавляем цифры 0-9 result += (remainder + '0'); // Добавляем цифры 0-9
} else { }
else {
result += (remainder - 10 + 'A'); // Добавляем буквы A-F result += (remainder - 10 + 'A'); // Добавляем буквы A-F
} }
number /= base; number /= base;
} }
std::reverse(result.begin(), result.end()); // Переворачиваем результат reverse(result.begin(), result.end()); // Переворачиваем результат
return result.empty() ? "0" : result; // Если результат пустой, возвращаем "0" return result.empty() ? "0" : result; // Если результат пустой, возвращаем "0"
} }
int main() { int main() {
std::string inputNumber; string inputNumber;
int oldBase, newBase; int oldBase, newBase;
setlocale(LC_ALL, "");
std::cout << "Введите число: "; cout << "Введите число: ";
std::cin >> inputNumber; cin >> inputNumber;
std::cout << "Введите основание исходной системы счисления (2-16): "; cout << "Введите основание исходной системы счисления (2-16): ";
std::cin >> oldBase; cin >> oldBase;
std::cout << "Введите основание новой системы счисления (2-16): "; cout << "Введите основание новой системы счисления (2-16): ";
std::cin >> newBase; cin >> newBase;
try { try {
// Переводим число в десятичную систему // Переводим число в десятичную систему
int decimalValue = convertToDecimal(inputNumber, oldBase); int decimalValue = convertToDecimal(inputNumber, oldBase);
// Переводим число из десятичной системы в новую систему счисления // Переводим число из десятичной системы в новую систему счисления
std::string result = convertFromDecimal(decimalValue, newBase); string result = convertFromDecimal(decimalValue, newBase);
std::cout << "Результат: " << result << std::endl; cout << "Результат: " << result << endl;
} catch (const std::invalid_argument& e) { }
std::cerr << "Ошибка: " << e.what() << std::endl; catch (const invalid_argument& e) {
cerr << "Ошибка: " << e.what() << endl;
} }
return 0; return 0;