Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <Windows.h>
- std::string changeTheNumber(int inSys, int outSys, std::string inNumber);
- int main()
- {
- /* while (true) {
- int inputSystem = 0; // System dla liczby wejściowej
- int outputSystem = 0; // System dla liczby wyjściowej
- std::string inputNumber = ""; // Liczba wejściowa
- char allowedChars[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; // Dozwolone znaki
- do {
- system("cls");
- std::cout << "Podaj system startowy: ";
- std::cin >> inputSystem;
- } while (inputSystem <= 2 && inputSystem >= 16); // Wprowadzenie systemu wejściowego z ograniczeniem (od 2 do 16)
- do {
- system("cls");
- std::cout << "Podaj system docelowy: ";
- std::cin >> outputSystem;
- } while (outputSystem <= 2 && outputSystem >= 16); // Wprowadzenie systemu docelowego z ograniczeniem (od 2 do 16)
- system("cls");
- std::cout << "System startowy: " << inputSystem << std::endl;
- std::cout << "System docelowy: " << outputSystem << std::endl;
- std::cout << "Podaj liczbe do zmiany: ";
- std::cin >> inputNumber; // Wprowadzenie liczby wejściowej
- bool inputNumberIsCorrect = true;
- for (int i = 0; i < inputNumber.length(); i++) { // Sprawdzanie w pętli czy liczba zawiera znaki dozwolone w podanym systemie liczbowym
- for (int z = 0; z < inputSystem; z++) {
- if (inputNumber[i] == allowedChars[z])
- break;
- else if (inputNumber[i] != allowedChars[z] && z == inputSystem - 1) {
- inputNumberIsCorrect = false;
- }
- }
- }
- if (!inputNumberIsCorrect) { // Error w przypadku niespełnienia warunków systemu liczbowego
- std::cout << "Twoja liczba jest niedobra";
- Sleep(1000);
- break;
- }
- std::cout << std::endl << changeTheNumber(inputSystem, outputSystem, inputNumber);
- break;
- }
- */
- std::cout << changeTheNumber(10, 16, "1803");
- std::cin.get();
- std::cin.get();
- return 1;
- }
- std::string changeTheNumber(int inSys, int outSys, std::string inNumber) {
- int decimalInputNumber = 0;
- std::string outNumber = "";
- for (int i = 0; i < inNumber.length(); i++) {
- switch (inNumber[i]) {
- case '0': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 0; break;
- case '1': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 1; break;
- case '2': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 2; break;
- case '3': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 3; break;
- case '4': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 4; break;
- case '5': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 5; break;
- case '6': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 6; break;
- case '7': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 7; break;
- case '8': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 8; break;
- case '9': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 9; break;
- case 'A': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 10; break;
- case 'B': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 11; break;
- case 'C': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 12; break;
- case 'D': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 13; break;
- case 'E': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 14; break;
- case 'F': decimalInputNumber += (int)pow(inSys, inNumber.length() - i - 1) * 15; break;
- }
- }
- int amountOfNumbers = 0;
- for (int z = 0; ; z++) { // Szukanie najwyższej potęgi
- if (pow(outSys, z) > decimalInputNumber) {
- amountOfNumbers = z - 1;
- break;
- }
- }
- while (true) {
- int maxStand = 0;
- for (int z = 1; z < outSys ; z++) { // Szukanie najwyższego mnożnika
- if (pow(outSys, amountOfNumbers) * z > decimalInputNumber) {
- maxStand = z - 1;
- break;
- }
- }
- decimalInputNumber -= (pow(outSys, amountOfNumbers) * maxStand);
- switch (maxStand) {
- case 0: outNumber += '0'; break;
- case 1: outNumber += '1'; break;
- case 2: outNumber += '2'; break;
- case 3: outNumber += '3'; break;
- case 4: outNumber += '4'; break;
- case 5: outNumber += '5'; break;
- case 6: outNumber += '6'; break;
- case 7: outNumber += '7'; break;
- case 8: outNumber += '8'; break;
- case 9: outNumber += '9'; break;
- case 10: outNumber += 'A'; break;
- case 11: outNumber += 'B'; break;
- case 12: outNumber += 'C'; break;
- case 13: outNumber += 'D'; break;
- case 14: outNumber += 'E'; break;
- case 15: outNumber += 'F'; break;
- }
- if (decimalInputNumber == 0 && amountOfNumbers == 0)
- break;
- else
- amountOfNumbers--;
- }
- return outNumber;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement