diff --git a/Makefile b/Makefile index 8058fac..e450f6f 100755 --- a/Makefile +++ b/Makefile @@ -1,22 +1,34 @@ + 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: + ${BUILD_CMD} src/sheet03.cpp src/MyType.cpp -o build/sheet03 + chmod +x build/sheet03 + build/sheet03 + +sheet04: + ${BUILD_CMD} src/sheet04.cpp -o build/sheet04 + chmod +x build/sheet04 + build/sheet04 + 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/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..3fa9b66 --- /dev/null +++ b/src/MyType.cpp @@ -0,0 +1,9 @@ +#include "../headers/MyType.h" +#include +#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 new file mode 100644 index 0000000..818e2c5 --- /dev/null +++ b/src/sheet03.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include "../headers/MyType.h" + +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) +{ + 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); + 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); + std::cout << '\n'; + pascal_mem(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(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 +#include +class vec +{ + private: + const double nan = std::nan(""); + unsigned vec_size; + double *vector; + public: + 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) : 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) + { + vector[i++] = *itr; + } + } + + ~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