Informatik I (D-ITET) Übungsstunde 12, 11.12.2017 Hossein Shafagh shafagh@inf.ethz.ch Self-Assessment now!
Task 11.1 Finite rings a) struct int_7 { ; int value; //INV: 0<= value <=6 b) // POST : return value is the sum of a and b int_7 operator+ (int_7 a, int_7 b) { int help; help = a.value + b.value; int_7 r; r.value = help%7; return r;
Task 11.1 Finite rings c) // POST : return value is the difference of a and b int_7 operator- (int_7 a, int_7 b) { int_7 r; if (a.value >= b.value) { int help; r.value = a.value - b.value; return r; help = a.value - b.value; r.value = 7 + help; return r;
Task 11.2a: Complex numbers // represents complex numbers in cartesian form struct Complex { ; double real; double imag; // real part // imaginary part
Task 11.2a: Complex numbers // complex number output std::ostream& operator << (std::ostream& out, const Complex number) { return out << '[' << number.real << ',' << number.imag << ']'; // complex number input std::istream& operator >> (std::istream& in, Complex& number) { unsigned char f1, f2, f3; return in >> f1 >> c.r >> f2 >> c.i >> f3;
Task 11.2a: Complex numbers // complex number addition Complex operator + (const Complex a, const Complex b){ Complex result; result.real = a.real + b.real; result.imag = a.imag + b.imag; return result; // complex number subtraction Complex operator - (const Complex a, const Complex b){ Complex result; result.real = a.real - b.real; result.imag = a.imag - b.imag; return result; // complex number multiplication Complex operator * (const Complex a, const Complex b){ Complex result; result.real = a.real * b.real - a.imag * b.imag; result.imag = a.imag * b.real + a.real * b.imag; return result;
Task 11.2a: Complex numbers // complex number division Complex operator / (const Complex a, const Complex b) { Complex result; const float divisor = b.real * b.real + b.imag * b.imag; result.real = (a.real * b.real + a.imag * b.imag) / divisor; result.imag = (a.imag * b.real - a.real * b.imag) / divisor; return result; Complex operator - (const Complex c) { Complex result; result.real = - c.real; result.imag = - c.imag; return result; // complex number equality bool operator == (const Complex a, const Complex b){ return a.real == b.real && a.imag == b.imag; // complex number inequality bool operator!= (const Complex a, const Complex b){ return!(a == b);
1 A class for vectors class Vector { private: // already private, keyword only used to emphasize ; double x; double y;
1 A class for vectors (constructor) class Vector { private: // already private, keyword only used to emphasize double x; double y; ; public: // now switch to public the class initialization list Vector () : x(0), y(0) { Vector (const double _x, const double _y) : x(_x), y(_y) { Vector v1; Vector v2(3,4);
1 A class for vectors (getter & setter) class Vector { private: // already private, keyword only used to emphasize double x; double y; public: // now switch to public Vector () : x(0), y(0) { Vector (const double _x, const double _y) : x(_x), y(_y) { ; modify the object via this double get_x() const {return x; double get_y() const {return y; Vector a(3,4); double x = a.get_x(); double y = a.get_y();
1 A class for vectors (getter & setter) class Vector { private: // already private, keyword only used to emphasize double x; double y; public: // now switch to public Vector () : x(0), y(0) { Vector (const double _x, const double _y) : x(_x), y(_y) { double get_x() const {return x; double get_y() const {return y; ; void set_x(const double _x) {x = _x; void set_y(const double _y) {y = _y;
1 A class for vectors (Arithmetic operators) class Vector { ; // y += z, the first argument passed implicitly, in this case y Vector& operator+= (const Vector& b); Vector v1; Vector v2(3,4); v2 += v1; Vector& Vector::operator+= (const Vector& b) { x += b.get_x(); y += b.get_y(); return *this;
1 A class for vectors (Arithmetic operators) class Vector { ; // implicit conversion using a constructor with only x component // add to class: Vector (const double _x) : x(_x), y(0) { Vector v1; Vector v2(3,4); v2 += v1; Vector v4 = v1 + 3; // implicit conversion Vector operator+ (const Vector& a, const Vector& b) { Vector res = a; res += b; return res;
1 A class for vectors (output) #include <iostream> std::ostream& operator<< (std::ostream& out, const Vector v) { return out << v.get_x() <<, << v.get_y(); Vector v1; Vector v2(3,4); v2 += v1; Vector v4 = v1 + 3; // implicit conversion const Vector& v5 = v1; std::cout << v1 << "\n" << v5 << "\n";
2 Dynamically allocated memory int* dyn_int = new int (3); // constructed with value 3 int n =...; int* dyn_int_range = new int[n]; delete dyn_int; // deconstruct dynamic variable dyn_int = 0; delete[] dyn_int_range; // deconstruct dynamic range dyn_int_range = 0;
2 Dynamically allocated memory //... int i; std::cin >> i; int* mem = new int[i]; // length of input for (int j = 0; j < i; ++j) std::cin >> *(mem + j); for (int j = i-1; j >= 0; --j) std::cout << *(mem + j) << " "; delete[] mem; mem = 0; //...
3 Dynamic Data Types stack s1; s1.push(1); s1.push(3); s1.push(2); stack s2(s1);
3 Dynamic Data Types stack::stack(const stack& s) : top_node (0) { copy(s.top_node, top_node); void stack::copy(const linked_list_node* from, linked_list_node*& to) { assert (to == 0); if (from!= 0) { to = new linked_list_node(from->key); copy(from->next, to->next);
3 Dynamic Data Types stack s2; s2.push(4); s2.push(9); s2 = s1; // s1 as before
3 Dynamic Data Types stack s2; s2.push(4); s2.push(9); s2 = s1; // s1 as before // overwrite operator= // re-use copy function as before and afterwards free memory void stack::clear(linked_list_node* from) { if (from!= 0) { clear (from->next); delete from;
3 Dynamic Data Types stack s2; s2.push(4); s2.push(9); s2 = s1; // s1 as before s1 = s1; // self-assignment fails! stack& stack::operator= (const stack& s) { if (top_node!= s.top_node) { // test for self-assignment clear(top_node); top_node = 0; // fix dangling pointer copy(s.top_node, top_node); return *this;
Übungsblatt 12 Problem 12.1. Understanding structs and classes Problem 12.2. Averager Special restriction: Must use provided main function Problem 12.3. Queue (dynamic data types) Special restriction: Must use provided template (no simplifcaton allowed) Must implement a dynamic data type, reuse of existing dynamic container (e.g., vector) is not allowed. Similar to stack as dynamic structure enqueue, dequeue, is_empty output operator operator<< [1 4.. 10] copy constructor, assignment operator, destructor (rule of three)