Tutorial 8

This commit is contained in:
Marcus Vinicius de Carvalho 2024-10-29 22:26:00 +08:00
commit 8195919a20
5 changed files with 158 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.idea
/T8Q1/.idea
/T8Q1/cmake-build-debug
/T8Q2/.idea
/T8Q2/cmake-build-debug

6
T8Q1/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.29)
project(T8Q1)
set(CMAKE_CXX_STANDARD 20)
add_executable(T8Q1 main.cpp)

57
T8Q1/main.cpp Normal file
View File

@ -0,0 +1,57 @@
//
// Created by Marcus Vinícius de Carvalho.
//
#include <iostream>
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] << " ";
}
}

6
T8Q2/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.29)
project(T8Q2)
set(CMAKE_CXX_STANDARD 20)
add_executable(T8Q2 main.cpp)

84
T8Q2/main.cpp Normal file
View File

@ -0,0 +1,84 @@
//
// Created by Marcus Vinicius de Carvalho
//
#include <iostream>
#include <string>
#include <utility>
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);
}