diff options
Add files via upload
1 files changed, 89 insertions, 0 deletions
| -rw-r--r-- | допълнително упражнение/функции от по - висок ред/demo.cpp | 89 |
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 |
