Skip to content

Practicum.05: Add 01-03 | Stoyan Yordanov #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Practicum/Week 05/Solutions/Task02-3MI0800550/dumbDynArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "dumbDynArray.hpp"

dumbDynArray::dumbDynArray(const char *str)
{

for (size_t i = 0; i<=strlen(str); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
this->size++;
}
}

this->numsInStr = new(std::nothrow) int[this->size];
int index = 0;
for (size_t i = 0 ; i < strlen(str); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
this->numsInStr[index] = str[i] - '0';
index++;
}
}

std::cout << this->numsInStr[1] << std::endl;
}

dumbDynArray::~dumbDynArray()
{
delete[] numsInStr;
}
13 changes: 13 additions & 0 deletions Practicum/Week 05/Solutions/Task02-3MI0800550/dumbDynArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <iostream>
#include <cstring>

class dumbDynArray
{
private:
int* numsInStr;
size_t size = 0;
public:
dumbDynArray(const char* str);
~dumbDynArray();
};
10 changes: 10 additions & 0 deletions Practicum/Week 05/Solutions/Task02-3MI0800550/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include "dumbDynArray.hpp"

int main()
{
dumbDynArray d("helo1234");


return 0;
}
174 changes: 174 additions & 0 deletions Practicum/Week 05/Solutions/Task03-3MI0800550/Vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include "Vector.hpp"

size_t NextPowerOfTwo(size_t x)
{
if ((x != 0) && ((x & (x - 1)) == 0))
return x;

unsigned int count = 0;

while (x != 0)
{
x >>= 1;
count++;
}

return 1 << count;
}

bool IsPowerOfTwo(size_t x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}

Vector::Vector()
{
this->cap = 1;
this->vector = new int[this->cap];
}

Vector::Vector(const size_t cap)
{
this->cap = cap;
this->vector = new (std::nothrow) int[this->cap];
if (this->vector == nullptr)
{
std::cout << "Error" << std::endl;
}
}

Vector::Vector(const size_t cap, int num)
{
if(!IsPowerOfTwo(cap)){
this->cap = NextPowerOfTwo(cap);
} else this->cap = cap;



this->vector = new (std::nothrow) int[this->cap];
if (this->vector == nullptr)
{
std::cout << "Error" << std::endl;
}

this->size = cap;
std::fill(this->vector, this->vector + this->size, num);
}

Vector::~Vector()
{
if (this->vector != nullptr)
{
delete[] this->vector;
}
}

void Vector::PrintVector()
{
std::cout << "<";
for (size_t i = 0; i < this->cap - 1; i++)
{
std::cout << this->vector[i] << ", ";
}
std::cout << this->vector[this->cap - 1];
std::cout << ">" << std::endl;
}

int Vector::operator[](const size_t pos)
{
if (!(pos >= 0 && pos <= this->size))
{
std::cout << "No element in this position!" << std::endl;
}

return this->vector[pos];
}

int Vector::At(size_t pos)
{
if (!(pos >= 0 && pos <= this->size))
{
std::cout << "No element in this position!" << std::endl;
}

return this->vector[pos];
}

void Vector::SetAt(size_t pos, int num)
{
if (!(pos >= 0 && pos <= this->size))
{
std::cout << "Invalid position!" << std::endl;
}

this->vector[pos] = num;
}

void Vector::RemoveAt(size_t pos)
{
if (!(pos >= 0 && pos <= this->size))
{
std::cout << "Invalid position!" << std::endl;
}

for (size_t i = pos; i < this->size; i++)
{
this->vector[i] = this->vector[i + 1];
}
this->size--;
}

void Vector::PushBack(int num)
{

this->size++;

if (this->size >= this->cap)
{
this->Resize(cap * 2);
}

this->vector[this->size - 1] = num;
}

void Vector::Resize(size_t new_size)
{
if (new_size <= this->cap)
{
std::cout << "new size must be more than the last" << std::endl;
return;
}

this->cap = new_size;
}

bool Vector::Empty()
{
if (this->size == 0)
return true;

return false;
}

size_t Vector::Size()
{
return this->size;
}

size_t Vector::Capacity()
{
return this->cap;
}

std::ostream &operator<<(std::ostream &os, const Vector &v)
{
os << "[";
for (size_t i = 0; i < v.size; ++i)
{
os << v.vector[i];
if (i < v.size - 1)
os << ", ";
}
os << "]";
return os;
}
32 changes: 32 additions & 0 deletions Practicum/Week 05/Solutions/Task03-3MI0800550/Vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <algorithm>

class Vector
{
private:
int* vector;
size_t size = 0;
size_t cap = 1;
public:
Vector();
Vector(const size_t size);
Vector(const size_t size, int num);
~Vector();

void Resize(size_t new_size);
int At(size_t pos);
void SetAt(size_t pos, int num);
void RemoveAt(size_t pos);
void PushBack(int num);
bool Empty();
size_t Size();
size_t Capacity();

void PrintVector();

friend std::ostream& operator<<(std::ostream& os, const Vector& v);
int operator[](const size_t pos);

};


16 changes: 16 additions & 0 deletions Practicum/Week 05/Solutions/Task03-3MI0800550/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include "Vector.hpp"

int main()
{
Vector v(5,2);
// v.PushBack(21);

// v.PushBack(123);

std::cout << v.Capacity() << std::endl;
v.SetAt(1, 12);
std::cout << v << std::endl;
std::cout << v[1] << std::endl;
return 0;
}