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