commit 8195919a209cbf6bd058a0a857a7ee54a8aa1efa Author: Ivsucram Date: Tue Oct 29 22:26:00 2024 +0800 Tutorial 8 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a9c225 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea +/T8Q1/.idea +/T8Q1/cmake-build-debug +/T8Q2/.idea +/T8Q2/cmake-build-debug diff --git a/T8Q1/CMakeLists.txt b/T8Q1/CMakeLists.txt new file mode 100644 index 0000000..4a947a7 --- /dev/null +++ b/T8Q1/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.29) +project(T8Q1) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(T8Q1 main.cpp) diff --git a/T8Q1/main.cpp b/T8Q1/main.cpp new file mode 100644 index 0000000..2e432b0 --- /dev/null +++ b/T8Q1/main.cpp @@ -0,0 +1,57 @@ +// +// Created by Marcus Vinícius de Carvalho. +// + +#include +using namespace std; + +class BubbleSort { + private: + //Optional, because member attributes are private by default in C++ + int _size; + int *_numArray; + + public: + BubbleSort(int nums[], int size) { + _size = size; + _numArray = nums; + } + + ~BubbleSort() { + // This is a destructor. It was introduced in C++ 20. If you are programming on an older version, you will + // have to manually clean the memory + } + + void sort() { + int t; + for (int i = _size - 2; i >= 0; i--) { + for (int j = 0; j <= i; j++) { + if (_numArray[j] > _numArray[j + 1]) { + t = _numArray[j]; + _numArray[j] = _numArray[j + 1]; + _numArray[j + 1] = t; + } + } + } + } +}; + +int main() { + int a[100], n, i; + + std::cout << "Enter number of Integers elements to be sorted: "; + std::cin >> n; + + for (i = 0; i < n; i++) { + std::cout << "Enter integer value for elements no." << i + 1 << ": "; + std::cin >> a[i]; + } + + BubbleSort b(a, n); + b.sort(); + + std::cout << "\nFinally sorted array is: "; + for (i = 0; i < n; i++) { + std::cout << a[i] << " "; + } +} \ No newline at end of file diff --git a/T8Q2/CMakeLists.txt b/T8Q2/CMakeLists.txt new file mode 100644 index 0000000..4ba88f0 --- /dev/null +++ b/T8Q2/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.29) +project(T8Q2) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(T8Q2 main.cpp) diff --git a/T8Q2/main.cpp b/T8Q2/main.cpp new file mode 100644 index 0000000..f0324c1 --- /dev/null +++ b/T8Q2/main.cpp @@ -0,0 +1,84 @@ +// +// Created by Marcus Vinicius de Carvalho +// + +#include +#include +#include + +using namespace std; +enum KindOfPolygon {POLY_PLAIN, POLY_RECT, POLY_TRIANG}; + +inline string StringKindofPolygon[] = {"POLY_PLAIN", "POLY_RECT", "POLY_TRIANG"}; + +class Polygon { + +protected: + string name; + float width; + float height; + KindOfPolygon polytype; + +public: + Polygon(string name, float width, float height) : name(std::move(name)), width(width), height(height) { + polytype = POLY_PLAIN; + } + + KindOfPolygon getPolytype() { + return polytype; + } + + void setPolytype(KindOfPolygon value) { + polytype = value; + } + + string getName() { + return name; + } + + virtual float calArea() = 0; + + void printWidthHeight() const { + cout << "Width: " << width << "; Height: " << height << endl; + } +}; + +class Rectangle : public Polygon { +public: + Rectangle(string name, float width, float height) : Polygon(std::move(name), width, height) { + polytype = POLY_RECT; + } + + float calArea() override { + return width * height; + } +}; + +class Triangle : public Polygon { +public: + Triangle(string name, float width, float height) : Polygon(std::move(name), width, height) { + polytype = POLY_TRIANG; + } + + float calArea() override { + return width * height * 0.5f; + } +}; + +class TestPolygon { +public: + static void printArea(Polygon& poly) { + float area = poly.calArea(); + cout << "The area of the " << StringKindofPolygon[poly.getPolytype()] << " is " << area << endl; + } +}; + +int main() { + Rectangle rectangle1("Rectangle1", 3.0f, 4.0f); + rectangle1.printWidthHeight(); + TestPolygon::printArea(rectangle1); + + Triangle triangle1("Triangle1", 3.0f, 4.0f); + triangle1.printWidthHeight(); + TestPolygon::printArea(triangle1); +} \ No newline at end of file