Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit82efd14cbeffddcc830bfecbb8a6c85434e2d1b1 (patch)
Author
 Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com>
2022-05-06 21:01:35 +0300
Committer
 GitHub <noreply@github.com>
2022-05-06 21:01:35 +0300
Filesb3a8f1510689a17c51b94a72b655cd70b435a8cc
Object-oriented_programming_FMI-82efd14cbeffddcc830bfecbb8a6c85434e2d1b1.tar
Object-oriented_programming_FMI-82efd14cbeffddcc830bfecbb8a6c85434e2d1b1.tar.gz
Object-oriented_programming_FMI-82efd14cbeffddcc830bfecbb8a6c85434e2d1b1.zip
Parent03c61e4c635aed6941c7841e85417def036434b6 (diff)
Add files via upload
diff --git a/допълнително упражнение/функции от по - висок ред/demo.cpp b/допълнително упражнение/функции от по - висок ред/demo.cpp
new file mode 100644
index 0000000..f481f05
--- /dev/null
+++ b/допълнително упражнение/функции от по - висок ред/demo.cpp
@@ -0,0 +1,89 @@
+#include<iostream>
+#include<vector>
+
+template<class T>
+class Entity {
+private:
+ int integerData;
+ char symbolData;
+ T abstractData;
+
+public:
+ Entity(int iArg, char cArg, T aArg) : integerData(iArg), symbolData(cArg), abstractData(aArg) {}
+
+ int getIntegerData() const {
+ return integerData;
+ }
+
+ char getCharData() const {
+ return symbolData;
+ }
+
+ const T& getAbstractData() const {
+ return abstractData;
+ }
+
+ void print() const {
+ std::cout << integerData << " " << symbolData << " " << abstractData << std::endl;
+ }
+};
+
+template<class T>
+bool compareIntegerValues(const Entity<T>& fst, const Entity<T>& snd) {
+ return fst.getIntegerData() < snd.getIntegerData();
+}
+
+template<class T>
+bool compareCharValues(const Entity<T>& fst, const Entity<T>& snd) {
+ return fst.getCharData() < snd.getCharData();
+}
+
+template<class T>
+bool compareAbstractValues(const Entity<T>& fst, const Entity<T>& snd) {
+ return fst.getAbstractData() < snd.getAbstractData();
+}
+
+template<class T>
+void sort(std::vector<Entity<T>>& collection, bool (*lessThan)(const Entity<T>&, const Entity<T>&)) {
+ for (size_t i = 0; i < collection.size(); i++) {
+ size_t minElementIndex = i;
+
+ for (size_t j = i + 1; j < collection.size(); j++) {
+ if (lessThan(collection[j], collection[minElementIndex])) {
+ minElementIndex = j;
+ }
+ }
+
+ if (minElementIndex != i)
+ std::swap(collection[i], collection[minElementIndex]);
+ }
+}
+
+char generateRandomLetter() {
+ return (char)(rand() % 26 + 'a');
+}
+
+double generateRandomDouble() {
+ return (double)rand() / RAND_MAX + rand() % 256;
+}
+
+int main() {
+ std::vector<Entity<double>> collection;
+
+ for (size_t i = 0; i < 100; i++)
+ collection.push_back({ rand() % 10000, generateRandomLetter(), generateRandomDouble() });
+
+
+ // Sort integer values
+ // sort(collection, compareIntegerValues);
+
+ // Sort char values
+ // sort(collection, compareCharValues);
+
+ // Sort abstract values
+ sort(collection, compareAbstractValues);
+
+
+ for (size_t i = 0; i < 100; i++)
+ collection[i].print();
+} \ No newline at end of file