informatik 1 woche 9informatik 1 woche 9 moritz schneider moritz schneider 2. mai 2019 1 outline...
TRANSCRIPT
![Page 1: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/1.jpg)
Informatik 1Woche 9Moritz Schneider
Moritz Schneider 2. Mai 2019 1
![Page 2: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/2.jpg)
Outline
1. Streams
2. Function Overloading
3. Classes
4. Repetition: Recursion
Moritz Schneider 2. Mai 2019 2
![Page 3: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/3.jpg)
Streams
• Streams werden benoetigt um Werte einzulesen oder auszugeben
• Beispiel: std :: cout oder std :: cin
• peek () oberster Charakter nur ansehen nicht einlesen
• >> : oberstere Charakter einlesen• CodeExpert Beispiel:
– char lookahead (std :: istream & is)– bool consume (std :: istream & is , char expected )
Moritz Schneider 2. Mai 2019 3
![Page 4: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/4.jpg)
Function Overloading
int fun1 (int a) { ... }int fun1 (int a, int b) { ... }
→ Für Compiler eindeutig unterscheidbar→ gültiges (C) C++
int fun2 (int a) { ... }int fun2 ( float a) { ... }
→ Typ wichtig (int ↔ float )→ Parametername (a) egal
Moritz Schneider 2. Mai 2019 4
![Page 5: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/5.jpg)
Function Overloading II
int fun3 (int a) { ... }int fun3 (int b) { ... } // compiler error
→ Für Compiler nicht unterscheidbar→ Parametername ist kein Entscheidungskriterium, da oft nicht mitgegeben.
int c = func (3); // unklar (kein Parametername )
int fun4 (int a) { ... }double fun4 (int a) { ... } // compiler error
→ Nicht unterscheidbardouble d = func4 (3); // unklar ( impliziter cast moeglich )
Moritz Schneider 2. Mai 2019 5
![Page 6: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/6.jpg)
Function Overloading III
#include <iostream >void out (const int i) {
std::cout << i << "␣(int)\n";}void out (const double i) {
std::cout << i << "␣(double )\n";}int main () {
out (3.5); // 3.5 (double)out (2); // 2 (int)out (2.0); // 2 (double)out (0); // 0 (int)out (0.0); // 0 (double)return 0;
}Moritz Schneider 2. Mai 2019 6
![Page 7: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/7.jpg)
Classes
• Ähnlich wie struct
• Aber: private Methoden/Member
class rect {private:
int x1 , y1 , x2 , y2; // private memberpublic:
int flaeche () { // public memberfunktionint fl = (x1 -x2) * (y2 -y1);if (fl < 0) return -fl;else return fl;
};}
Moritz Schneider 2. Mai 2019 7
![Page 8: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/8.jpg)
Classes II - Deklaration vs Definition
Deklaration: rect.h
class rect {private:
int x1 , y1 , x2 , y2;public:
int flaeche ();
// Definition geht auchint get_x1 () { return x1; }
}
Definition: rect.cpp
#include "rect.h"
// POST: return area of the rectint rect:: flaeche () {
int fl = (x1 -x2) * (y2 -y1);if (fl < 0) return -fl;else return fl;
}
Moritz Schneider 2. Mai 2019 8
![Page 9: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/9.jpg)
Classes III - KonstruktorenKonstruktoren werden zum initialisieren benoetigt:
class rect {private:
int x1 , x2 , y1 , y2 , fl;public:
rect(int p1_x , int p1_y , int p2_x , int p2_y) :x1(p1_x), x2(p2_x), y1(p1_y), y2(p2_y) {
fl = flaeche ();}~rect() {
// loeschen von variablen// int , float , vector etc automatisch geloescht// pointer: muss selbst geloescht werden!
}}
Moritz Schneider 2. Mai 2019 9
![Page 10: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/10.jpg)
Beispiel: Operator Überladen
Addition von zwei Punkten:class point {
private :int x, y;
public :point (x_inp , y_inp ): x( x_inp ), y( y_inp ) {};
};
point operator +( const point &first , const point & second ) {return point ( first .x + second .x, first .y + second .y);
}
Oder:point point :: operator +( const point & second ) {
return point (this ->x + second .x, this ->y + second .y);}
Moritz Schneider 2. Mai 2019 10
![Page 11: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/11.jpg)
TriBool
CodeExpert
Moritz Schneider 2. Mai 2019 11
![Page 12: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/12.jpg)
Recursion Exercise 1
![Page 13: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/13.jpg)
Recursion Exercise 1
Rewrite the following recursive function in iterative form.
(From: Script Exercise 128) 2
unsigned int f (const unsigned int n)
{
if (n <= 2) return 1;
return f(n-1) + 2 * f(n-3);
}
![Page 14: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/14.jpg)
Recursion Exercise 1
(From: Script Exercise 128) 3
unsigned int f_it (const unsigned int n)
{
if (n <= 2) return 1;
unsigned int a = 1; // f(0)
unsigned int b = 1; // f(1)
unsigned int c = 1; // f(2)
for (unsigned int i = 3; i < n; ++i) {
const unsigned int a_prev = a; // f(i-3)
a = b; // f(i-2)
b = c; // f(i-1)
c = b + 2 * a_prev; // f(i)
}
return c + 2 * a; // f(n-1) + 2 * f(n-3)
}
Solution below uses just 4 variables.Other solutions are of course also possible.
![Page 15: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/15.jpg)
Recursion Exercise 2
![Page 16: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/16.jpg)
Recursion Exercise 2
Rewrite the following recursive function in iterative form.
(From: Script Exercise 129) 5
unsigned int f (const unsigned int n)
{
if (n == 0) return 1;
return f(n-1) + 2 * f(n/2);
}
![Page 17: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/17.jpg)
Recursion Exercise 2
(From: Script Exercise 129) 6
unsigned int f_it (const unsigned int n)
{
if (n == 0) return 1; // special case
std::vector<unsigned int> f_values(n+1, 0);
f_values[0] = 1;
for (unsigned int i=1; i<=n; ++i)
f_values[i] = f_values[i-1] + 2*f_values[i/2];
return f_values[n];
}
Solution below stores intermediate results in a vector.Other solutions are of course also possible.
![Page 18: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/18.jpg)
Recursion Exercise 3
![Page 19: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/19.jpg)
Recursion Exercise 3
Write a function rev_out (see template below) which outputs the contents of an istream in reverse order using recursion.
8
#include <iostream>
#include <sstream>
// POST: output the content of is in reverse order to
// std::cout, and removed it from is.
void rev_out (std::istream& is)
{
// your code
}
int main () {
std::stringstream input ("abcdefghijklmno");
rev_out (input);
return 0;
}
![Page 20: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/20.jpg)
Recursion Exercise 3
9
Other solutions are of course also possible.
#include <iostream>
#include <sstream>
// POST: output the content of is in reverse order to
// std::cout, and removed it from is.
void rev_out (std::istream& is)
{
char val;
if (is >> val) {
rev_out(is);
std::cout << val;
}
}
int main () {
std::stringstream input ("abcdefghijklmno");
rev_out (input);
return 0;
}
![Page 21: Informatik 1 Woche 9Informatik 1 Woche 9 Moritz Schneider Moritz Schneider 2. Mai 2019 1 Outline 1.Streams 2.Function Overloading 3.Classes 4.Repetition: Recursion Moritz Schneider](https://reader035.vdocuments.site/reader035/viewer/2022071508/612972bde7eced36460a0436/html5/thumbnails/21.jpg)
Fragen?
Moritz Schneider 2. Mai 2019 12