diff --git a/seminar01_overload/homework/code/01_myspace/myspace.cpp b/seminar01_overload/homework/code/01_myspace/myspace.cpp new file mode 100644 index 0000000..4d93679 --- /dev/null +++ b/seminar01_overload/homework/code/01_myspace/myspace.cpp @@ -0,0 +1,14 @@ +#include +using std::cout; + +namespace myspace { + void print_n_times(char str[], int n = 10) { + for (int i = 0; i < n; ++i) + cout << str; + } +} + +int main() { + char s[] = "nya\n"; + myspace::print_n_times(s); +} diff --git a/seminar01_overload/homework/code/02_cubev/cubev.cpp b/seminar01_overload/homework/code/02_cubev/cubev.cpp new file mode 100644 index 0000000..01de22e --- /dev/null +++ b/seminar01_overload/homework/code/02_cubev/cubev.cpp @@ -0,0 +1,11 @@ +#include +using std::cout, std::endl; + +int cubeV(int x) { + return x * x * x; +} + +int main() { + int x = 3; + cout << cubeV(x) << endl; +} diff --git a/seminar01_overload/homework/code/03_cuber/cuber.cpp b/seminar01_overload/homework/code/03_cuber/cuber.cpp new file mode 100644 index 0000000..9a61a0a --- /dev/null +++ b/seminar01_overload/homework/code/03_cuber/cuber.cpp @@ -0,0 +1,12 @@ +#include + +using std::cout, std::endl; + +int cubeR(int& x) { + return x * x * x; +} + +int main() { + int x = 3; + cout << cubeR(x) << endl; +} diff --git a/seminar01_overload/homework/code/04_count_letters/count_letters.cpp b/seminar01_overload/homework/code/04_count_letters/count_letters.cpp new file mode 100644 index 0000000..6bf0706 --- /dev/null +++ b/seminar01_overload/homework/code/04_count_letters/count_letters.cpp @@ -0,0 +1,26 @@ +#include +using std::cout, std::endl; + +void count_letters(char str[], int& n_letters, int& n_digits, int& n_other) { + while (*str) { + if (*str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z') { + n_letters += 1; + } + else if (*str >= '0' && *str <= '9') { + n_digits += 1; + } + else { + n_other += 1; + } + str += 1; + } +} + +int main() { + int n_letters = 0, n_digits = 0, n_other = 0; + + char s[] = "1n!2y#3a$"; + count_letters(s, n_letters, n_digits, n_other); + + cout << "letters: " << n_letters << endl << "digits: " << n_digits << endl << "n_other: " << n_other << endl; +} diff --git a/seminar01_overload/homework/code/05_add_price/add_price.cpp b/seminar01_overload/homework/code/05_add_price/add_price.cpp new file mode 100644 index 0000000..37b5baa --- /dev/null +++ b/seminar01_overload/homework/code/05_add_price/add_price.cpp @@ -0,0 +1,20 @@ +#include + +using std::cout, std::endl; + +struct Book { + char title[100]; + int pages; + float price; +}; + +void addPrice(Book& b, float x) { + b.price += x; +} + +int main() { + Book b = {"One Hundred Years of Solitude", 456, 1200}; + float x = 15.6; + addPrice(b, x); + cout << b.price << endl; +} diff --git a/seminar01_overload/homework/code/06_is_expensive/is_expensive.cpp b/seminar01_overload/homework/code/06_is_expensive/is_expensive.cpp new file mode 100644 index 0000000..1d56ff1 --- /dev/null +++ b/seminar01_overload/homework/code/06_is_expensive/is_expensive.cpp @@ -0,0 +1,20 @@ +#include +using std::cout, std::endl; + +struct Book { + char title[100]; + int pages; + float price; +}; + +bool isExpensive(const Book& b) { + if (b.price > 1000) { + return true; + } + return false; +} + +int main() { + Book b = {"One Hundred Years of Solitude", 456, 1200}; + cout << isExpensive(b) << endl; +} diff --git a/seminar01_overload/homework/code/07_vector3f/main.cpp b/seminar01_overload/homework/code/07_vector3f/main.cpp new file mode 100644 index 0000000..bde4776 --- /dev/null +++ b/seminar01_overload/homework/code/07_vector3f/main.cpp @@ -0,0 +1,38 @@ +#include +#include "vector3f.h" +using namespace std; + +int main() { + Vector3f a = {1.0, 2.0, -2.0}; + Vector3f b = {4.0, -1.0, 3.0}; + cout << "a = " << a << endl << "b = " << b << endl; + cout << "a + b = " << a + b << endl; + cout << "a - b = " << a - b << endl; + + cout << "0.5 * a = " << 0.5 * a << endl; + cout << "a * 0.5 = " << a * 0.5 << endl; + + cout << "(a, b) = " << a * b << endl; + + cout << "a / 5 = " << a / 5 << endl; + + cout << "-a = " << -a << endl; + cout << "+a = " << +a << endl; + + cout << "a == b = " << (a == b) << endl; + cout << "a != b = " << (a != b) << endl; + + a += b; + cout << "a += b: " << a << endl; + + a -= b; + cout << "a -= b: " << a << endl; + + a *= 2; + cout << "a *= 2: " << a << endl; + a /= 2; + cout << "a /= 2: " << a << endl; + + normalize(a); + cout << "normalize(a): " << a << " |a| = " << norm(a) << endl; +} diff --git a/seminar01_overload/homework/code/07_vector3f/vector3f.h b/seminar01_overload/homework/code/07_vector3f/vector3f.h new file mode 100644 index 0000000..36ce62f --- /dev/null +++ b/seminar01_overload/homework/code/07_vector3f/vector3f.h @@ -0,0 +1,96 @@ +#pragma once +#include +#include + +struct Vector3f { + float x; + float y; + float z; +}; + +Vector3f operator+(const Vector3f& a, const Vector3f& b) { + Vector3f result = {a.x + b.x, a.y + b.y, a.z + b.z}; + return result; +} + +Vector3f operator-(const Vector3f& a, const Vector3f& b) { + Vector3f result = {a.x - b.x, a.y - b.y, a.z - b.z}; + return result; +} + +Vector3f operator*(const Vector3f& a, float f) { + Vector3f result = {f * a.x, f * a.y, f * a.z}; + return result; +} + +Vector3f operator*(float f, const Vector3f& a) { + Vector3f result = {f * a.x, f * a.y, f * a.z}; + return result; +} + +int operator*(const Vector3f& a, const Vector3f& b) { + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +Vector3f operator/(const Vector3f& a, float f) { + Vector3f result = {a.x / f, a.y / f, a.z / f}; + return result; +} + +Vector3f operator+(const Vector3f& a) { + return a; +} + +Vector3f operator-(const Vector3f& a) { + Vector3f result = {-a.x, -a.y, -a.z}; + return result; +} + +bool operator==(const Vector3f& a, const Vector3f& b) { + if (a.x == b.x && a.y == b.y && a.z == b.z) { + return true; + } + return false; +} + +bool operator!=(const Vector3f& a, const Vector3f& b) { + return not (a == b); +} + +void operator+=(Vector3f& a, const Vector3f& b) { + a = a + b; +} + +void operator-=(Vector3f& a, const Vector3f& b) { + a = a - b; +} + +void operator*=(Vector3f& a, float f) { + a = f * a; +} + +void operator/=(Vector3f& a, float f) { + a = a / f; +} + +float squared_norm(const Vector3f& a) { + return a * a; +} + +float norm(const Vector3f& a) { + return sqrt(squared_norm(a)); +} + +void normalize(Vector3f& a) { + a /= norm(a); +} + +std::istream& operator>>(std::istream& in, Vector3f& a) { + in >> a.x >> a.y >> a.z; + return in; +} + +std::ostream& operator<<(std::ostream& out, const Vector3f& a) { + out << "(" << a.x << ", " << a.y << ", " << a.z << ")"; + return out; +} diff --git a/seminar01_overload/homework/code/complex.h b/seminar01_overload/homework/code/08_complex/complex.h similarity index 100% rename from seminar01_overload/homework/code/complex.h rename to seminar01_overload/homework/code/08_complex/complex.h diff --git a/seminar01_overload/homework/code/complex_image.cpp b/seminar01_overload/homework/code/08_complex/complex_image.cpp similarity index 100% rename from seminar01_overload/homework/code/complex_image.cpp rename to seminar01_overload/homework/code/08_complex/complex_image.cpp diff --git a/seminar01_overload/homework/code/complex_movie.cpp b/seminar01_overload/homework/code/08_complex/complex_movie.cpp similarity index 100% rename from seminar01_overload/homework/code/complex_movie.cpp rename to seminar01_overload/homework/code/08_complex/complex_movie.cpp diff --git a/seminar01_overload/homework/code/08_complex/complex_movie.mp4 b/seminar01_overload/homework/code/08_complex/complex_movie.mp4 new file mode 100644 index 0000000..dbb775c Binary files /dev/null and b/seminar01_overload/homework/code/08_complex/complex_movie.mp4 differ diff --git a/seminar01_overload/homework/code/complex_test.cpp b/seminar01_overload/homework/code/08_complex/complex_test.cpp similarity index 100% rename from seminar01_overload/homework/code/complex_test.cpp rename to seminar01_overload/homework/code/08_complex/complex_test.cpp diff --git a/seminar01_overload/homework/code/08_complex/julia.cpp b/seminar01_overload/homework/code/08_complex/julia.cpp new file mode 100644 index 0000000..9488592 --- /dev/null +++ b/seminar01_overload/homework/code/08_complex/julia.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include "complex.h" +using namespace std; + +struct Color { + unsigned char r, g, b; +}; + +Complex func(Complex z, Complex c) { + return z * z + c; +} + +int main(int argc, char** argv) { + Complex c = {0, 0}; + int n = 20; + int width = 800, height = 800; + float x0 = -2.0f, x1 = 2.0f; + float y0 = -2.0f, y1 = 2.0f; + + if (argc < 4) { + cout << "usage: julia [re] [im] [n]" << endl + << "using default values: 0 + 0i, n = 20 "<< endl; + } + else { + cout << argv[1] << " + " << argv[2] << "i" << endl << "n = " << n << endl; + sscanf(argv[1], "%f", &c.re); + sscanf(argv[2], "%f", &c.im); + sscanf(argv[3], "%d", &n); + } + + Color* data = (Color*)malloc(sizeof(Color) * width * height); + + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + Complex z = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j}; + for (int k = 0; k < n; ++k) { + z = func(z, c); + } + + float r = abs(z.re); + float b = abs(z.im); + if (r > 255) { + r = 255; + } + if (b > 255) { + b = 255; + } + + data[i + width * j].r = 255 - r; + data[i + width * j].g = 0; + data[i + width * j].b = 255 - b; + } + } + + + FILE* file = fopen("julia.ppm", "wb"); + fprintf(file, "P6\n%d %d\n255\n", width, height); + fwrite(data, sizeof(Color), height * width, file); + fclose(file); + + free(data); +} diff --git a/seminar01_overload/homework/code/08_complex/julia_images/-0.4+0.6i.ppm b/seminar01_overload/homework/code/08_complex/julia_images/-0.4+0.6i.ppm new file mode 100644 index 0000000..5bbe88a Binary files /dev/null and b/seminar01_overload/homework/code/08_complex/julia_images/-0.4+0.6i.ppm differ diff --git a/seminar01_overload/homework/code/08_complex/julia_images/-0.7-0.38i.ppm b/seminar01_overload/homework/code/08_complex/julia_images/-0.7-0.38i.ppm new file mode 100644 index 0000000..5b6d46a Binary files /dev/null and b/seminar01_overload/homework/code/08_complex/julia_images/-0.7-0.38i.ppm differ diff --git a/seminar01_overload/homework/code/08_complex/julia_images/-0.8+0.16i.ppm b/seminar01_overload/homework/code/08_complex/julia_images/-0.8+0.16i.ppm new file mode 100644 index 0000000..e11cc0a Binary files /dev/null and b/seminar01_overload/homework/code/08_complex/julia_images/-0.8+0.16i.ppm differ diff --git a/seminar01_overload/homework/code/08_complex/julia_images/0.28+0.011i.ppm b/seminar01_overload/homework/code/08_complex/julia_images/0.28+0.011i.ppm new file mode 100644 index 0000000..510b0aa Binary files /dev/null and b/seminar01_overload/homework/code/08_complex/julia_images/0.28+0.011i.ppm differ diff --git a/seminar01_overload/homework/code/08_complex/julia_movie.cpp b/seminar01_overload/homework/code/08_complex/julia_movie.cpp new file mode 100644 index 0000000..fd5ba3a --- /dev/null +++ b/seminar01_overload/homework/code/08_complex/julia_movie.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include "complex.h" +using namespace std; + +struct Color { + unsigned char r, g, b; +}; + +Complex func(Complex z, Complex c) { + return z * z + c; +} + + +int main() { + int width = 800, height = 800; + float x0 = -2.0f, x1 = 2.0f; + float y0 = -2.0f, y1 = 2.0f; + int n = 20; + + Color* data = (Color*)malloc(sizeof(Color) * width * height); + + int max_time_steps = 500; + for (int time = 0; time < max_time_steps; time++) + { + Complex c = {-1.5 + (1.5 / max_time_steps) * time, -1.5 + (1.5 / max_time_steps) * time + 1}; + for (int j = 0; j < height; j++) + { + for (int i = 0; i < width; i++) + { + Complex z = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j}; + for (int k = 0; k < n; ++k) { + z = func(z, c); + } + + float r = abs(z.re); + float b = abs(z.im); + if (r > 255) { + r = 255; + } + if (b > 255) { + b = 255; + } + data[i + width * j].r = 255 - r; + data[i + width * j].g = 0; + data[i + width * j].b = 255 - b; + } + } + + char filename[100]; + sprintf(filename, "animation/complex_%03d.ppm", time); + + FILE* file = fopen(filename, "wb"); + fprintf(file, "P6\n%d %d\n255\n", width, height); + fwrite(data, sizeof(Color), height * width, file); + fclose(file); + } + free(data); +} diff --git a/seminar01_overload/homework/code/08_complex/mandelbrot.cpp b/seminar01_overload/homework/code/08_complex/mandelbrot.cpp new file mode 100644 index 0000000..6e3eef0 --- /dev/null +++ b/seminar01_overload/homework/code/08_complex/mandelbrot.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include "complex.h" +using namespace std; + +struct Color { + unsigned char r, g, b; +}; + +Complex func(Complex z, Complex c) { + return z * z + c; +} + +int main(int argc, char** argv) { + int n = 20; + Complex z = {0, 0}; + int width = 800, height = 800; + float x0 = -2.0f, x1 = 2.0f; + float y0 = -2.0f, y1 = 2.0f; + + + Color* data = (Color*)malloc(sizeof(Color) * width * height); + + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + z = {0, 0}; + Complex c = {x0 + (x1-x0) / width * i, y0 + (y1-y0) / width * j}; + for (int k = 0; k < n; ++k) { + z = func(z, c); + } + + float r = abs(z.re); + float b = abs(z.im); + if (r > 255) { + r = 255; + } + if (b > 255) { + b = 255; + } + data[i + width * j].r = 255 - r; + data[i + width * j].g = 0; + data[i + width * j].b = 255 - b; + } + } + + + FILE* file = fopen("mandelbrot.ppm", "wb"); + fprintf(file, "P6\n%d %d\n255\n", width, height); + fwrite(data, sizeof(Color), height * width, file); + fclose(file); + + free(data); +} diff --git a/seminar01_overload/homework/code/08_complex/mandelbrot_images/mandelbrot.ppm b/seminar01_overload/homework/code/08_complex/mandelbrot_images/mandelbrot.ppm new file mode 100644 index 0000000..6a21dce Binary files /dev/null and b/seminar01_overload/homework/code/08_complex/mandelbrot_images/mandelbrot.ppm differ