From 82efd14cbeffddcc830bfecbb8a6c85434e2d1b1 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com> Date: Fri, 6 May 2022 21:01:35 +0300 Subject: Add files via upload --- .../demo.cpp" | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 "\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" diff --git "a/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" new file mode 100644 index 0000000..f481f05 --- /dev/null +++ "b/\320\264\320\276\320\277\321\212\320\273\320\275\320\270\321\202\320\265\320\273\320\275\320\276 \321\203\320\277\321\200\320\260\320\266\320\275\320\265\320\275\320\270\320\265/\321\204\321\203\320\275\320\272\321\206\320\270\320\270 \320\276\321\202 \320\277\320\276 - \320\262\320\270\321\201\320\276\320\272 \321\200\320\265\320\264/demo.cpp" @@ -0,0 +1,89 @@ +#include +#include + +template +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 +bool compareIntegerValues(const Entity& fst, const Entity& snd) { + return fst.getIntegerData() < snd.getIntegerData(); +} + +template +bool compareCharValues(const Entity& fst, const Entity& snd) { + return fst.getCharData() < snd.getCharData(); +} + +template +bool compareAbstractValues(const Entity& fst, const Entity& snd) { + return fst.getAbstractData() < snd.getAbstractData(); +} + +template +void sort(std::vector>& collection, bool (*lessThan)(const Entity&, const Entity&)) { + 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> 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 -- cgit v1.3.1-23-gd82d