Program do dzielenia z reszta
Guest 143 5th Nov, 2024
#include <iostream>
#include <sstream>
#include <iomanip>
std::pair<std::string, std::string> hex_division(const std::string& dividend_hex, const std::string& divisor_hex) {
int dividend, divisor;
std::stringstream ss;
ss << std::hex << dividend_hex;
ss >> dividend;
ss.clear();
ss << std::hex << divisor_hex;
ss >> divisor;
int quotient = dividend / divisor;
int remainder = dividend % divisor;
std::stringstream quotient_ss, remainder_ss;
quotient_ss << std::hex << std::uppercase << quotient;
remainder_ss << std::hex << std::uppercase << remainder;
return {quotient_ss.str(), remainder_ss.str()};
}
int main() {
std::string dividend_hex, divisor_hex;
while (true) {
std::cout << "Podaj liczbę do dzielenia (w systemie szesnastkowym, wpisz 'exit' aby zakończyć): ";
std::cin >> dividend_hex;
// Sprawdzenie, czy użytkownik chce zakończyć program
if (dividend_hex == "exit") {
std::cout << "Zakończenie programu." << std::endl;
break;
}
std::cout << "Podaj dzielnik (w systemie szesnastkowym): ";
std::cin >> divisor_hex;
try {
if (divisor_hex == "0") throw std::runtime_error("Dzielenie przez zero nie jest dozwolone.");
auto [quotient_hex, remainder_hex] = hex_division(dividend_hex, divisor_hex);
std::cout << "Wynik dzielenia: " << quotient_hex << std::endl;
std::cout << "Reszta: " << remainder_hex << std::endl;
} catch (const std::exception& e) {
std::cout << "Błąd: " << e.what() << std::endl;
}
std::cout << std::endl;
}
return 0;
}
To share this paste please copy this url and send to your friends
RAW Paste Data