Hxdbs 1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include <iostream>

#include <string>
#include <cmath>
#include <stdexcept>
#include <ctime>
#include <exception>

using namespace std;

class HXDBS {
private:
string decToHeks(int dekadniBroj) const;
string heks;
public:
HXDBS(string heksBroj = "0");

int heksToDec() const;

string getHeks() const;

friend HXDBS operator+(const HXDBS&, const HXDBS&);


friend HXDBS operator-(const HXDBS&, const HXDBS&);
friend HXDBS operator*(const HXDBS&, const HXDBS&);
friend HXDBS operator/(const HXDBS&, const HXDBS&);
HXDBS& operator=(const HXDBS&);
friend bool operator==(const HXDBS&, const HXDBS&);
friend bool operator!=(const HXDBS&, const HXDBS&);
friend bool operator<(const HXDBS&, const HXDBS&);
friend bool operator>(const HXDBS&, const HXDBS&);
friend ostream& operator<<(ostream& os, const HXDBS& heksBroj);
friend istream& operator>>(istream& is, HXDBS& heksBroj);

};

HXDBS::HXDBS(string heksBroj) : heks(heksBroj) {}

int HXDBS::heksToDec() const {


int dekadniBroj = 0;
int osnovniMnozilac = 1;
int pocetniIndeks = (heks[0] == '-');

for (int i = heks.length() - 1; i >= pocetniIndeks; --i) {


char trenutniZnak = heks[i];

if ('0' <= trenutniZnak && trenutniZnak <= '9') {


dekadniBroj += (trenutniZnak - '0') * osnovniMnozilac;
} else if ('a' <= trenutniZnak && trenutniZnak <= 'f') {
dekadniBroj += (trenutniZnak - 'a' + 10) * osnovniMnozilac;
} else if ('A' <= trenutniZnak && trenutniZnak <= 'F') {
dekadniBroj += (trenutniZnak - 'A' + 10) * osnovniMnozilac;
} else {
throw std::domain_error("Neispravan unos");
}

osnovniMnozilac *= 16;
}

if (heks[0] == '-') {
return -dekadniBroj;
} else {
return dekadniBroj;
}
}

string HXDBS::getHeks() const {


return heks;
}

string HXDBS::decToHeks(int dekadniBroj) const {


string rezultat;
if (dekadniBroj == 0) {
return "0";
}

while (dekadniBroj != 0) {
int ostatak = abs(dekadniBroj % 16);
char hexDigit = (ostatak < 10) ? ('0' + ostatak) : ('A' + ostatak - 10);
rezultat = hexDigit + rezultat;
dekadniBroj /= 16;
}

if (heks[0] == '-') {
return '-' + rezultat;
} else {
return rezultat;
}
}

HXDBS operator+(const HXDBS& prvi, const HXDBS& drugi) {


int suma = prvi.heksToDec() + drugi.heksToDec();
return HXDBS(prvi.decToHeks(suma));
}

HXDBS operator-(const HXDBS& prvi, const HXDBS& drugi) {


int razlika = prvi.heksToDec() - drugi.heksToDec();
string rezultat = prvi.decToHeks(razlika);
if (razlika < 0) {
rezultat = "-" + rezultat;
}
return HXDBS(rezultat);
}

HXDBS operator*(const HXDBS& prvi, const HXDBS& drugi) {


int proizvod = prvi.heksToDec() * drugi.heksToDec();
return HXDBS(prvi.decToHeks(proizvod));
}

HXDBS operator/(const HXDBS& prvi, const HXDBS& drugi) {


if (drugi.heksToDec() == 0) {
throw logic_error("Dijeljenje nulom nije dozvoljeno.");
}

int kolicnik = prvi.heksToDec() / drugi.heksToDec();


int ostatak = prvi.heksToDec() % drugi.heksToDec();

string rezultat;
if (kolicnik == 0) {
rezultat = "0 ostatak " + prvi.decToHeks(ostatak);
} else {
rezultat = prvi.decToHeks(kolicnik) + " ostatak " +
prvi.decToHeks(ostatak);
}

return HXDBS(rezultat);
}

HXDBS& HXDBS::operator=(const HXDBS& objekat) {


if (this != &objekat) {
heks = objekat.heks;
}
return *this;
}

bool operator==(const HXDBS& prvi, const HXDBS& drugi) {


return prvi.heksToDec() == drugi.heksToDec();
}

bool operator!=(const HXDBS& prvi, const HXDBS& drugi) {


return !(prvi == drugi);
}

bool operator<(const HXDBS& prvi, const HXDBS& drugi) {


return prvi.heksToDec() < drugi.heksToDec();
}

bool operator>(const HXDBS& prvi, const HXDBS& drugi) {


return prvi.heksToDec() > drugi.heksToDec();
}

ostream& operator<<(ostream& os, const HXDBS& heksBroj) {


os << heksBroj.getHeks();
return os;
}

istream& operator>>(istream& is, HXDBS& heksBroj) {


string uneseniHeks;
is >> uneseniHeks;
heksBroj = HXDBS(uneseniHeks);
return is;
}

---------------------------------------------------------------
#include "hxdbs.h""

int main() {
clock_t t;
t = clock();

HXDBS heksBroj1;
HXDBS heksBroj2;
try {
cout << "Unesi 1. heksadekadni broj: ";
cin >> heksBroj1;
cout << "Unesi 2. heksadekadni broj: ";
cin >> heksBroj2;
} catch (const exception &e) {
cout << "Greška: " << e.what() << endl;
}

int dekadniBroj1 = heksBroj1.heksToDec();


int dekadniBroj2 = heksBroj2.heksToDec();
cout << "Dekadni broj (1): " << dekadniBroj1 << endl;
cout << "Dekadni broj (2): " << dekadniBroj2 << endl;

HXDBS sabiranje = heksBroj1 + heksBroj2;


HXDBS oduzimanje = heksBroj1 - heksBroj2;
HXDBS mnozenje = heksBroj1 * heksBroj2;
HXDBS dijeljenje = heksBroj1 / heksBroj2;
cout << endl
<< "Operacije u heksadekadnom brojnom sistemu: " << endl;
cout << "Sabiranje: " << sabiranje << endl;
cout << "Oduzimanje: " << oduzimanje << endl;
cout << "Mnozenje: " << mnozenje << endl;
cout << "Dijeljenje: " << dijeljenje << endl;

cout << endl


<< "Operacije u dekadnom brojnom sistemu: " << endl;
cout << "Sabiranje: " << dekadniBroj1 + dekadniBroj2 << endl;
cout << "Oduzimanje: " << dekadniBroj1 - dekadniBroj2 << endl;
cout << "Mnozenje: " << dekadniBroj1 * dekadniBroj2 << endl;
if (dekadniBroj2 != 0) {
cout << "Dijeljenje: " << dekadniBroj1 / dekadniBroj2 << " Ostatak: " <<
dekadniBroj1 % dekadniBroj2<< endl;
} else {
cout << "Dijeljenje: Nedefinisano (dijeljenje nulom nije dozvoljeno)." <<
endl;
}

if (heksBroj1 == heksBroj2) {
cout << endl <<"Heksadekadni brojevi su jednaki." << endl;
} else if (heksBroj1 != heksBroj2) {
cout << endl << "Heksadekadni brojevi nisu jednaki." << endl;
}

if (heksBroj1 < heksBroj2) {


cout << "Heksadekadni broj 1 je manji od broja 2." << endl;
} else if (heksBroj1 > heksBroj2) {
cout << "Heksadekadni broj 1 je veci od broja 2." << endl;
} else {
cout << "Heksadekadni brojevi su jednaki." << endl;
}

t = clock() - t;
cout << endl
<< "Vrijeme izvrsavanja je " << ((long double)t) / CLOCKS_PER_SEC << "
sekundi.";

return 0;
}

You might also like