diff --git a/uczelnia/2_semestr/programowanie/laby/lab7/liczba.cpp b/uczelnia/2_semestr/programowanie/laby/lab7/liczba.cpp new file mode 100644 index 0000000..1405a1a --- /dev/null +++ b/uczelnia/2_semestr/programowanie/laby/lab7/liczba.cpp @@ -0,0 +1,24 @@ +#include +#include + +class Liczba { + int wartosc; + +public: + Liczba(int w) : wartosc(w) {} + Liczba operator+(Liczba &inny) { + wartosc += inny.wartosc; + return *this; + } + friend std::ostream &operator<<(std::ostream &os, const Liczba &liczba) { + return os << liczba.wartosc; + } +}; + +int main() { + Liczba l1 = Liczba(5); + Liczba l2 = Liczba(10); + Liczba l3 = l1 + l2; + std::cout << l3; + return 0; +} diff --git a/uczelnia/2_semestr/programowanie/laby/lab7/out/1 b/uczelnia/2_semestr/programowanie/laby/lab7/out/1 new file mode 100755 index 0000000..8453402 Binary files /dev/null and b/uczelnia/2_semestr/programowanie/laby/lab7/out/1 differ diff --git a/uczelnia/2_semestr/programowanie/laby/lab7/out/liczba b/uczelnia/2_semestr/programowanie/laby/lab7/out/liczba new file mode 100755 index 0000000..06ca96d Binary files /dev/null and b/uczelnia/2_semestr/programowanie/laby/lab7/out/liczba differ diff --git a/uczelnia/2_semestr/programowanie/laby/lab7/out/ulamki b/uczelnia/2_semestr/programowanie/laby/lab7/out/ulamki new file mode 100755 index 0000000..32cf855 Binary files /dev/null and b/uczelnia/2_semestr/programowanie/laby/lab7/out/ulamki differ diff --git a/uczelnia/2_semestr/programowanie/laby/lab7/punkt.cpp b/uczelnia/2_semestr/programowanie/laby/lab7/punkt.cpp new file mode 100644 index 0000000..ad2a8ff --- /dev/null +++ b/uczelnia/2_semestr/programowanie/laby/lab7/punkt.cpp @@ -0,0 +1,45 @@ +#include + +class Punkt2d { + +public: + double x, y; + Punkt2d(double x, double y) : x(x), y(y) {} + Punkt2d() : x(0), y(0) {} + + Punkt2d operator+(const Punkt2d &inny_pkt) const { + Punkt2d pkt; + pkt.x = x + inny_pkt.x; + pkt.y = y + inny_pkt.y; + return pkt; + } + + Punkt2d operator-(const Punkt2d &inny_pkt) const { + Punkt2d pkt; + pkt.x = x - inny_pkt.x; + pkt.y = y - inny_pkt.y; + return pkt; + } + bool operator==(const Punkt2d &inny_pkt) const { + if (x != inny_pkt.x || y != inny_pkt.y) { + return false; + } + return true; + } + bool operator!=(const Punkt2d &inny_pkt) const { + return !(*this == inny_pkt); + } + + friend std::ostream &operator<<(std::ostream &os, const Punkt2d &inny_pkt) { + os << '(' << inny_pkt.x << ',' << inny_pkt.y << ')'; + return os; + } +}; + +int main() { + Punkt2d a(6, 7), b(3, 4); + Punkt2d c = a + b; + std::cout << c << std::endl; // (9, 11) + std::cout << (a == b) << std::endl; // false + std::cout << (a != a) << std::endl; // false +} diff --git a/uczelnia/2_semestr/programowanie/laby/lab7/ulamki.cpp b/uczelnia/2_semestr/programowanie/laby/lab7/ulamki.cpp new file mode 100644 index 0000000..5caaf79 --- /dev/null +++ b/uczelnia/2_semestr/programowanie/laby/lab7/ulamki.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +class Ulamek { + void skroc() { + int skracacz = std::gcd(licznik, mianownik); + if (mianownik < 0) { + licznik = -licznik; + mianownik = -mianownik; + } + licznik /= skracacz; + mianownik /= skracacz; + } + +public: + int licznik, mianownik; + + Ulamek(int licznik, int mianownik) : licznik(licznik), mianownik(mianownik) { + if (mianownik == 0) { + throw std::invalid_argument("Mianownik nie moze byc zerem"); + } + skroc(); + } + + Ulamek() : licznik(0), mianownik(1) {} + + Ulamek operator+(const Ulamek &inny_ulamek) const { + return Ulamek(licznik * inny_ulamek.mianownik + + inny_ulamek.licznik * mianownik, + mianownik * inny_ulamek.mianownik); + } + + Ulamek operator-(const Ulamek &inny_ulamek) const { + return Ulamek(licznik * inny_ulamek.mianownik - + inny_ulamek.licznik * mianownik, + mianownik * inny_ulamek.mianownik); + } + + Ulamek operator*(const Ulamek &inny_ulamek) const { + return Ulamek(licznik * inny_ulamek.licznik, + mianownik * inny_ulamek.mianownik); + } + + Ulamek operator/(const Ulamek &inny_ulamek) const { + if (inny_ulamek.licznik == 0) { + throw std::invalid_argument("Dzielenie przez zero"); + } + return Ulamek(licznik * inny_ulamek.mianownik, + mianownik * inny_ulamek.licznik); + } + + bool operator==(const Ulamek &inny_ulamek) const { + return licznik == inny_ulamek.licznik && mianownik == inny_ulamek.mianownik; + } + + bool operator!=(const Ulamek &inny_ulamek) const { + return !(*this == inny_ulamek); + } + + bool operator<(const Ulamek &inny_ulamek) const { + return licznik * inny_ulamek.mianownik < inny_ulamek.licznik * mianownik; + } + + bool operator>=(const Ulamek &inny_ulamek) const { + return !(*this < inny_ulamek); + } + + bool operator<=(const Ulamek &inny_ulamek) const { + return *this == inny_ulamek || *this < inny_ulamek; + } + + bool operator>(const Ulamek &inny_ulamek) const { + return !(*this <= inny_ulamek); + } + + friend std::ostream &operator<<(std::ostream &os, const Ulamek &u) { + os << u.licznik << '/' << u.mianownik; + return os; + } +}; + +Ulamek stworz_ulamek() { + int licznik, mianownik; + std::cout << "Podaj licznik: "; + std::cin >> licznik; + std::cout << "Podaj mianownik: "; + std::cin >> mianownik; + return Ulamek(licznik, mianownik); +} + +int main() { + Ulamek u1 = stworz_ulamek(); + std::cout << "Ulamek 1: " << u1 << std::endl; + Ulamek u2 = stworz_ulamek(); + std::cout << "Ulamek 2: " << u2 << std::endl; + std::cout << "=== OPERACJE ===" << std::endl; + std::cout << "Dodawanie: " << u1 + u2 << std::endl; + std::cout << "Odejmowanie: " << u1 - u2 << std::endl; + std::cout << "Mnożenie: " << u1 * u2 << std::endl; + try { + std::cout << "Dzielenie: " << u1 / u2 << std::endl; + } catch (const std::invalid_argument &e) { + std::cout << "Błąd: " << e.what() << std::endl; + } + std::cout << "=== PORÓWNYWANIE ===" << std::endl; + std::cout << std::boolalpha; + std::cout << u1 << " == " << u2 << ": " << (u1 == u2) << std::endl; + std::cout << u1 << " != " << u2 << ": " << (u1 != u2) << std::endl; + std::cout << u1 << " <= " << u2 << ": " << (u1 <= u2) << std::endl; + std::cout << u1 << " >= " << u2 << ": " << (u1 >= u2) << std::endl; + std::cout << u1 << " < " << u2 << ": " << (u1 < u2) << std::endl; + std::cout << u1 << " > " << u2 << ": " << (u1 > u2) << std::endl; +} diff --git a/uczelnia/2_semestr/programowanie/laby/lab8/liczba.cpp b/uczelnia/2_semestr/programowanie/laby/lab8/liczba.cpp new file mode 100644 index 0000000..1405a1a --- /dev/null +++ b/uczelnia/2_semestr/programowanie/laby/lab8/liczba.cpp @@ -0,0 +1,24 @@ +#include +#include + +class Liczba { + int wartosc; + +public: + Liczba(int w) : wartosc(w) {} + Liczba operator+(Liczba &inny) { + wartosc += inny.wartosc; + return *this; + } + friend std::ostream &operator<<(std::ostream &os, const Liczba &liczba) { + return os << liczba.wartosc; + } +}; + +int main() { + Liczba l1 = Liczba(5); + Liczba l2 = Liczba(10); + Liczba l3 = l1 + l2; + std::cout << l3; + return 0; +} diff --git a/uczelnia/2_semestr/programowanie/laby/lab8/punkt.cpp b/uczelnia/2_semestr/programowanie/laby/lab8/punkt.cpp new file mode 100644 index 0000000..0f2c921 --- /dev/null +++ b/uczelnia/2_semestr/programowanie/laby/lab8/punkt.cpp @@ -0,0 +1,62 @@ +#include + +class Punkt2d { + +public: + double x, y; + Punkt2d(double x, double y) : x(x), y(y) {} + Punkt2d() : x(0), y(0) {} + + Punkt2d operator+=(const Punkt2d &inny_pkt) { + this->x += inny_pkt.x; + this->y += inny_pkt.y; + return *this; + } + + Punkt2d operator-=(const Punkt2d &inny_pkt) { + this->x -= inny_pkt.x; + this->y -= inny_pkt.y; + return *this; + } + + Punkt2d operator*=(const Punkt2d &inny_pkt) { + this->x *= inny_pkt.x; + this->y *= inny_pkt.y; + return *this; + } + + Punkt2d operator+(const Punkt2d &inny_pkt) const { + Punkt2d p = *this; + p += inny_pkt; + return p; + } + + Punkt2d operator-(const Punkt2d &inny_pkt) const { + Punkt2d pkt; + pkt.x = x - inny_pkt.x; + pkt.y = y - inny_pkt.y; + return pkt; + } + bool operator==(const Punkt2d &inny_pkt) const { + if (x != inny_pkt.x || y != inny_pkt.y) { + return false; + } + return true; + } + bool operator!=(const Punkt2d &inny_pkt) const { + return !(*this == inny_pkt); + } + + friend std::ostream &operator<<(std::ostream &os, const Punkt2d &inny_pkt) { + os << '(' << inny_pkt.x << ',' << inny_pkt.y << ')'; + return os; + } +}; + +int main() { + Punkt2d a(6, 7), b(3, 4); + Punkt2d c = a + b; + std::cout << c << std::endl; // (9, 11) + std::cout << (a == b) << std::endl; // false + std::cout << (a != a) << std::endl; // false +} diff --git a/uczelnia/2_semestr/programowanie/laby/lab8/ulamki.cpp b/uczelnia/2_semestr/programowanie/laby/lab8/ulamki.cpp new file mode 100644 index 0000000..5caaf79 --- /dev/null +++ b/uczelnia/2_semestr/programowanie/laby/lab8/ulamki.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +class Ulamek { + void skroc() { + int skracacz = std::gcd(licznik, mianownik); + if (mianownik < 0) { + licznik = -licznik; + mianownik = -mianownik; + } + licznik /= skracacz; + mianownik /= skracacz; + } + +public: + int licznik, mianownik; + + Ulamek(int licznik, int mianownik) : licznik(licznik), mianownik(mianownik) { + if (mianownik == 0) { + throw std::invalid_argument("Mianownik nie moze byc zerem"); + } + skroc(); + } + + Ulamek() : licznik(0), mianownik(1) {} + + Ulamek operator+(const Ulamek &inny_ulamek) const { + return Ulamek(licznik * inny_ulamek.mianownik + + inny_ulamek.licznik * mianownik, + mianownik * inny_ulamek.mianownik); + } + + Ulamek operator-(const Ulamek &inny_ulamek) const { + return Ulamek(licznik * inny_ulamek.mianownik - + inny_ulamek.licznik * mianownik, + mianownik * inny_ulamek.mianownik); + } + + Ulamek operator*(const Ulamek &inny_ulamek) const { + return Ulamek(licznik * inny_ulamek.licznik, + mianownik * inny_ulamek.mianownik); + } + + Ulamek operator/(const Ulamek &inny_ulamek) const { + if (inny_ulamek.licznik == 0) { + throw std::invalid_argument("Dzielenie przez zero"); + } + return Ulamek(licznik * inny_ulamek.mianownik, + mianownik * inny_ulamek.licznik); + } + + bool operator==(const Ulamek &inny_ulamek) const { + return licznik == inny_ulamek.licznik && mianownik == inny_ulamek.mianownik; + } + + bool operator!=(const Ulamek &inny_ulamek) const { + return !(*this == inny_ulamek); + } + + bool operator<(const Ulamek &inny_ulamek) const { + return licznik * inny_ulamek.mianownik < inny_ulamek.licznik * mianownik; + } + + bool operator>=(const Ulamek &inny_ulamek) const { + return !(*this < inny_ulamek); + } + + bool operator<=(const Ulamek &inny_ulamek) const { + return *this == inny_ulamek || *this < inny_ulamek; + } + + bool operator>(const Ulamek &inny_ulamek) const { + return !(*this <= inny_ulamek); + } + + friend std::ostream &operator<<(std::ostream &os, const Ulamek &u) { + os << u.licznik << '/' << u.mianownik; + return os; + } +}; + +Ulamek stworz_ulamek() { + int licznik, mianownik; + std::cout << "Podaj licznik: "; + std::cin >> licznik; + std::cout << "Podaj mianownik: "; + std::cin >> mianownik; + return Ulamek(licznik, mianownik); +} + +int main() { + Ulamek u1 = stworz_ulamek(); + std::cout << "Ulamek 1: " << u1 << std::endl; + Ulamek u2 = stworz_ulamek(); + std::cout << "Ulamek 2: " << u2 << std::endl; + std::cout << "=== OPERACJE ===" << std::endl; + std::cout << "Dodawanie: " << u1 + u2 << std::endl; + std::cout << "Odejmowanie: " << u1 - u2 << std::endl; + std::cout << "Mnożenie: " << u1 * u2 << std::endl; + try { + std::cout << "Dzielenie: " << u1 / u2 << std::endl; + } catch (const std::invalid_argument &e) { + std::cout << "Błąd: " << e.what() << std::endl; + } + std::cout << "=== PORÓWNYWANIE ===" << std::endl; + std::cout << std::boolalpha; + std::cout << u1 << " == " << u2 << ": " << (u1 == u2) << std::endl; + std::cout << u1 << " != " << u2 << ": " << (u1 != u2) << std::endl; + std::cout << u1 << " <= " << u2 << ": " << (u1 <= u2) << std::endl; + std::cout << u1 << " >= " << u2 << ": " << (u1 >= u2) << std::endl; + std::cout << u1 << " < " << u2 << ": " << (u1 < u2) << std::endl; + std::cout << u1 << " > " << u2 << ": " << (u1 > u2) << std::endl; +}