Skip to content
Merged
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
131 changes: 131 additions & 0 deletions course03/homework00/balint_hw/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include <iostream>

class Date {
private:
int y, m, d;

bool yearvalid() const { return y >= 1600 && y <= 2500; }

bool monthvalid() const { return m >= 1 && m <= 12; }
static bool isleapyear(int y) {
if (y % 4 != 0) {
return false;
}
if (y % 100 != 0) {
return true;
}
if (y % 400 != 0) {
return false;
}
return true;
}
int maxDayInMonth(int y, int m) const {
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isleapyear(y) ? 29 : 28;
default:
throw std::runtime_error("Invalid month value");
};
}
int dayDiff(Date first, Date second) const {
if (first == second) {
return 0;
}
if (!(first < second)) {
return -1 * dayDiff(second, first);
}

Date dayCnt = first.nextDay();
int cnt = 1;
while (dayCnt < second) {
cnt++;
dayCnt = dayCnt.nextDay();
}
return cnt;
}

public:
Date(int y, int m, int d) : y(y), m(m), d(d){};
Date nextDay() const {
Date firstguess = {y, m, d + 1};
if (!firstguess.isValid()) {
firstguess = {y, m + 1, 1};
if (!firstguess.isValid()) {
firstguess = {y + 1, 1, 1};
}
}
return firstguess;
}
bool isValid() const {
if (!yearvalid() || !monthvalid()) {
return false;
}
if (d < 1 || d > maxDayInMonth(y, m)) {
return false;
}
return true;
}
int getY() const { return y; }
int getM() const { return m; }
int getD() const { return d; }

bool operator==(const Date &other) const {
return std::tie(y, m, d) == std::tie(other.y, other.m, other.d);
}
bool operator<(const Date &other) const {
return std::tie(y, m, d) < std::tie(other.y, other.m, other.d);
}
int daysSinceEpoch() const { return dayDiff({1970, 1, 1}, *this); }

std::string dayOfWeek() const {
if (!isValid()) {
throw std::runtime_error("dayOfWeek() called while !isValid()");
}
int offset = (((daysSinceEpoch() % 7) + 7) % 7);
switch (offset) {
case 0:
return "Thursday";
case 1:
return "Friday";
case 2:
return "Saturday";
case 3:
return "Sunday";
case 4:
return "Monday";
case 5:
return "Tuesday";
case 6:
return "Wednesday";
default:
throw std::logic_error("Shouldn't happen");
}
}
};
std::ostream &operator<<(std::ostream &strm, const Date &a) {
return strm << a.getY() << "-" << a.getM() << "-" << a.getD();
}

int main() {

Date test2 = {2019, 5, 5};

std::cout << "Nr of days since epoch is: " << test2.daysSinceEpoch()
<< std::endl;
std::cout << "Day of week is : " << test2.dayOfWeek() << std::endl;

return 0;
}
10 changes: 10 additions & 0 deletions course03/homework01/balint/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.1)

find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g -Wall")
add_executable(main main.cpp)

add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)
131 changes: 131 additions & 0 deletions course03/homework01/balint/llist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include <algorithm>
#include <iostream>
#include <memory>

struct Node
{
int melem;
std::unique_ptr<Node> mnext;

Node() : melem(), mnext(){};
Node(int i) : melem(i), mnext(){};

~Node() { mnext.reset(); }
Node(const Node &other)
{
if (other.melem)
{
melem = other.melem;
}
if (other.mnext)
{
mnext = std::make_unique<Node>(*other.mnext);
}
}
Node &operator=(const Node &other)
{
if (other.melem)
{
melem = other.melem;
}
if (other.mnext)
{
mnext = std::make_unique<Node>(*other.mnext);
}
return *this;
}
};

typedef std::unique_ptr<Node> Lst;

class LLint
{
public:
operator bool() const { return mlist ? mlist->melem ? true : false : false; }
~LLint() { mlist.reset(); }
LLint(const LLint &other) { mlist = std::make_unique<Node>(*other.mlist); }
LLint &operator=(const LLint &other)
{
mlist = other.mlist ? std::make_unique<Node>(*other.mlist) : nullptr;
return *this;
}
LLint(const Lst &lst) { mlist = lst ? std::make_unique<Node>(*lst) : nullptr; }
LLint(Node n) { mlist = std::make_unique<Node>(n); }
Node copyNode() { return Node(*mlist); }
Node copyNodeMnext() { return Node(*(*mlist).mnext); }
static LLint emptyList() { return LLint(nullptr); }
// int operator[](int i) const {return nth(*this, i);}
int car() const
{
if (*this)
{
return (*mlist).melem;
}
else
{
throw std::runtime_error("Car called on empty mlist.");
}
}
LLint cdr() const
{
if (*this)
{
return LLint(mlist->mnext);
}
else
{
return LLint(nullptr);
}
}

Lst mlist;
};

LLint cons(int i, LLint l)
{
// return LLint(*(*l.mlist).mnext);
Node firstNode = Node(i);
Lst restOfList = l.mlist ? std::make_unique<Node>(*l.mlist) : nullptr;
firstNode.mnext = std::move(restOfList);
return firstNode;
}

int nth(const LLint &l, int i)
{
if (i < 0)
{
throw std::runtime_error("Cant index with negative index");
}
if (i == 0)
{
return l.car();
}
else
{
return nth(l.cdr(), i - 1);
}
}

LLint pushBack(const LLint &l, int i)
{
if (l)
{
return cons(l.car(), pushBack(l.cdr(), i));
}
else
{
return LLint(i);
}
}

int len(const LLint &l)
{
if (l)
{
return 1 + len(l.cdr());
}
else
{
return 0;
}
}
39 changes: 39 additions & 0 deletions course03/homework01/balint/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "llist.cpp"

// TESTS

int main()
{
int i;
Node n = {3};
Node m = {4};
Lst tmp = std::make_unique<Node>(m);
Lst tmp2 = std::make_unique<Node>(n);
Node m2 = m;
Node m3 = Node(m);
// m2.mnext = std::move(m3);
m2.mnext = std::make_unique<Node>(m3);
LLint ls1 = LLint(m3);
LLint ls3 = LLint(n);
LLint ls2 = ls1;
ls2.mlist->mnext = std::move(ls3.mlist);
// LLint ls2_cad = cad(ls2);
// std::cout<<"car of ls2: "<<ls2.car()<<std::endl;

LLint consed = cons(100, cons(99, ls1));
std::cout << "First elem is: " << consed.car() << std::endl;
std::cout << "Second elem is: " << consed.cdr().car() << std::endl;
std::cout << "Third elem is: " << consed.cdr().cdr().car() << std::endl;
// Lst n;
// Lst n = Node(1, Node());
// Node n = Node();
if (ls2)
{
i = 0;
}
else
{
i = 1;
}
std::cout << "Node is: " << i << std::endl;
}
Loading