Merge branch 'main' of git.libre.moe:Leon/cppcourse
This commit is contained in:
commit
25221cf075
20
Makefile
20
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
|
||||
|
15
headers/MyType.h
Normal file
15
headers/MyType.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef MYTYPE_H
|
||||
#define MYTYPE_H
|
||||
#include <string>
|
||||
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
|
9
src/MyType.cpp
Normal file
9
src/MyType.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "../headers/MyType.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
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;}
|
123
src/sheet03.cpp
Normal file
123
src/sheet03.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#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<std::vector<unsigned long long>> pascal(depth+1, std::vector<unsigned long long>(depth+1,0));
|
||||
pascal[1][1] = 1;
|
||||
for(unsigned long long y=2; y<depth; ++y)
|
||||
{
|
||||
for(unsigned long long x=2; x<=depth; ++x)
|
||||
{
|
||||
pascal[y][x] = pascal[y-1][x-1] + pascal[y-1][x];
|
||||
if(pascal[y][x]!=0)
|
||||
{
|
||||
std::cout << pascal[y][x] << '\t';
|
||||
}
|
||||
}
|
||||
std::cout<< '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void pascal_mem(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2 ) { return; }
|
||||
|
||||
unsigned depth = std::stol(argv[1]);
|
||||
|
||||
if(depth == 1 )
|
||||
{
|
||||
std::cout << "1\n";
|
||||
}
|
||||
else if(depth==0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned vector_depth = (depth*depth-depth)/2;
|
||||
std::vector<unsigned long long> 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<index-1; ++i)
|
||||
{
|
||||
pascal[i+rel_index] = pascal[i] + pascal[i+1];
|
||||
std::cout << pascal[i+rel_index] << '\t';
|
||||
}
|
||||
pascal[next_index-1] = 1;
|
||||
std::cout << pascal[next_index-1] << '\n';
|
||||
++rel_index;
|
||||
}
|
||||
}
|
77
src/sheet04.cpp
Normal file
77
src/sheet04.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include <cstddef>
|
||||
#include <array>
|
||||
#include <initializer_list>
|
||||
#include <cmath>
|
||||
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<size;++i) { vector[i]=ival; }
|
||||
}
|
||||
|
||||
vec(std::initializer_list<double> ilist) : vec_size(ilist.size()), vector(new double[vec_size]) {
|
||||
size_t i = 0;
|
||||
for(std::initializer_list<double>::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; i<vec_size;++i)
|
||||
{
|
||||
vector[i] = m.vector[i];
|
||||
}
|
||||
}
|
||||
|
||||
vec& operator= (const vec &m)
|
||||
{
|
||||
if(this!=&m)
|
||||
{
|
||||
vec_size = m.vec_size;
|
||||
delete[] vector;
|
||||
vector = new double[vec_size];
|
||||
|
||||
for(size_t i = 0; i<vec_size; ++i)
|
||||
{
|
||||
vector[i] = m.vector[i];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec(vec &&m) : vec_size(m.vec_size), vector(new double[vec_size])
|
||||
{
|
||||
vector = std::move(m.vector);
|
||||
}
|
||||
|
||||
vec& operator= (vec &&m)
|
||||
{
|
||||
vec_size = m.vec_size;
|
||||
vector = std::move(m.vector);
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t size()
|
||||
{
|
||||
return vec_size;
|
||||
}
|
||||
|
||||
double& operator[] (size_t idx)
|
||||
{
|
||||
if(idx>vec_size)
|
||||
{
|
||||
return nan;
|
||||
}
|
||||
|
||||
return vector[idx];
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user