Programowanie Sylwester Arabas prowadzący ćwiczenia: Magdalena Kuich, Krzysztof Piasecki, Łukasz Dulny Wydział Fizyki Uniwersytetu Warszawskiego wykład X 29. kwietnia 2015 r.
na ostatnim wykładzie... 1 #include <iostream> 2 3 struct cmplx 4 { 5 double real, imag; 6 7 // konstruktor 8 cmplx() 9 { 10 real = 0; 11 imag = 0; 12 } 13 }; 14 15 int main() 16 { 17 cmplx z; 18 std::cout << "(" 19 << z.real << "," 20 << z.imag 21 << ")" << std::endl; 22 } konstruktory
na ostatnim wykładzie... 1 #include <iostream> 2 3 struct cmplx 4 { 5 double real, imag; 6 7 // konstruktor 8 cmplx() 9 { 10 real = 0; 11 imag = 0; 12 } 13 }; 14 15 int main() 16 { 17 cmplx z; 18 std::cout << "(" 19 << z.real << "," 20 << z.imag 21 << ")" << std::endl; 22 } konstruktory metody wykonywane zaraz po zadeklarowaniu zmiennej
na ostatnim wykładzie... 1 #include <iostream> 2 3 struct cmplx 4 { 5 double real, imag; 6 7 // konstruktor 8 cmplx() 9 { 10 real = 0; 11 imag = 0; 12 } 13 }; 14 15 int main() 16 { 17 cmplx z; 18 std::cout << "(" 19 << z.real << "," 20 << z.imag 21 << ")" << std::endl; 22 } konstruktory metody wykonywane zaraz po zadeklarowaniu zmiennej definicja: metoda bez typu o nazwie takiej jak struktura/klasa
na ostatnim wykładzie... 1 #include <iostream> 2 3 struct cmplx 4 { 5 double real, imag; 6 7 // konstruktor 8 cmplx() 9 { 10 real = 0; 11 imag = 0; 12 } 13 }; 14 15 int main() 16 { 17 cmplx z; 18 std::cout << "(" 19 << z.real << "," 20 << z.imag 21 << ")" << std::endl; 22 } konstruktory metody wykonywane zaraz po zadeklarowaniu zmiennej definicja: metoda bez typu o nazwie takiej jak struktura/klasa użycie: brak arg. kod bez zmian argumenty typ etyk(arg);
na ostatnim wykładzie... 1 #include <iostream> 2 3 struct cmplx 4 { 5 double real, imag; 6 7 // konstruktor 8 cmplx() 9 { 10 real = 0; 11 imag = 0; 12 } 13 }; 14 15 int main() 16 { 17 cmplx z; 18 std::cout << "(" 19 << z.real << "," 20 << z.imag 21 << ")" << std::endl; 22 } konstruktory metody wykonywane zaraz po zadeklarowaniu zmiennej definicja: metoda bez typu o nazwie takiej jak struktura/klasa użycie: brak arg. kod bez zmian argumenty typ etyk(arg); konstruktor kopiujący: tworzony domyślnie dla każdej struktury/klasy
odwołania do wycinków tablic std::valarray
sposoby odwoływania się do elementów tablic std::valarray 1 #include <valarray> 2 #include <iostream> 3 4 template <typename T> 5 void prnt(t &a) 6 { 7 for (auto &i : a) std::cout << i << "\t"; 8 std::cout << std::endl; 9 } 10 11 int main() 12 { 13 std::valarray<double> a = {11,22,33,44}; prnt(a); 14 15 // operacja na całej tablicy 16 a *= 2; prnt(a); 17 18 // operacja na elementach 19 a[0] *= 2; prnt(a); 20 21 // pętla po elementach 22 for (auto &e : a) e *= 2; prnt(a); 23 24 // operacja na wycinku (od, ile, co ile) 25 std::slice i(1,2,1); 26 a[i] = 0; prnt(a); 27 } $./a.out 11 22 33 44 22 44 66 88 44 44 66 88 88 88 132 176 88 0 0 176
generatory liczb losowych (C++11)
generatory liczb losowych w C++11 (<random>) 1 wybrane typy generatorów std::default random engine std::knuth b std::mt19937... arg. konstruktora (unsigned int seed) (unsigned int seed) (unsigned int seed) 1 unsigned int typ int bez znaku, zakres wartości [0,... ]
generatory liczb losowych w C++11 (<random>) 1 wybrane typy generatorów std::default random engine std::knuth b std::mt19937... arg. konstruktora (unsigned int seed) (unsigned int seed) (unsigned int seed) wybrane typy rozkładów arg. konstruktora std::uniform int distribution<t> (int min, int max) std::uniform real distribution<t> (double min, double max) std::poisson distribution<t> (double mean=1) std::normal distribution<t> (double mean=0, double stdev=1)... 1 unsigned int typ int bez znaku, zakres wartości [0,... ]
generatory liczb losowych w C++11 (<random>) 1 wybrane typy generatorów std::default random engine std::knuth b std::mt19937... arg. konstruktora (unsigned int seed) (unsigned int seed) (unsigned int seed) wybrane typy rozkładów arg. konstruktora std::uniform int distribution<t> (int min, int max) std::uniform real distribution<t> (double min, double max) std::poisson distribution<t> (double mean=1) std::normal distribution<t> (double mean=0, double stdev=1)... użycie: liczba = rozkład(generator) 1 unsigned int typ int bez znaku, zakres wartości [0,... ]
generatory liczb losowych w C++11: przykład 1 #include <random> 2 #include <iostream> 3 4 int main() 5 { 6 double mean = 0, stdev = 1; 7 8 std::default_random_engine rng; 9 std::normal_distribution<double> dist(mean, stdev); 10 11 for (int i=0; i < 1000; ++i) 12 std::cout << dist(rng) << std::endl; 13 } $./a.out $ gnuplot gnuplot> plot <./a.out
generatory liczb losowych w C++11: przykład gnuplot 3 '<./a.out' 2 1 0-1 -2-3 -4 0 100 200 300 400 500 600 700 800 900 1000
napiszmy program zastępujący kostkę do gry
kostka do gry w C++ 1 #include <random> 2 3 int main() 4 { 5 6 7 8 std::default_random_engine rng; 9 std::uniform_int_distribution<int> dist(1,6); 10 11 return dist(rng); 12 } $./a.out ; echo $? 1 $./a.out ; echo $? 1
kostka do gry w C++ 1 #include <random> 2 3 int main() 4 { 5 std::random_device rdev; 6 unsigned int seed = rdev(); 7 8 std::default_random_engine rng(seed); 9 std::uniform_int_distribution<int> dist(1,6); 10 11 return dist(rng); 12 } $./a.out ; echo $? 5 $./a.out ; echo $? 4
Dziękuję za uwagę!