#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// Funkcja do konwersji liczby z systemu dziesiętnego na system o podstawie base
string decimalToBase(int decimal, int base) {
if (decimal == 0) return "0";
string result;
bool isNegative = decimal < 0;
decimal = abs(decimal);
while (decimal > 0) {
int remainder = decimal % base;
if (remainder >= 10) {
result = char(remainder - 10 + 'A') + result; // Konwersja na znaki A-Z
} else {
result = char(remainder + '0') + result; // Konwersja na znaki 0-9
}
decimal /= base;
}
if (isNegative) {
result = '-' + result;
}
return result;
}
// Funkcja do konwersji liczby z systemu o podstawie base do systemu dziesiętnego
int baseToDecimal(const string &number, int base) {
int result = 0;
int power = 0;
bool isNegative = (number[0] == '-');
for (int i = number.size() - 1; i >= (isNegative ? 1 : 0); --i) {
char digit = number[i];
int value;
if (digit >= '0' && digit <= '9') {
value = digit - '0';
} else if (digit >= 'A' && digit <= 'Z') {
value = digit - 'A' + 10;
} else {
throw invalid_argument("Invalid character in number.");
}
if (value >= base) {
throw invalid_argument("Digit exceeds base.");
}
result += value * pow(base, power++);
}
return isNegative ? -result : result;
}
int main() {
int choice;
cout << "Wybierz opcję:\n1. Dziesiętny na inny system\n2. Inny system na dziesiętny\n";
cin >> choice;
if (choice == 1) {
int decimal, base;
cout << "Podaj liczbę dziesiętną: ";
cin >> decimal;
cout << "Podaj podstawę (2-36): ";
cin >> base;
if (base < 2 || base > 36) {
cout << "Podstawa musi być w przedziale 2-36." << endl;
return 1;
}
string result = decimalToBase(decimal, base);
cout << "Liczba w systemie o podstawie " << base << ": " << result << endl;
} else if (choice == 2) {
string number;
int base;
cout << "Podaj liczbę: ";
cin >> number;
cout << "Podaj podstawę (2-36): ";
cin >> base;
if (base < 2 || base > 36) {
cout << "Podstawa musi być w przedziale 2-36." << endl;
return 1;
}
try {
int result = baseToDecimal(number, base);
cout << "Liczba w systemie dziesiętnym: " << result << endl;
} catch (const invalid_argument &e) {
cout << "Błąd: " << e.what() << endl;
return 1;
}
} else {
cout << "Niepoprawny wybór." << endl;
}
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: