1 / 130

Felder (Arrays) und Zeiger (Pointers) - Teil II

Felder (Arrays) und Zeiger (Pointers) - Teil II. Zeichen, Texte, String Matching; Mehrdimensionale Felder; kürzeste Wege. Zeichen und Texte. Texte haben wir schon gesehen:. std::cout &lt;&lt; &quot;Prime numbers in {2,...,999}:<br>&quot; ;. Zeichen und Texte. Texte haben wir schon gesehen:.

trapper
Télécharger la présentation

Felder (Arrays) und Zeiger (Pointers) - Teil II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Felder (Arrays) und Zeiger (Pointers) - Teil II Zeichen, Texte, String Matching; Mehrdimensionale Felder; kürzeste Wege

  2. Zeichen und Texte • Texte haben wir schon gesehen: std::cout << "Prime numbers in {2,...,999}:\n";

  3. Zeichen und Texte • Texte haben wir schon gesehen: std::cout << "Prime numbers in {2,...,999}:\n"; String-Literal

  4. Zeichen und Texte • Texte haben wir schon gesehen: • Können wir auch “richtig” mit Texten arbeiten? std::cout << "Prime numbers in {2,...,999}:\n"; String-Literal

  5. Zeichen und Texte • Texte haben wir schon gesehen: • Können wir auch “richtig” mit Texten arbeiten? Ja: std::cout << "Prime numbers in {2,...,999}:\n"; String-Literal Zeichen: Wert des fundamentalen Typs char Text: Feld mit zugrundeliegendem Typ char

  6. Der Typ char (“character”) • repräsentiert druckbare Zeichen (z.B. 'a') und Steuerzeichen (z.B. '\n')

  7. Der Typ char (“character”) • repräsentiert druckbare Zeichen (z.B. 'a') und Steuerzeichen (z.B. '\n') char c = 'a' definiert Variable c vom Typ char mit Wert 'a';

  8. Der Typ char (“character”) • repräsentiert druckbare Zeichen (z.B. 'a') und Steuerzeichen (z.B. '\n') char c = 'a' definiert Variable c vom Typ char mit Wert 'a'; Literal vom Typ char

  9. Der Typ char (“character”) • ist formal ein ganzzahliger Typ • Werte konvertierbar nach int / unsigned int • Alle arithemtischen Operatoren verfügbar (Nutzen zweifelhaft: was ist 'a'/'b' ?)

  10. Der Typ char (“character”) • ist formal ein ganzzahliger Typ • Werte konvertierbar nach int / unsigned int • Alle arithemtischen Operatoren verfügbar (Nutzen zweifelhaft: was ist 'a'/'b' ?) • Werte belegen meistens 8 Bit Wertebereich: {-128,...,127} oder {0,...,255}

  11. Der ASCII-Code • definiert konkrete Konversionregeln char int / unsigned int • wird von fast allen Plattformen benutzt

  12. Der ASCII-Code • definiert konkrete Konversionregeln char int / unsigned int • wird von fast allen Plattformen benutzt Zeichen  {0,...,127} 'A','B',...,'Z'  65, 66,...,90 'a','b',...,'z'  97, 98,...,122

  13. Der ASCII-Code for (char c = 'a'; c <= 'z'; ++c) std::cout << c; Ausgabe: abcdefghijklmnopqrstuvwxyz Zeichen  {0,...,127} 'A','B',...,'Z'  65, 66,...,90 'a','b',...,'z'  97, 98,...,122

  14. Texte • sind repräsentierbar als Felder mit zugrundeliegendem Typ char

  15. Texte • sind repräsentierbar als Felder mit zugrundeliegendem Typ char char text[] = {'b', 'o', 'o', 'l'} definiert ein Feld der Länge 4, das dem Text “bool” entspricht

  16. Texte • sind repräsentierbar als Felder mit zugrundeliegendem Typ char • können auch durch String-Literale definiert werden char text[] = {'b', 'o', 'o', 'l'} char text[] = "bool"

  17. Texte • sind repräsentierbar als Felder mit zugrundeliegendem Typ char • können auch durch String-Literale definiert werden char text[] = {'b', 'o', 'o', 'l'} definiert ein Feld der Länge 5, das dem Text “bool” ent-spricht und null-terminiert ist (Extrazeichen '\0' wird am Ende angehängt) char text[] = "bool"

  18. Texte • sind repräsentierbar als Felder mit zugrundeliegendem Typ char • können auch durch String-Literale definiert werden char text[] = {'b', 'o', 'o', 'l'} definiert ein Feld der Länge 5, das dem Text “bool” ent-spricht und null-terminiert ist (Extrazeichen '\0' wird am Ende angehängt) char text[] = "bool" “speichert” seine Länge!

  19. Anwendung: String matching Finde das erste (oder alle) Vorkommen eines Musters (meist kurz) in einem ge- gebenen Text (meist lang)!

  20. Anwendung: String matching Finde das erste (oder alle) Vorkommen eines Musters (meist kurz) in einem ge- gebenen Text (meist lang)! “Trivialer” Algorithmus: Gallia est omnis divisa in partes tres ≠ visa

  21. Anwendung: String matching Finde das erste (oder alle) Vorkommen eines Musters (meist kurz) in einem ge- gebenen Text (meist lang)! “Trivialer” Algorithmus: Gallia est omnis divisa in partes tres ≠ visa

  22. Anwendung: String matching Finde das erste (oder alle) Vorkommen eines Musters (meist kurz) in einem ge- gebenen Text (meist lang)! “Trivialer” Algorithmus: Gallia est omnis divisa in partes tres ≠ visa

  23. Anwendung: String matching Finde das erste (oder alle) Vorkommen eines Musters (meist kurz) in einem ge- gebenen Text (meist lang)! “Trivialer” Algorithmus: Gallia est omnis divisa in partes tres = (gefunden!) visa

  24. Anwendung: String matching #include<iostream> int main () { // search string const char s[] = "bool"; // determine search string length m unsigned int m = 0; for (const char* p = s; *p != '\0'; ++p) ++m; // cyclic text window of size m char* const t = new char[m]; unsigned int w = 0; // number of characters read so far unsigned int i = 0; // index where t logically starts ... Muster “fest verdrahtet” in diesem Program (aber siehe Details im Skript)

  25. Anwendung: String matching #include<iostream> int main () { // search string const char s[] = "bool"; // determine search string length m unsigned int m = 0; for (const char* p = s; *p != '\0'; ++p) ++m; // cyclic text window of size m char* const t = new char[m]; unsigned int w = 0; // number of characters read so far unsigned int i = 0; // index where t logically starts ... Rechne die Musterlänge aus (das geht, weil s null-terminiert ist)

  26. Anwendung: String matching #include<iostream> int main () { // search string const char s[] = "bool"; // determine search string length m unsigned int m = 0; for (const char* p = s; *p != '\0'; ++p) ++m; // cyclic text window of size m char* const t = new char[m]; unsigned int w = 0; // number of characters read so far unsigned int i = 0; // index where t logically starts ... G a l l Gallia i

  27. Anwendung: String matching #include<iostream> int main () { // search string const char s[] = "bool"; // determine search string length m unsigned int m = 0; for (const char* p = s; *p != '\0'; ++p) ++m; // cyclic text window of size m char* const t = new char[m]; unsigned int w = 0; // number of characters read so far unsigned int i = 0; // index where t logically starts ... i a l l Gallia i

  28. Anwendung: String matching #include<iostream> int main () { // search string const char s[] = "bool"; // determine search string length m unsigned int m = 0; for (const char* p = s; *p != '\0'; ++p) ++m; // cyclic text window of size m char* const t = new char[m]; unsigned int w = 0; // number of characters read so far unsigned int i = 0; // index where t logically starts ... i a l l Gallia i

  29. Anwendung: String matching Leerzeichen und Zeilenumbrüche sollen nicht ignoriert werden ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; }

  30. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i v i s a j w == 0

  31. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i v i s a j w == 0

  32. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G i v i s a j w == 0

  33. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G i Konversion nach bool (false wenn Eingabe-strom leer) v i s a j w == 0

  34. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G i v i s a j w == 1

  35. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G i v i s a j w == 1

  36. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G a i v i s a j w == 2

  37. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G a l i v i s a j w == 3

  38. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G a l l i v i s a j w == 4

  39. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } G a l l (i+j)%m i v i s a j w == 4

  40. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i a l l i v i s a j w == 5

  41. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v i v i s a j w == 23

  42. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v (i+j)%m i v i s a j w == 23

  43. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v i v i s a j w == 23

  44. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v (i+j)%m i v i s a j w == 23

  45. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v (i+j)%m i v i s a j w == 23

  46. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v (i+j)%m i v i s a j w == 23

  47. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v (i+j)%m i v i s a j w == 23

  48. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v false (i+j)%m i v i s a j w == 23

  49. Anwendung: String matching ... // find pattern in the text being read from std::cin std::cin >> std::noskipws; // don't skip whitespaces! for (unsigned int j = 0; j < m;) // compare search string with window at j-th element if (w < m || s[j] != t[(i+j)%m]) // input text still too short, or mismatch: // advance window by replacing first character if (std::cin >> t[i]) { std::cout << t[i]; ++w; // one more character read j = 0; // restart with first characters i = (i+1)%m; // of string and window } else break; // no more characters in the input else ++j; // match: go to next character std::cout << "\n"; delete[] t; return 0; } i s a v (i+j)%m i v i s a j Fertig! Die ersten 23 Text-zeichen wurden ausgegeben w == 23

  50. Anwendung: String matching ./string_matching < eratosthenes.cpp

More Related