From cc53df2dab6d2323d16d7e3d1043466c748c04d2 Mon Sep 17 00:00:00 2001 From: Leon Wilzer Date: Tue, 8 Nov 2022 15:41:38 +0100 Subject: [PATCH 1/5] idk --- Makefile | 5 +++++ headers/MyType.h | 15 +++++++++++++++ src/MyType.cpp | 11 +++++++++++ src/sheet03.cpp | 16 ++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 headers/MyType.h create mode 100644 src/MyType.cpp create mode 100644 src/sheet03.cpp diff --git a/Makefile b/Makefile index 8058fac..832c49d 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,11 @@ sheet02: chmod +x build/sheet02 build/sheet02 +sheet03: + g++ src/sheet03.cpp -o ./build/sheet03 + chmod +x build/sheet03 + build/sheet03 + pizza: g++ src/pizza.cpp -o ./build/pizza chmod +x build/pizza diff --git a/headers/MyType.h b/headers/MyType.h new file mode 100644 index 0000000..d33ee14 --- /dev/null +++ b/headers/MyType.h @@ -0,0 +1,15 @@ +#ifndef MYTYPE_H +#define MYTYPE_H +#include +struct MyType{ + std::string name; + unsigned age; + + MyType(std::string n, unsigned a); + ~MyType(); + MyType(const MyType &t); + MyType& operator= (const MyType &t); + MyType(MyType &&t); + MyType& operator= (MyType &&t); +}; +#endif \ No newline at end of file diff --git a/src/MyType.cpp b/src/MyType.cpp new file mode 100644 index 0000000..2703aa5 --- /dev/null +++ b/src/MyType.cpp @@ -0,0 +1,11 @@ +#include "../headers/MyType.h" +#include + std::string name; + unsigned age; + + MyType(std::string n, unsigned a) : name(n), age(a) {std::cout << "ctor\n";} + ~MyType() {} + MyType(const MyType &t) { std::cout << "copy ctor\n";} + MyType& operator= (const MyType &t) { std::cout << "copy ass ctor\n"; return *this;} + MyType(MyType &&t){std::cout << "move ctor\n";} + MyType& operator= (MyType &&t) { std::cout << "move ass ctor\n"; return t;} \ No newline at end of file diff --git a/src/sheet03.cpp b/src/sheet03.cpp new file mode 100644 index 0000000..af74888 --- /dev/null +++ b/src/sheet03.cpp @@ -0,0 +1,16 @@ +#include +#include +#include "../headers/MyType.h" + + +int main() +{ + MyType j("jj", 69); + MyType cj(j); + MyType caj("jj",420); + caj = j; + MyType mj(std::move(j)); + MyType maj("jj", 69); + maj = std::move(cj); + return 0; +} \ No newline at end of file From c06e72fca1206310d953e4edc9d356f92f89b954 Mon Sep 17 00:00:00 2001 From: Leon Wilzer Date: Thu, 10 Nov 2022 10:41:51 +0100 Subject: [PATCH 2/5] idk --- Makefile | 12 +++++++----- src/MyType.cpp | 16 +++++++--------- src/sheet03.cpp | 12 ++++++++++-- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 832c49d..65f48bc 100644 --- a/Makefile +++ b/Makefile @@ -1,27 +1,29 @@ + export BUILD_CMD=clang++ -Wall -std=c++17 -Wall -Wextra + setup: mkdir build sheet0: - g++ src/sheet0.cpp -o ./build/sheet0 + ${BUILD_CMD} src/sheet00.cpp -o build/sheet0 chmod +x build/sheet0 build/sheet0 sheet01: - g++ src/sheet01.cpp -o ./build/sheet01 + ${BUILD_CMD} src/sheet01.cpp -o build/sheet01 chmod +x build/sheet01 build/sheet01 sheet02: - g++ src/sheet02.cpp -o ./build/sheet02 + ${BUILD_CMD} src/sheet02.cpp -o build/sheet02 chmod +x build/sheet02 build/sheet02 sheet03: - g++ src/sheet03.cpp -o ./build/sheet03 + ${BUILD_CMD} src/sheet03.cpp src/MyType.cpp -o build/sheet03 chmod +x build/sheet03 build/sheet03 pizza: - g++ src/pizza.cpp -o ./build/pizza + ${BUILD_CMD} src/pizza.cpp -o build/pizza chmod +x build/pizza build/pizza diff --git a/src/MyType.cpp b/src/MyType.cpp index 2703aa5..3fa9b66 100644 --- a/src/MyType.cpp +++ b/src/MyType.cpp @@ -1,11 +1,9 @@ #include "../headers/MyType.h" #include - std::string name; - unsigned age; - - MyType(std::string n, unsigned a) : name(n), age(a) {std::cout << "ctor\n";} - ~MyType() {} - MyType(const MyType &t) { std::cout << "copy ctor\n";} - MyType& operator= (const MyType &t) { std::cout << "copy ass ctor\n"; return *this;} - MyType(MyType &&t){std::cout << "move ctor\n";} - MyType& operator= (MyType &&t) { std::cout << "move ass ctor\n"; return t;} \ No newline at end of file +#include + MyType::MyType(std::string n, unsigned a) : name(n), age(a) {std::cout << "ctor\n";} + MyType::~MyType() {std::cout << "dtor \n";} + MyType::MyType(const MyType &t) { std::cout << "copy ctor\n";} + MyType& MyType::operator= (const MyType &t) { std::cout << "copy ass ctor\n"; return *this;} + MyType::MyType(MyType &&t){std::cout << "move ctor\n";} + MyType& MyType::operator= (MyType &&t) { std::cout << "move ass ctor\n"; return t;} \ No newline at end of file diff --git a/src/sheet03.cpp b/src/sheet03.cpp index af74888..55a6435 100644 --- a/src/sheet03.cpp +++ b/src/sheet03.cpp @@ -2,8 +2,12 @@ #include #include "../headers/MyType.h" +int calculate(const char &argv) +{ -int main() +} + +int main(int argc, char **argv) { MyType j("jj", 69); MyType cj(j); @@ -12,5 +16,9 @@ int main() MyType mj(std::move(j)); MyType maj("jj", 69); maj = std::move(cj); + if(argc == 4) + { + calculate(argv); + } return 0; -} \ No newline at end of file +} From da64db914e136f336d79226ae35bfe78cc602f05 Mon Sep 17 00:00:00 2001 From: Leon Wilzer Date: Mon, 14 Nov 2022 12:41:19 +0100 Subject: [PATCH 3/5] idk --- src/sheet03.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/src/sheet03.cpp b/src/sheet03.cpp index 55a6435..7402b1a 100644 --- a/src/sheet03.cpp +++ b/src/sheet03.cpp @@ -1,11 +1,13 @@ #include #include +#include +#include #include "../headers/MyType.h" -int calculate(const char &argv) -{ - -} +double calculate(int argc, char **argv); +void pol2cart(double r, double phi, double &x, double &y); +void pascal(int argc, char **argv); +void pascal_mem(int argc, char **argv); int main(int argc, char **argv) { @@ -16,9 +18,98 @@ int main(int argc, char **argv) MyType mj(std::move(j)); MyType maj("jj", 69); maj = std::move(cj); - if(argc == 4) - { - calculate(argv); - } + std::cout << calculate(argc, argv) << '\n'; + double x = 0; + double y = 0; + pol2cart(0.5, 0, x, y); + std::cout << x << " " << y << '\n'; + pascal(argc, argv); return 0; } + +double calculate(int argc, char **argv) +{ + if(argc != 4) + { + return std::nan(""); + } + + switch(argv[2][0]) + { + case '+': + return std::stod(argv[1]) + std::stod(argv[3]); + case '-': + return std::stod(argv[1]) - std::stod(argv[3]); + case 'x': + return std::stod(argv[1]) * std::stod(argv[3]); + case '/': + return std::stod(argv[1]) / std::stod(argv[3]); + default: + std::cout << "Invalid operand: " << argv[2][0] << '\n'; + return std::nan(""); + } +} + +void pol2cart(double r, double phi, double &x, double &y) +{ + x = r * cos(phi); + y = r * sin(phi); +} + +void pascal(int argc, char **argv) +{ + if (argc != 2 || std::stol(argv[1]) == 0) { return; } + + unsigned depth = std::stol(argv[1]); + + std::vector> pascal(depth+1, std::vector(depth+1,0)); + pascal[1][1] = 1; + for(unsigned long long y=2; y pascal(depth,0); + pascal[0] = 1; + for(unsigned long long i=1; i Date: Wed, 16 Nov 2022 16:25:33 +0100 Subject: [PATCH 4/5] idk --- Makefile | 5 ++++ src/sheet03.cpp | 62 ++++++++++++++++++++++++++++--------------------- src/sheet04.cpp | 32 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 src/sheet04.cpp diff --git a/Makefile b/Makefile index 65f48bc..e450f6f 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,11 @@ sheet03: chmod +x build/sheet03 build/sheet03 +sheet04: + ${BUILD_CMD} src/sheet04.cpp -o build/sheet04 + chmod +x build/sheet04 + build/sheet04 + pizza: ${BUILD_CMD} src/pizza.cpp -o build/pizza chmod +x build/pizza diff --git a/src/sheet03.cpp b/src/sheet03.cpp index 7402b1a..818e2c5 100644 --- a/src/sheet03.cpp +++ b/src/sheet03.cpp @@ -24,6 +24,8 @@ int main(int argc, char **argv) pol2cart(0.5, 0, x, y); std::cout << x << " " << y << '\n'; pascal(argc, argv); + std::cout << '\n'; + pascal_mem(argc, argv); return 0; } @@ -77,39 +79,45 @@ void pascal(int argc, char **argv) std::cout<< '\n'; } } -unsigned fibonacci_nonrec(unsigned n) -{ - unsigned fibo = 0; - unsigned prev_fibo = 1; - unsigned prev_prev_fibo = 0; - for(int i=2; i<=n; ++i) - { - fibo = prev_fibo + prev_prev_fibo; - prev_prev_fibo = prev_fibo; - prev_fibo = fibo; - } - - return fibo; -} void pascal_mem(int argc, char **argv) { - - if (argc != 2 || std::stol(argv[1]) == 0) { return; } + if (argc != 2 ) { return; } unsigned depth = std::stol(argv[1]); - depth = (depth*depth-depth)/2; - std::vector pascal(depth,0); - pascal[0] = 1; - for(unsigned long long i=1; i pascal(vector_depth,0); + pascal[0] = 1; + std::cout << "1\n"; + // pascal[1] = 1; + // pascal[2] = 1; + // std::cout << "1\t1\n"; + unsigned rel_index = 2; + while(rel_index<=depth) + { + unsigned index = (rel_index*rel_index-rel_index)/2; + unsigned prev_index = ((rel_index-1)*(rel_index-1)-(rel_index-1))/2; + unsigned next_index = ((rel_index+1)*(rel_index+1)-(rel_index+1))/2; + pascal[index] = 1; + std::cout << pascal[index] << '\t'; + for(unsigned i=prev_index; i +#include +#include +class vec +{ + private: + unsigned size; + double *vector; + public: + vec(size_t size) : size(size), vector(new double[size]) {} + vec(size_t size, double ival) : size(size), vector(new double[size]) + { + for(size_t i=0; i ilist) : size(ilist.size()), vector(new double[size]) { + size_t i = 0; + for(std::initializer_list::iterator itr = ilist.begin(); itr!=ilist.end();++itr) + { + vector[i++] = *itr; + } + } + + ~vec() {delete[] vector; } + vec(const vec &m) : vector(new double[m.size]) + { + for(size_t i=0; i Date: Fri, 18 Nov 2022 13:58:16 +0100 Subject: [PATCH 5/5] idk --- src/sheet04.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/src/sheet04.cpp b/src/sheet04.cpp index e099105..65a1e9f 100644 --- a/src/sheet04.cpp +++ b/src/sheet04.cpp @@ -1,19 +1,21 @@ #include #include #include +#include class vec { private: - unsigned size; + const double nan = std::nan(""); + unsigned vec_size; double *vector; public: - vec(size_t size) : size(size), vector(new double[size]) {} - vec(size_t size, double ival) : size(size), vector(new double[size]) + vec(size_t size) : vec_size(size), vector(new double[size]) {} + vec(size_t size, double ival) : vec_size(size), vector(new double[size]) { for(size_t i=0; i ilist) : size(ilist.size()), vector(new double[size]) { + vec(std::initializer_list ilist) : vec_size(ilist.size()), vector(new double[vec_size]) { size_t i = 0; for(std::initializer_list::iterator itr = ilist.begin(); itr!=ilist.end();++itr) { @@ -21,12 +23,55 @@ class vec } } - ~vec() {delete[] vector; } - vec(const vec &m) : vector(new double[m.size]) + ~vec() {delete[] vector;} + vec(const vec &m) : vec_size(m.vec_size), vector(new double[vec_size]) { - for(size_t i=0; ivec_size) + { + return nan; + } + + return vector[idx]; + } }; \ No newline at end of file