mirror of
https://github.com/szymon-jozef/ZUT-notatki.git
synced 2026-09-07 12:10:40 +02:00
kod
This commit is contained in:
BIN
uczelnia/2_semestr/programowanie/laby/kol1
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/kol1
Executable file
Binary file not shown.
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Pole_Szymon_Prusiewicz {
|
||||
public:
|
||||
int x, y;
|
||||
Pole_Szymon_Prusiewicz(int x, int y) : x(x), y(y) {
|
||||
cout << x << ' ' << y << endl;
|
||||
}
|
||||
~Pole_Szymon_Prusiewicz() { cout << "koniec zycia" << endl; }
|
||||
void oblicz_pole() { cout << "Pole wynosi: " << x * y << endl; }
|
||||
|
||||
Pole_Szymon_Prusiewicz &operator=(const Pole_Szymon_Prusiewicz &inne_pole) {
|
||||
cout << "Przypisanie" << endl;
|
||||
if (this != &inne_pole) {
|
||||
x = inne_pole.x;
|
||||
y = inne_pole.y;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Pole_Szymon_Prusiewicz p1(1, 2);
|
||||
Pole_Szymon_Prusiewicz p2 = p1;
|
||||
p1.oblicz_pole();
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/1
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/1
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/10
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/10
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/3
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/3
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/4
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/4
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/5
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/5
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/6
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/6
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/7
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/7
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/8
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/8
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/9
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab5/out/9
Executable file
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
int main() {
|
||||
int *ptr = nullptr; // deklarujemy wskaźnik wskazujący na nic
|
||||
ptr = new int(5); // wskaźnik wskazuje teraz na wartość 5. jest ona za
|
||||
// lokowana na stercie
|
||||
// alokowana na stercie
|
||||
std::cout << *ptr << '\n'; // wyświetla 5
|
||||
*ptr = *ptr + 3; // zwiększamy wartość o 3
|
||||
std::cout << *ptr << '\n'; // wyświetla 8
|
||||
|
||||
@@ -1,2 +1,59 @@
|
||||
// Przepisz rozwiązanie zadania 9, używając std::unique_ptr<double[]> zamiast su
|
||||
// rowego wskaźnika. Sprawdź, co musisz zmienić i co działa tak samo.
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
double obliczSrednia(std::unique_ptr<double[]> &tab, int n) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
sum += tab[i];
|
||||
}
|
||||
return sum / n;
|
||||
}
|
||||
|
||||
double znajdzMin(std::unique_ptr<double[]> &tab, int n) {
|
||||
double min = 6, x;
|
||||
for (int i = 0; i < n; i++) {
|
||||
x = tab[i];
|
||||
min = x < min ? x : min;
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
double znajdzMax(std::unique_ptr<double[]> &tab, int n) {
|
||||
double max = 0, x;
|
||||
for (int i = 0; i < n; i++) {
|
||||
x = tab[i];
|
||||
max = x > max ? x : max;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Ile chcesz podać ocen: " << std::endl;
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
std::unique_ptr<double[]> arr = std::make_unique<double[]>(n);
|
||||
|
||||
// wypełniamy tablicę
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << "Podaj ocenę numer " << i + 1 << ' ';
|
||||
std::cin >> arr[i];
|
||||
}
|
||||
double max = znajdzMax(arr, n);
|
||||
double min = znajdzMin(arr, n);
|
||||
double avg = obliczSrednia(arr, n);
|
||||
std::string comment = avg > 3.5 ? "Uczeń zaliczył :D" : "Uczeń oblał D:";
|
||||
std::cout << "=== Oceny ===" << std::endl;
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << arr[i] << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "=== Informacje ===" << std::endl;
|
||||
std::cout << "Największa ocena: " << max << '\n' << "Najmniejsza ocena: " << min << '\n' << "Średnia: " << avg << '\n' << comment << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -3,5 +3,26 @@
|
||||
// wartości krok i zwróć wskaźnik. Pamiętaj, że wywołujący jest odpowiedzialny za del
|
||||
// te[].
|
||||
|
||||
int *generujTableau(int n, int krok);
|
||||
#include <iostream>
|
||||
|
||||
int *generujTableau(int n, int krok) {
|
||||
int *arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
*(arr + i) = krok * i;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
void pokazTableau(int *arr, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << *(arr + i) << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int *arr = generujTableau(5, 3);
|
||||
pokazTableau(arr, 5);
|
||||
}
|
||||
|
||||
// Przykład: generujTableau(5, 3) zwraca {0, 3, 6, 9, 12}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
// Uzupełnij program i sprawdź zachowanie. Wyjaśnij, dlaczego wyniki mogą się różn
|
||||
// ić.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void podwojWartoscia(int x) { x *= 2; }
|
||||
void podwojWskaznikiem(int *p) { *p *= 2; }
|
||||
int *q = new int(5);
|
||||
podwojWartoscia(*q);
|
||||
std::cout << *q << '\n'; // co zostanie wypisane?
|
||||
podwojWskaznikiem(q);
|
||||
std::cout << *q << '\n'; // co zostanie wypisane?
|
||||
delete q;
|
||||
|
||||
int main() {
|
||||
int *q = new int(5);
|
||||
podwojWartoscia(*q);
|
||||
std::cout << *q << '\n'; // co zostanie wypisane? // 5
|
||||
podwojWskaznikiem(q);
|
||||
std::cout << *q << '\n'; // co zostanie wypisane? // 10
|
||||
delete q;
|
||||
// wyniki różnią się, bowiem w podwojeniu wartością mnożymy lokalną kopię argumentu razy 2, a w podwojeniu wskaźnikiem mnożymy wartość przetrzymywaną w miejscu, na które wskazuje wskaźnik
|
||||
}
|
||||
|
||||
@@ -2,8 +2,13 @@
|
||||
// apisz poprawną wersję.
|
||||
|
||||
int *stworzTablice(int n) {
|
||||
int tab[n]; // (1) — co jest nie tak?
|
||||
// int tab[n]; // (1) — co jest nie tak? // alokuje na stosie - błąd
|
||||
int *tab = new int[n]; // alokujemy na stercie
|
||||
for (int i = 0; i < n; i++)
|
||||
tab[i] = i;
|
||||
return tab; // (2) — co jest nie tak?
|
||||
return tab; // (2) — co jest nie tak? // poprzednio zwracaliśmy adres lokalnej zm
|
||||
// ennej, teraz zwracamy adres danych zaalokowanych na stercie
|
||||
// i jest super
|
||||
}
|
||||
|
||||
int main() { return 0; }
|
||||
|
||||
@@ -2,8 +2,16 @@
|
||||
// Upewnij się, że nie musisz pisać delete.
|
||||
|
||||
// Wersja z surowym wskaźnikiem:
|
||||
double *wynik = new double(3.14);
|
||||
std::cout << *wynik << '\n';
|
||||
delete wynik;
|
||||
// double *wynik = new double(3.14);
|
||||
// std::cout << *wynik << '\n';
|
||||
// delete wynik;
|
||||
// Twoja wersja z unique_ptr:
|
||||
// ...
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
int main() {
|
||||
std::unique_ptr<double> wynik = std::make_unique<double>(3.14);
|
||||
std::cout << *wynik << '\n';
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
int main() {
|
||||
auto a = std::make_shared<int>(42);
|
||||
std::cout << a.use_count() << '\n'; // ?
|
||||
std::cout << a.use_count() << '\n'; // 1
|
||||
{
|
||||
auto b = a;
|
||||
std::cout << a.use_count() << '\n'; // ?
|
||||
std::cout << a.use_count() << '\n'; // 2
|
||||
auto c = a;
|
||||
std::cout << a.use_count() << '\n'; // ?
|
||||
std::cout << a.use_count() << '\n'; // 3
|
||||
}
|
||||
std::cout << a.use_count() << '\n'; // ?
|
||||
std::cout << a.use_count() << '\n'; // 1
|
||||
}
|
||||
|
||||
@@ -7,3 +7,61 @@
|
||||
// • poprawnie zwalnia pamięć
|
||||
// Użyj funkcji pomocniczych: obliczSrednia(double* tab, int n),
|
||||
// znajdzMin(double* tab, int n), znajdzMax(double* tab, int n).
|
||||
|
||||
#include <iostream>
|
||||
|
||||
double obliczSrednia(double *tab, int n) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
sum += *(tab + i);
|
||||
}
|
||||
return sum / n;
|
||||
}
|
||||
|
||||
double znajdzMin(double *tab, int n) {
|
||||
double min = 6, x;
|
||||
for (int i = 0; i < n; i++) {
|
||||
x = *(tab + i);
|
||||
min = x < min ? x : min;
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
double znajdzMax(double *tab, int n) {
|
||||
double max = 0, x;
|
||||
for (int i = 0; i < n; i++) {
|
||||
x = *(tab + i);
|
||||
max = x > max ? x : max;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Ile chcesz podać ocen: " << std::endl;
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
double *arr = new double[n];
|
||||
|
||||
// wypełniamy tablicę
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << "Podaj ocenę numer " << i + 1 << ' ';
|
||||
std::cin >> *(arr + i);
|
||||
}
|
||||
double max = znajdzMax(arr, n);
|
||||
double min = znajdzMin(arr, n);
|
||||
double avg = obliczSrednia(arr, n);
|
||||
std::string comment = avg > 3.5 ? "Uczeń zaliczył :D" : "Uczeń oblał D:";
|
||||
std::cout << "=== Oceny ===" << std::endl;
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << *(arr + i) << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "=== Informacje ===" << std::endl;
|
||||
std::cout << "Największa ocena: " << max << '\n' << "Najmniejsza ocena: " << min << '\n' << "Średnia: " << avg << '\n' << comment << std::endl;
|
||||
delete[] arr;
|
||||
arr = nullptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user