This commit is contained in:
2026-05-26 15:01:50 +02:00
parent 6a948ffe9f
commit a645291f8b
55 changed files with 1337 additions and 2 deletions

View 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;
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View 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';
}

View 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;
}

View 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
}

View 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
}

View 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);
}

View 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";
}
}

View 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";
}
}

View 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
}

View 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

View 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;
}