From 0779c5261b848a7d973bdaeaeaa97f5b36098010 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 2 Oct 2023 22:26:27 +0200 Subject: [PATCH] Added TD2 and done question 1. Added Windows Support for run script. --- README.md | 11 ++- run.bat | 4 +- td/td2/include/vector.h | 152 ++++++++++++++++++++++++++++++++++++++++ td/td2/src/main.cpp | 21 ++++++ 4 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 td/td2/include/vector.h create mode 100644 td/td2/src/main.cpp diff --git a/README.md b/README.md index 1376731..03e7d47 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,13 @@ Pour cela rendez vous à la racine du projet après avoir crée et préparer le ./run.sh ``` _Pour GNU GCC/G++ sur Linux_ ->`` étant soit `td_insa` ou `exercice_insa` -_le script Windows n'est plus fonctionnel pour le moment_ \ No newline at end of file +``` +./run.bat +``` +_Pour Powershell sur Windows_ +``` +run.bat +``` +_Pour CMD sur Windows_ +>`` étant soit `td_insa` ou `exercice_insa` \ No newline at end of file diff --git a/run.bat b/run.bat index 8ae4f3c..d733b5f 100644 --- a/run.bat +++ b/run.bat @@ -1,5 +1,5 @@ echo off -cmake --build build +cmake --build build --target %1 cd build/Debug -cpp_insa.exe +%1.exe cd ../.. \ No newline at end of file diff --git a/td/td2/include/vector.h b/td/td2/include/vector.h new file mode 100644 index 0000000..bd2b8e4 --- /dev/null +++ b/td/td2/include/vector.h @@ -0,0 +1,152 @@ +#ifndef HEADER_VECTOR +#define HEADER_VECTOR + +#include +#include + +template +class Vector { +public: + Vector(); + Vector(size_t capacity); + Vector(size_t capacity, T value); + Vector(const Vector& other); + ~Vector(); + size_t Size(); + size_t Capacity(); + void PushBack(T value); + template + friend std::ostream& operator<<(std::ostream& stream, const Vector& vec); + template + friend std::istream& operator>>(std::istream& stream, Vector& vec); + + Vector& operator=(const Vector& other); + Vector& operator()(const Vector& other); + T operator[](const int indice); + +private: + size_t _capacity; + size_t _size; + T* _values; +}; + +template +Vector::Vector() + : _capacity(1) + , _size(0) + , _values(new T[1]) +{ +} + +template +Vector::Vector(size_t capacity) + : _capacity(capacity) + , _size(0) +{ + // Allocating the heap with the given capacity + _values = new T[capacity]; +} + +template +Vector::Vector(size_t capacity, T value) + : _capacity(capacity) + , _size(capacity) +{ + // Allocating the heap and setting the value to each element. + _values = new T[capacity]; + for (size_t i = 0; i < capacity; i++) + _values[i] = value; +} + +template +Vector::Vector(const Vector& other) + : _capacity(other._capacity) + , _size(other._size) + , _values(new T[other._capacity]) +{ + for (size_t i = 0; i < other._size; i++) + _values[i] = other._values[i]; +} + +template +Vector::~Vector() +{ + delete[] _values; +} + +template +Vector& Vector::operator=(const Vector& other) +{ + if (this != &other) { + this->_capacity = other._capacity; + this->_size = other._size; + delete[] this->_values; + this->_values = new T[other._capacity]; + for (size_t i = 0; i < other._size; i++) + this->_values[i] = other._values[i]; + } + return *this; +} + +template +Vector& Vector::operator()(const Vector& other) +{ + *this = other; + return *this; +} + +template +size_t Vector::Capacity() { return _capacity; } +template +size_t Vector::Size() { return _size; } + +template +void Vector::PushBack(T value) +{ + // if the next size will overload the heap + if (_capacity < _size + 1) { + // Keep a track of the old pointer + T* oldPtr = _values; + // Creating a new pointer by resizing its capacity by factor 2. + _values = new T[_size * 2]; + _capacity *= 2; + // Copying the old array onto the new + for (size_t i = 0; i < _size; i++) + _values[i] = oldPtr[i]; + // Deallocating the old array + delete[] oldPtr; + } + // Setting the given value and incrementing the size + _values[_size] = value; + _size++; +} + +// methode get +template +std::ostream& operator<<(std::ostream& stream, const Vector& vec) +{ + for (size_t i = 0; i < vec._size; i++) + stream << vec._values[i] << ", "; + return stream; +} + +// methode put +template +std::istream& operator>>(std::istream& stream, Vector& vec) +{ + for (size_t i = 0; i < vec._capacity; i++) + stream >> vec._values[i]; + vec._size = vec._capacity; + return stream; +} + +template +T Vector::operator[](const int indice) +{ + if(indice > _size) + throw "Indice from the vector is not reachable"; + else + return _values[indice]; +} + +#endif // HEADER_VECTOR \ No newline at end of file diff --git a/td/td2/src/main.cpp b/td/td2/src/main.cpp new file mode 100644 index 0000000..5756cc2 --- /dev/null +++ b/td/td2/src/main.cpp @@ -0,0 +1,21 @@ +#include +#include "../include/vector.h" + +int main(void) +{ + Vector intVector; + for(int i=0; i<6; i++) + { + intVector.PushBack(i); + std::cout << intVector[i] << std::endl; + } + try{ + std::cout << intVector[12] << std::endl; + } + catch(const char* expr) + { + std::cout << "Error: " << expr << std::endl; + } + + return 0; +} \ No newline at end of file