mirror of
https://github.com/szymon-jozef/ZUT-notatki.git
synced 2026-09-07 12:10:40 +02:00
rzeczy
This commit is contained in:
BIN
uczelnia/2_semestr/matematyka_stosowana/laby/ankieta.RData
Normal file
BIN
uczelnia/2_semestr/matematyka_stosowana/laby/ankieta.RData
Normal file
Binary file not shown.
66
uczelnia/2_semestr/matematyka_stosowana/laby/lab_7.R
Normal file
66
uczelnia/2_semestr/matematyka_stosowana/laby/lab_7.R
Normal file
@@ -0,0 +1,66 @@
|
||||
# a)
|
||||
|
||||
summary(Ankieta$Płeć)
|
||||
# Length Class Mode
|
||||
# 72 character character
|
||||
|
||||
Ankieta$Płeć = factor(Ankieta$Płeć) # ustawiamy płeć na typ kategorialny
|
||||
# robimy to samo dla pozostałych niemierzalnych cech
|
||||
Ankieta$Sysytem = factor(Ankieta$System)
|
||||
Ankieta$M.zamieszkania = factor(Ankieta$M.zamieszkania)
|
||||
Ankieta$Sz.średnia = factor(Ankieta$Sz.średnia)
|
||||
Ankieta$Rodzeństwo = factor(Ankieta$Rodzeństwo)
|
||||
|
||||
summary(Ankieta$Płeć)
|
||||
|
||||
# b)
|
||||
Ankieta$Średnia = round(((Ankieta$Algebra * 5 + Ankieta$Fizyka * 5 + Ankieta$Programowanie1 * 5 + Ankieta$MSzS1 * 5 + Ankieta$Algorytmy1 * 6 + Ankieta$WdI * 4) / 30), 2);
|
||||
|
||||
# c)
|
||||
Ankieta.kursy = subset(Ankieta, select = c(Algebra, Fizyka, Programowanie1, WdI, MSzS1, Algorytmy1))
|
||||
|
||||
# d)
|
||||
Ankieta$Waga.dag = Ankieta$Waga * 100
|
||||
|
||||
# e)
|
||||
Ankieta$Waga.dag = NULL;
|
||||
|
||||
# f)
|
||||
ggplot(Ankieta, aes(x=Płeć, y=Wzrost))+geom_boxplot(fill='red', col='blue')
|
||||
ggplot(Ankieta, aes(x=Płeć, y=Waga))+geom_boxplot(fill='red', col='blue')
|
||||
|
||||
# g)
|
||||
zakres3sigm = function(x) {
|
||||
data.frame(lewy.kres=mean(x) - 3 * sd(x), prawy.kres=mean(x) + 3 * sd(x))
|
||||
}
|
||||
|
||||
zakres3sigm(Ankieta$Wzrost)
|
||||
|
||||
# h)
|
||||
Ankieta.M = subset(Ankieta, Płeć == 'M')
|
||||
Ankieta.K = subset(Ankieta, Płeć == 'K')
|
||||
|
||||
# i)
|
||||
|
||||
# === K ===
|
||||
zakres3sigm(Ankieta.K$Wzrost)
|
||||
summary(Ankieta.K$Wzrost)
|
||||
|
||||
zakres3sigm(Ankieta.K$Waga)
|
||||
summary(Ankieta.K$Waga)
|
||||
|
||||
# === M ===
|
||||
zakres3sigm(Ankieta.M$Wzrost)
|
||||
summary(Ankieta.M$Wzrost)
|
||||
|
||||
zakres3sigm(Ankieta.M$Waga)
|
||||
summary(Ankieta.M$Waga)
|
||||
|
||||
which(Ankieta.M$Waga > 126)
|
||||
Ankieta.M$Waga[28] = round(mean(Ankieta.M$Waga[-28]), 1)
|
||||
|
||||
i = which(Ankieta$Waga > 126)
|
||||
Ankieta$Waga[i] = round(mean(Ankieta.M$Waga[-28]), 1)
|
||||
|
||||
# j)
|
||||
|
||||
121
uczelnia/2_semestr/programowanie/laby/kolokwium2.cpp
Normal file
121
uczelnia/2_semestr/programowanie/laby/kolokwium2.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Towar {
|
||||
double cena;
|
||||
int sztuki;
|
||||
|
||||
public:
|
||||
std::string nazwa;
|
||||
Towar(std::string nazwa, double cena, int sztuki)
|
||||
: nazwa(nazwa), cena(cena), sztuki(sztuki) {}
|
||||
Towar() {}
|
||||
|
||||
void wypisz() const {
|
||||
std::cout << "Nazwa: " << nazwa << '\n'
|
||||
<< "Cena: " << cena << '\n'
|
||||
<< "Liczba sztuk: " << sztuki << std::endl;
|
||||
}
|
||||
double wartosc() const { return cena * sztuki; }
|
||||
void zmienCene(double nowaCena) { cena = nowaCena; }
|
||||
void dodajSztuki(int ile) { sztuki += ile; }
|
||||
// int operator+(Towar &t) { return cena + t.cena; }
|
||||
Towar operator+(Towar &t) {
|
||||
return Towar(nazwa + t.nazwa, cena + t.cena, sztuki + t.sztuki);
|
||||
}
|
||||
};
|
||||
|
||||
class Sklep {
|
||||
Towar *sklep;
|
||||
int max_ammount;
|
||||
int current_ammount;
|
||||
static int sklep_ammount;
|
||||
|
||||
public:
|
||||
Sklep(size_t max_towary)
|
||||
: max_ammount(max_towary), sklep(new Towar[max_towary]),
|
||||
current_ammount(0) {
|
||||
Sklep::sklep_ammount++;
|
||||
}
|
||||
|
||||
~Sklep() {
|
||||
delete[] sklep;
|
||||
sklep = nullptr;
|
||||
Sklep::sklep_ammount--;
|
||||
}
|
||||
|
||||
bool dodajTowar(const Towar &t) {
|
||||
if (current_ammount >= max_ammount) {
|
||||
return false;
|
||||
}
|
||||
sklep[current_ammount++] = t;
|
||||
return true;
|
||||
}
|
||||
|
||||
Towar *znajdzTowar(const std::string &nazwa) {
|
||||
for (int i = 0; i < current_ammount; i++) {
|
||||
if (sklep[i].nazwa == nazwa) {
|
||||
return &sklep[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void wypiszWszystkie() const {
|
||||
for (int i = 0; i < current_ammount; i++) {
|
||||
sklep[i].wypisz();
|
||||
}
|
||||
}
|
||||
|
||||
double obliczWartoscSklepu() const {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < current_ammount; i++) {
|
||||
sum += sklep[i].wartosc();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int ileSklepow() { return Sklep::sklep_ammount; }
|
||||
};
|
||||
|
||||
int Sklep::sklep_ammount = 0;
|
||||
|
||||
int main() {
|
||||
Sklep sklep = Sklep(5);
|
||||
std::cout << "=== 1. Oryginalny stan sklepu ===" << std::endl;
|
||||
sklep.dodajTowar(Towar("Marchewka", 2, 3));
|
||||
sklep.dodajTowar(Towar("Banan", 4, 2));
|
||||
sklep.dodajTowar(Towar("Jabłko", 5, 3));
|
||||
sklep.dodajTowar(Towar("Ziemniak", 10, 15));
|
||||
sklep.wypiszWszystkie();
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "=== 2. Modyfikowany stan sklepu ===" << std::endl;
|
||||
|
||||
Towar *znalezionyTowar = sklep.znajdzTowar("Marchewka");
|
||||
znalezionyTowar->zmienCene(4);
|
||||
sklep.wypiszWszystkie();
|
||||
Towar *znalezionyTowar2 = sklep.znajdzTowar("Banan");
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "=== 3. Dodajemy marchewkę z bananem ===" << std::endl;
|
||||
|
||||
Towar suma_towarow = *znalezionyTowar + *znalezionyTowar2;
|
||||
suma_towarow.wypisz();
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "=== 4. Wypisujemy wszystkie towary ===" << std::endl;
|
||||
|
||||
sklep.wypiszWszystkie();
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "=== 5. Wypisujemy łączną wartość sklepu ===" << std::endl;
|
||||
|
||||
std::cout << "Wartość: " << sklep.obliczWartoscSklepu() << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "=== 6. Wypisujemy łączną ilość sklepów ===" << std::endl;
|
||||
|
||||
std::cout << "Ilość: " << sklep.ileSklepow() << std::endl;
|
||||
}
|
||||
83
uczelnia/2_semestr/programowanie/laby/kolokwium2_probne.cpp
Normal file
83
uczelnia/2_semestr/programowanie/laby/kolokwium2_probne.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Produkt {
|
||||
double cena;
|
||||
int ilosc;
|
||||
|
||||
public:
|
||||
std::string nazwa;
|
||||
Produkt(std::string nazwa, double cena, int ilosc)
|
||||
: nazwa(nazwa), cena(cena), ilosc(ilosc) {}
|
||||
Produkt() {}
|
||||
|
||||
void wypisz() const {
|
||||
std::cout << "Nazwa: " << nazwa << '\n'
|
||||
<< "Cena: " << cena << '\n'
|
||||
<< "Ilość: " << ilosc << std::endl;
|
||||
}
|
||||
|
||||
double wartosc() const { return cena * ilosc; }
|
||||
void zmienCene(double nowaCena) { cena = nowaCena; }
|
||||
void dodajSztuke(int ile) { ilosc += ile; }
|
||||
};
|
||||
|
||||
class Magazyn {
|
||||
int max_pojemnosc;
|
||||
int aktualna_pojemnosc;
|
||||
static int ilosc_magazynow;
|
||||
|
||||
public:
|
||||
Produkt *magazyn;
|
||||
|
||||
Magazyn(int pojemnosc)
|
||||
: max_pojemnosc(pojemnosc), magazyn(new Produkt[pojemnosc]),
|
||||
aktualna_pojemnosc(0) {}
|
||||
~Magazyn() { delete[] magazyn; }
|
||||
bool dodajProdukt(const Produkt &p) {
|
||||
if (aktualna_pojemnosc >= max_pojemnosc) {
|
||||
return false;
|
||||
}
|
||||
magazyn[aktualna_pojemnosc++] = p;
|
||||
return true;
|
||||
}
|
||||
|
||||
Produkt *znajdzProdukt(const std::string &nazwa) {
|
||||
for (int i = 0; i < aktualna_pojemnosc; i++) {
|
||||
if (magazyn[i].nazwa == nazwa) {
|
||||
return &magazyn[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void wypiszWszystkie() const {
|
||||
for (int i = 0; i < aktualna_pojemnosc; i++) {
|
||||
magazyn[i].wypisz();
|
||||
}
|
||||
}
|
||||
|
||||
double obliczWartoscMagazynu() const {
|
||||
double wartosc = 0;
|
||||
for (int i = 0; i < aktualna_pojemnosc; i++) {
|
||||
wartosc += magazyn[i].wartosc();
|
||||
}
|
||||
return wartosc;
|
||||
}
|
||||
|
||||
static int ileMagazynow() { return Magazyn::ilosc_magazynow; }
|
||||
};
|
||||
|
||||
int Magazyn::ilosc_magazynow = 0;
|
||||
|
||||
int main() {
|
||||
Magazyn m = Magazyn(10);
|
||||
m.dodajProdukt(Produkt("marchewka", 2, 2));
|
||||
m.dodajProdukt(Produkt("banan", 5, 3));
|
||||
m.dodajProdukt(Produkt("gruszka", 4, 10));
|
||||
Produkt *p = m.znajdzProdukt("gruszka");
|
||||
p->zmienCene(234);
|
||||
m.wypiszWszystkie();
|
||||
m.obliczWartoscMagazynu();
|
||||
m.ileMagazynow();
|
||||
}
|
||||
22
uczelnia/2_semestr/programowanie/laby/lab8/kontener.cpp
Normal file
22
uczelnia/2_semestr/programowanie/laby/lab8/kontener.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
class Kontener {
|
||||
int *dane;
|
||||
int n;
|
||||
|
||||
public:
|
||||
Kontener(int rozmiar) : n(rozmiar), dane(new int[rozmiar]()) {}
|
||||
const int &operator[](int i) const { return dane[i]; }
|
||||
|
||||
Kontener &operator+=(const Kontener &inny) {
|
||||
for (int i = 0; i < n; i++)
|
||||
dane[i] += inny.dane[i];
|
||||
return *this;
|
||||
}
|
||||
operator int() const { return n; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
Kontener k = Kontener(5);
|
||||
k += 1;
|
||||
std::cout << int(k);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
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;
|
||||
}
|
||||
45
uczelnia/2_semestr/programowanie/laby/lab8/licznik.cpp
Normal file
45
uczelnia/2_semestr/programowanie/laby/lab8/licznik.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
class Licznik {
|
||||
int wartosc;
|
||||
|
||||
public:
|
||||
Licznik(int wartosc) : wartosc(wartosc) {}
|
||||
Licznik() : wartosc(0) {}
|
||||
Licznik &operator++() {
|
||||
wartosc++;
|
||||
return *this;
|
||||
}
|
||||
Licznik operator++(int) {
|
||||
Licznik stary = *this;
|
||||
++wartosc;
|
||||
return stary;
|
||||
}
|
||||
Licznik &operator--() {
|
||||
wartosc--;
|
||||
return *this;
|
||||
}
|
||||
Licznik operator--(int) {
|
||||
Licznik stary = *this;
|
||||
--wartosc;
|
||||
return stary;
|
||||
}
|
||||
Licznik &operator=(const Licznik &inny_licznik) {
|
||||
if (this != &inny_licznik) {
|
||||
wartosc = inny_licznik.wartosc;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
friend std::ostream &operator<<(std::ostream &os, Licznik &licznik) {
|
||||
os << licznik.wartosc;
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
// int main() {
|
||||
// Licznik a(5);
|
||||
// std::cout << a++ << std::endl; // 6
|
||||
// std::cout << a << std::endl; // 6
|
||||
// std::cout << ++a << std::endl; // 7
|
||||
// }
|
||||
117
uczelnia/2_semestr/programowanie/laby/lab8/macierz.cpp
Normal file
117
uczelnia/2_semestr/programowanie/laby/lab8/macierz.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#define SIZE 2
|
||||
|
||||
class Macierz2x2 {
|
||||
|
||||
public:
|
||||
int macierz[SIZE][SIZE];
|
||||
Macierz2x2(int a, int b, int c, int d) {
|
||||
macierz[0][0] = a;
|
||||
macierz[0][1] = b;
|
||||
macierz[1][0] = c;
|
||||
macierz[1][1] = d;
|
||||
}
|
||||
Macierz2x2() {
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
macierz[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Macierz2x2 &operator+=(const Macierz2x2 &inna_macierz) {
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
macierz[i][j] += inna_macierz.macierz[i][j];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Macierz2x2 operator+(const Macierz2x2 &inna_macierz) const {
|
||||
Macierz2x2 m = *this;
|
||||
m += inna_macierz;
|
||||
return m;
|
||||
}
|
||||
|
||||
Macierz2x2 &operator-=(const Macierz2x2 &inna_macierz) {
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
macierz[i][j] -= inna_macierz.macierz[i][j];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Macierz2x2 operator-(const Macierz2x2 &inna_macierz) const {
|
||||
Macierz2x2 m = *this;
|
||||
m -= inna_macierz;
|
||||
return m;
|
||||
}
|
||||
|
||||
Macierz2x2 operator*(const Macierz2x2 &inna_macierz) const {
|
||||
Macierz2x2 m;
|
||||
m.macierz[0][0] = macierz[0][0] * inna_macierz.macierz[0][0] +
|
||||
macierz[0][1] * inna_macierz.macierz[1][0];
|
||||
m.macierz[0][1] = macierz[0][0] * inna_macierz.macierz[0][1] +
|
||||
macierz[0][1] * inna_macierz.macierz[1][1];
|
||||
m.macierz[1][0] = macierz[1][0] * inna_macierz.macierz[0][0] +
|
||||
macierz[1][1] * inna_macierz.macierz[1][0];
|
||||
m.macierz[1][1] = macierz[1][0] * inna_macierz.macierz[0][1] +
|
||||
macierz[1][1] * inna_macierz.macierz[1][1];
|
||||
return m;
|
||||
}
|
||||
|
||||
int *operator[](int x) { return macierz[x]; }
|
||||
const int *operator[](int x) const { return macierz[x]; }
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Macierz2x2 &macierz) {
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
os << '[';
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
os << macierz.macierz[i][j];
|
||||
if (j < SIZE - 1)
|
||||
os << " ";
|
||||
}
|
||||
os << ']';
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
friend std::istream &operator>>(std::istream &is, Macierz2x2 &macierz) {
|
||||
char separator;
|
||||
is >> macierz.macierz[0][0] >> separator >> macierz.macierz[0][1] >>
|
||||
separator >> macierz.macierz[1][0] >> separator >>
|
||||
macierz.macierz[1][1];
|
||||
return is;
|
||||
}
|
||||
|
||||
explicit operator double() const {
|
||||
return macierz[0][0] * macierz[1][1] - macierz[1][0] * macierz[0][1];
|
||||
}
|
||||
|
||||
const int operator()(int i, int j) const { return macierz[i][j]; }
|
||||
int &operator()(int i, int j) { return macierz[i][j]; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
Macierz2x2 m1 = Macierz2x2(1, 2, 3, 4);
|
||||
std::cout << "Macierz 1:\n";
|
||||
std::cout << m1;
|
||||
Macierz2x2 m2;
|
||||
std::cout << "Podaj swoją macierz: \n";
|
||||
std::cout << "Macierz 2:\n";
|
||||
std::cin >> m2;
|
||||
std::cout << m2;
|
||||
Macierz2x2 m3 = m1 + m2;
|
||||
Macierz2x2 m4 = m1 * m2;
|
||||
std::cout << "Macierz 3(dodawanie):\n";
|
||||
std::cout << m3 << std::endl;
|
||||
std::cout << "Macierz 4(mnożenie):\n";
|
||||
std::cout << m4 << std::endl;
|
||||
std::cout << "Wyznacznik macierz m1: " << double(m1) << std::endl;
|
||||
std::cout << "Element (1,1) macierzy m1: " << m1(1, 1) << std::endl;
|
||||
;
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
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
|
||||
}
|
||||
51
uczelnia/2_semestr/programowanie/laby/lab8/tablica.cpp
Normal file
51
uczelnia/2_semestr/programowanie/laby/lab8/tablica.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
class Tablica {
|
||||
int *arr;
|
||||
|
||||
void is_size_correct(size_t index) const {
|
||||
if (index > size) {
|
||||
throw std::out_of_range("Zbyt duży index! Maksymalny rozmiar to %d" +
|
||||
std::to_string(size));
|
||||
}
|
||||
|
||||
if (index < 0) {
|
||||
throw std::out_of_range("Zbyt mały index! Minimalny rozmiar to 0");
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
size_t size;
|
||||
|
||||
Tablica(size_t size) : size{size} { arr = new int[size]; }
|
||||
|
||||
// po zakomentowaniu tej metody program wciąż się kompiluje
|
||||
int &operator[](size_t index) const {
|
||||
is_size_correct(index);
|
||||
return *(arr + index);
|
||||
}
|
||||
|
||||
int &operator[](size_t index) {
|
||||
is_size_correct(index);
|
||||
return *(arr + index);
|
||||
}
|
||||
};
|
||||
|
||||
void przeczytaj_tablice(Tablica &tablica) {
|
||||
for (int i = 0; i < tablica.size; i++) {
|
||||
try {
|
||||
std::cout << tablica[i] << '\n';
|
||||
} catch (const std::out_of_range &e) {
|
||||
std::cout << e.what();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Tablica t = Tablica(10);
|
||||
przeczytaj_tablice(t);
|
||||
}
|
||||
@@ -26,6 +26,10 @@ public:
|
||||
|
||||
Ulamek() : licznik(0), mianownik(1) {}
|
||||
|
||||
explicit operator double() const {
|
||||
return static_cast<double>(licznik) / mianownik;
|
||||
}
|
||||
|
||||
Ulamek operator+(const Ulamek &inny_ulamek) const {
|
||||
return Ulamek(licznik * inny_ulamek.mianownik +
|
||||
inny_ulamek.licznik * mianownik,
|
||||
@@ -79,37 +83,28 @@ public:
|
||||
os << u.licznik << '/' << u.mianownik;
|
||||
return os;
|
||||
}
|
||||
friend std::istream &operator>>(std::istream &is, Ulamek &u) {
|
||||
char separator;
|
||||
int licznik, mianownik;
|
||||
is >> licznik >> separator >> mianownik;
|
||||
|
||||
if (mianownik == 0) {
|
||||
is.setstate(std::ios::failbit); // nadmiarowe, bo w konstruktorze też
|
||||
// sprawdzamy czy mianownik == 0
|
||||
}
|
||||
|
||||
if (is) {
|
||||
u = Ulamek(licznik, mianownik);
|
||||
}
|
||||
return is;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
Ulamek ulamek;
|
||||
std::cin >> ulamek;
|
||||
// double ulamek_double = ulamek; ta linia się nie skompiluje, ponieważ k
|
||||
// onwersja musi być explicit
|
||||
double ulamek_double = double(ulamek);
|
||||
std::cout << ulamek_double;
|
||||
}
|
||||
54
uczelnia/2_semestr/programowanie/laby/lab8/wektor.cpp
Normal file
54
uczelnia/2_semestr/programowanie/laby/lab8/wektor.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <iostream>
|
||||
|
||||
class Wektor2D {
|
||||
|
||||
public:
|
||||
double x, y;
|
||||
Wektor2D(double x, double y) : x(x), y(y) {}
|
||||
Wektor2D() : x(0), y(0) {}
|
||||
|
||||
Wektor2D operator+=(const Wektor2D &inny_pkt) {
|
||||
this->x += inny_pkt.x;
|
||||
this->y += inny_pkt.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Wektor2D operator-=(const Wektor2D &inny_pkt) {
|
||||
this->x -= inny_pkt.x;
|
||||
this->y -= inny_pkt.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Wektor2D operator*=(const Wektor2D &inny_pkt) {
|
||||
this->x *= inny_pkt.x;
|
||||
this->y *= inny_pkt.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Wektor2D operator+(const Wektor2D &inny_pkt) const {
|
||||
Wektor2D p = *this;
|
||||
p += inny_pkt;
|
||||
return p;
|
||||
}
|
||||
|
||||
Wektor2D operator-(const Wektor2D &inny_pkt) const {
|
||||
Wektor2D pkt;
|
||||
pkt.x = x - inny_pkt.x;
|
||||
pkt.y = y - inny_pkt.y;
|
||||
return pkt;
|
||||
}
|
||||
bool operator==(const Wektor2D &inny_pkt) const {
|
||||
if (x != inny_pkt.x || y != inny_pkt.y) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool operator!=(const Wektor2D &inny_pkt) const {
|
||||
return !(*this == inny_pkt);
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Wektor2D &inny_pkt) {
|
||||
os << '(' << inny_pkt.x << ',' << inny_pkt.y << ')';
|
||||
return os;
|
||||
}
|
||||
};
|
||||
11
uczelnia/2_semestr/programowanie/laby/lab8/zad3.cpp
Normal file
11
uczelnia/2_semestr/programowanie/laby/lab8/zad3.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "licznik.cpp"
|
||||
#include "wektor.cpp"
|
||||
|
||||
int main() {
|
||||
Wektor2D a(1, 2), b(3, 4);
|
||||
a += b;
|
||||
std::cout << a << std::endl; // (4,6)
|
||||
Licznik c(10);
|
||||
Licznik d = c++;
|
||||
std::cout << c << " " << d << std::endl; // 11 10
|
||||
}
|
||||
Reference in New Issue
Block a user