seminar_01
This commit is contained in:
		
							parent
							
								
									2369f801af
								
							
						
					
					
						commit
						46d1c64684
					
				
					 21 changed files with 415 additions and 0 deletions
				
			
		
							
								
								
									
										14
									
								
								seminar01_overload/homework/code/01_myspace/myspace.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								seminar01_overload/homework/code/01_myspace/myspace.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | ||||||
|  | #include <iostream> | ||||||
|  | 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); | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								seminar01_overload/homework/code/02_cubev/cubev.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								seminar01_overload/homework/code/02_cubev/cubev.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | #include <iostream> | ||||||
|  | using std::cout, std::endl; | ||||||
|  | 
 | ||||||
|  | int cubeV(int x) { | ||||||
|  |     return x * x * x; | ||||||
|  | }  | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |     int x = 3; | ||||||
|  |     cout << cubeV(x) << endl; | ||||||
|  | } | ||||||
							
								
								
									
										12
									
								
								seminar01_overload/homework/code/03_cuber/cuber.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								seminar01_overload/homework/code/03_cuber/cuber.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | ||||||
|  | #include <iostream> | ||||||
|  | 
 | ||||||
|  | using std::cout, std::endl; | ||||||
|  | 
 | ||||||
|  | int cubeR(int& x) { | ||||||
|  |     return x * x * x; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |    int x = 3; | ||||||
|  |    cout << cubeR(x) << endl;   | ||||||
|  | } | ||||||
|  | @ -0,0 +1,26 @@ | ||||||
|  | #include <iostream> | ||||||
|  | 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; | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								seminar01_overload/homework/code/05_add_price/add_price.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								seminar01_overload/homework/code/05_add_price/add_price.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | ||||||
|  | #include <iostream> | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | } | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | #include <iostream> | ||||||
|  | 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; | ||||||
|  | } | ||||||
							
								
								
									
										38
									
								
								seminar01_overload/homework/code/07_vector3f/main.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								seminar01_overload/homework/code/07_vector3f/main.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,38 @@ | ||||||
|  | #include <iostream> | ||||||
|  | #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; | ||||||
|  | } | ||||||
							
								
								
									
										96
									
								
								seminar01_overload/homework/code/07_vector3f/vector3f.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								seminar01_overload/homework/code/07_vector3f/vector3f.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,96 @@ | ||||||
|  | #pragma once | ||||||
|  | #include <cmath> | ||||||
|  | #include <iostream> | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | } | ||||||
							
								
								
									
										
											BIN
										
									
								
								seminar01_overload/homework/code/08_complex/complex_movie.mp4
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								seminar01_overload/homework/code/08_complex/complex_movie.mp4
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										64
									
								
								seminar01_overload/homework/code/08_complex/julia.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								seminar01_overload/homework/code/08_complex/julia.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,64 @@ | ||||||
|  | #include <cstdio> | ||||||
|  | #include <cstdlib> | ||||||
|  | #include <cmath> | ||||||
|  | #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); | ||||||
|  | } | ||||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										60
									
								
								seminar01_overload/homework/code/08_complex/julia_movie.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								seminar01_overload/homework/code/08_complex/julia_movie.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,60 @@ | ||||||
|  | #include <cstdio> | ||||||
|  | #include <cstdlib> | ||||||
|  | #include <cmath> | ||||||
|  | #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); | ||||||
|  | } | ||||||
							
								
								
									
										54
									
								
								seminar01_overload/homework/code/08_complex/mandelbrot.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								seminar01_overload/homework/code/08_complex/mandelbrot.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | ||||||
|  | #include <cstdio> | ||||||
|  | #include <cstdlib> | ||||||
|  | #include <cmath> | ||||||
|  | #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); | ||||||
|  | } | ||||||
										
											Binary file not shown.
										
									
								
							
		Reference in a new issue