mirror of
https://github.com/szymon-jozef/ZUT-notatki.git
synced 2026-10-27 19:20:39 +01:00
bla bla
This commit is contained in:
27
uczelnia/2_semestr/programowanie/laby/lab12/narzedzia.hpp
Normal file
27
uczelnia/2_semestr/programowanie/laby/lab12/narzedzia.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
template <typename T> T wiekszy(T a, T b) { return a > b ? a : b; }
|
||||
|
||||
template <typename T> void zamien(T &a, T &b) {
|
||||
T c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
|
||||
template <typename T> void wypisz(T *tab, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << *(tab + i);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template <typename T> void sortuj3(T a, T b, T c) {
|
||||
std::vector<T> vec{a, b, c};
|
||||
std::sort(vec.begin(), vec.end());
|
||||
|
||||
for (const auto &symbol : vec) {
|
||||
std::cout << symbol;
|
||||
}
|
||||
}
|
||||
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad1
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad1
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad10
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad10
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad2
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad2
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad3
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad3
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad4
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad4
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad5
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad5
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad6
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad6
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad7
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad7
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad9
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab12/out/zad9
Executable file
Binary file not shown.
9
uczelnia/2_semestr/programowanie/laby/lab12/zad1.cpp
Normal file
9
uczelnia/2_semestr/programowanie/laby/lab12/zad1.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T> T potroj(T x) { return x * 3; }
|
||||
|
||||
int main() {
|
||||
std::cout << potroj(7) << '\n';
|
||||
std::cout << potroj(2.5) << '\n';
|
||||
std::cout << potroj('A') << '\n';
|
||||
}
|
||||
13
uczelnia/2_semestr/programowanie/laby/lab12/zad10.cpp
Normal file
13
uczelnia/2_semestr/programowanie/laby/lab12/zad10.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "narzedzia.hpp"
|
||||
|
||||
int main() {
|
||||
int a = 2, b = 1, c = 4;
|
||||
sortuj3(a, b, c);
|
||||
std::cout << std::endl;
|
||||
double i = 1.5, j = 0, k = 14.234;
|
||||
sortuj3(i, j, k);
|
||||
std::cout << std::endl;
|
||||
std::string x = "abc", y = "cde", z = "gde";
|
||||
sortuj3(x, y, z);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
10
uczelnia/2_semestr/programowanie/laby/lab12/zad2.cpp
Normal file
10
uczelnia/2_semestr/programowanie/laby/lab12/zad2.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
template <typename T> T wiekszy(T a, T b) { return a > b ? a : b; }
|
||||
|
||||
int main() {
|
||||
wiekszy(2, 5); // int int
|
||||
wiekszy(2.0, 5.0); // double double
|
||||
// wiekszy(2, 5.0); // nie uruchomi się, bowiem zadeklarowalilśmy dwa
|
||||
// argumenty tego samego typu, a podajemy dwa różne
|
||||
wiekszy<double>(2, 5.0); // double
|
||||
wiekszy('A', 'Z'); // char
|
||||
}
|
||||
9
uczelnia/2_semestr/programowanie/laby/lab12/zad3.cpp
Normal file
9
uczelnia/2_semestr/programowanie/laby/lab12/zad3.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T> T srednia(T a, T b) { return (a + b) / 2; }
|
||||
|
||||
int main() {
|
||||
std::cout << srednia(3, 4) << std::endl; // co się wypisze? 3
|
||||
std::cout << srednia(3.0, 4.0) << std::endl; // co się wypisze? 3.5
|
||||
std::cout << srednia<double>(3, 4.0) << std::endl; // 3.5
|
||||
}
|
||||
21
uczelnia/2_semestr/programowanie/laby/lab12/zad4.cpp
Normal file
21
uczelnia/2_semestr/programowanie/laby/lab12/zad4.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T> void zamien(T &a, T &b) {
|
||||
T c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
|
||||
template <typename T> void zamiana_i_wyswietlenie(T &x, T &y) {
|
||||
zamien(x, y); // teraz x == 2, y == 1
|
||||
std::cout << x << ',' << y << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int x = 2, y = 4;
|
||||
zamiana_i_wyswietlenie(x, y);
|
||||
double i = 2.5, j = 3.5;
|
||||
zamiana_i_wyswietlenie(i, j);
|
||||
std::string a = "hej", b = "ho";
|
||||
zamiana_i_wyswietlenie(a, b);
|
||||
}
|
||||
14
uczelnia/2_semestr/programowanie/laby/lab12/zad5.cpp
Normal file
14
uczelnia/2_semestr/programowanie/laby/lab12/zad5.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T> T minZ3(T a, T b, T c) {
|
||||
return a < b ? (a < c ? a : c) : (b < c ? b : c);
|
||||
}
|
||||
|
||||
int main() {
|
||||
if (minZ3(7, 2, 5) == 2) {
|
||||
std::cout << "1 ok\n";
|
||||
}
|
||||
if (minZ3(3.5, 1.2, 4.8) == 1.2) {
|
||||
std::cout << "2 ok\n";
|
||||
}
|
||||
}
|
||||
17
uczelnia/2_semestr/programowanie/laby/lab12/zad6.cpp
Normal file
17
uczelnia/2_semestr/programowanie/laby/lab12/zad6.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T> int policz(const T *tab, int n, T wartosc) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (*(tab + i) == wartosc)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int dane[] = {1, 2, 3, 2, 2, 5};
|
||||
if (policz(dane, 6, 2) == 3) {
|
||||
std::cout << "Zgadza się :D\n";
|
||||
}
|
||||
}
|
||||
13
uczelnia/2_semestr/programowanie/laby/lab12/zad7.cpp
Normal file
13
uczelnia/2_semestr/programowanie/laby/lab12/zad7.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T, int N> void powtorz(const T &wartosc) {
|
||||
for (int i = 0; i < N; i++) {
|
||||
std::cout << wartosc;
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
powtorz<char, 5>('*'); // *****
|
||||
powtorz<int, 3>(7); // 7 7 7
|
||||
}
|
||||
11
uczelnia/2_semestr/programowanie/laby/lab12/zad8.cpp
Normal file
11
uczelnia/2_semestr/programowanie/laby/lab12/zad8.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
// Fragment A
|
||||
template <typename T> T podwoj(T x) { return x + x; }
|
||||
podwoj(2, 3); // funkcja przyjmuje jeden argument a jest wywołana z dwoma
|
||||
|
||||
// Fragment B
|
||||
template <typename T> T iloczyn(T a, T b);
|
||||
// definicja w innym pliku .cpp - kod się nie linkuje
|
||||
|
||||
// Fragment C
|
||||
template <typename T> T wiekszy(T a, T b) { return a > b ? a : b; }
|
||||
wiekszy("abc", 5); // musi być dwa razy ten sam typ danych, a nie dwa różne
|
||||
21
uczelnia/2_semestr/programowanie/laby/lab12/zad9.cpp
Normal file
21
uczelnia/2_semestr/programowanie/laby/lab12/zad9.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "narzedzia.hpp"
|
||||
|
||||
int main() {
|
||||
std::cout << wiekszy(2, 4) << std::endl;
|
||||
std::cout << wiekszy(2.2, 4.3) << std::endl;
|
||||
std::cout << wiekszy("abc", "cdf") << std::endl;
|
||||
|
||||
std::cout << "\n===\n";
|
||||
|
||||
int x = 2, y = 4;
|
||||
zamien(x, y);
|
||||
std::cout << x << ',' << y;
|
||||
|
||||
double i = 2.5, j = 3.5;
|
||||
zamien(i, j);
|
||||
std::cout << i << ',' << j;
|
||||
|
||||
std::string a = "abc", b = "cde";
|
||||
zamien(a, b);
|
||||
std::cout << a << ',' << b;
|
||||
}
|
||||
Reference in New Issue
Block a user