Centrum für Informations- und Sprachverarbeitung SoSe 2018: Höhere Programmierung mit C++ Andreas Krieger Musterlösung 7 Aufgabe 7.3 bool palindrome ( std :: wstring & word ); void reverse ( std :: wstring & word ); std :: wifstream input ; std :: wstring line ; std :: wstring word ; std :: wcout << L" Dateiname : "; input. open ( filename ); std :: cerr << " Die Datei existiert nicht!" << std :: endl ; if ( palindrome ( word )) { std :: wcout << word << std :: endl ; bool palindrome ( std :: wstring & word ) { std :: wstring r_word = word ; reverse ( r_word ); if ( word == r_word ) { else { void reverse ( std :: wstring & word ) { wchar_t temp ; for ( int i = 0; i < word. size ()/2; i ++) { temp = word [i]; word [i] = word [ word. size () - i - 1]; word [ word. size () - i - 1] = temp ; 1
// eine effizientere Funktion zum Test eines Palindroms // wir iterieren bis zur Mitte des Wortes und vergleichen // die i- ten Zeichen von vorne mit den i- Zeichen von hinten // wenn wir bis zur Mitte ohne Fehlschlag kommen, wird true zur ü ckgegeben bool ispalindrome ( std :: wstring & word ) { for ( int i = 0; i < word. length ()/2; i ++) { if ( word [i]!= word [ word. length () - i - 1]) { Aufgabe 7.4 bool splice ( std :: wstring word1, std :: wstring & word2, std :: wstring & erg ); std :: wifstream input ; std :: wstring line, word, substring, rest ; std :: wcin. imbue ( utf8 ); std :: wcout << L" Dateiname : "; input. open ( filename ); std :: cerr << " Datei existiert nicht!" << std :: endl ; std :: wcout << L" Bitte einen Substring eingeben >> "; std :: wcin >> substring ; if ( splice ( word, substring, rest )) { std :: wcout << word << L" - " << substring << L" = " << rest << std :: endl ; bool splice ( std :: wstring word1, std :: wstring & word2, std :: wstring & erg ) { int index = word1. find ( word2 ); // somit finden wir die Position, // wo das gesuchte Wort anf ä ngt if ( index!= std :: wstring :: npos ) { // std :: wstring :: npos -1 erg = word1. erase ( index, word2. size ()); // entfernen das word2 aus dem word1 // true, wenn das Word gefunden wird else { 2
Aufgabe 7.5 int split ( std :: wstring array [], std :: wstring & line ); std :: wifstream input (" idiot. txt "); std :: cerr << " Die Datei existiert nicht!" << std :: endl ; std :: wstring line ; std :: wstring array [20]; // ein Array der Lä nge 20 wird erstellt std :: wcout << split ( array, line ) << L" :: "; for ( auto x: array ) { // es wird durch ganzes Array iteriert if (! x. empty ()) { // x ist ein Element vom Array ( hier : String ) std :: wcout << x << L ; int split ( std :: wstring array [], std :: wstring & line ) { int i = 0; std :: wstring word ; while ( my_ stream >> word ){ // wir extrahieren die Wö rter aus der Zeile array [ i] = word ; // und speichern in einem Array i ++; return i; Aufgabe 7.6 bool first ( std :: wstring * array, int & count_words, std :: wstring & first_ word ); std :: wstring line, word, first_ word ; 3
std :: wcout << L" Dateiname >> "; std :: wifstream input ( filename ); std :: cerr << " Keine Datei gefunden!" << std :: endl ; std :: wcout << line << std :: endl ; int count_ words = 0; std :: wstring array [17] = {; // ein Array mit der Lä nge 17 ( leere Strings ) array [ count_words ] = word ; count_ words ++; bool not_ empty = first ( array, count_words, first_ word ); if( not_empty ){ std :: wcout << L" Anzahl der Wö rter im Array : " << count_ words << std :: endl ; std :: wcout << L" Das erste Wort : " << first_ word << std :: endl ; else { std :: wcout << L" >>>>>>>>> LISTE IST LEER!!! <<<<<<<<<" << std :: endl ; bool first ( std :: wstring * array, int & count_words, std :: wstring & first_ word ) { if ( count_ words == 0) { else { first_ word = array [0]; Aufgabe 7.7 bool last ( std :: wstring array [], int & count_ words ); std :: wstring line, word ; std :: wcout << L" Dateiname >> "; std :: wifstream input ( filename ); std :: cerr << " Keine Datei gefunden!" << std :: endl ; 4
std :: wcout << line << std :: endl ; int count_ words = 0; std :: wstring array [16] = {; array [ count_words ] = word ; count_ words ++; bool not_ empty = last ( array, count_ words ); if( not_empty ){ std :: wcout << L" Anzahl der Wö rter im Array : " << count_ words << std :: endl ; for ( auto x: array ) { std :: wcout << x << L ; if (x. empty ()){ std :: wcout << L" LEER "; else { std :: wcout << L" >>>> LISTE IST LEER!!! <<<<" << std :: endl ; bool last ( std :: wstring array [], int & count_ words ) { if ( count_ words == 0) { else { array [ count_ words - 1]. clear (); //. clear () setzt das letzte // Element vom Array auf Leer count_ words -= 1; Aufgabe 7.8 # include <array > bool member ( std :: wstring word, std :: wstring * array, int pos ); std :: wifstream input (" rassen. txt "); std :: cerr << " Die Datei existiert nicht!" << std :: endl ; std :: wstring line ; std :: wstring * array ; // Arrayzuweisung mit Pointer // damit wir zum sp ä teren Zeitpunkt // ein Array erstellen kö nnen und // gegebenenfalls lö schen. array = new std :: wstring [158]; // In der Datei " rassen. txt " sind 158 Zeilen, // deswegen erstellen wir ein 158 - Array. 5
for ( int i = 0; i < 158; i ++) { getline ( input, line ); array [i] = line ; std :: wstring word ; std :: wcout << L" Hund entlaufen!? Welche Rasse? "; getline ( std :: wcin, word ); int pos = 0; // setzen Position auf 0 bool ismember = member ( word, array, pos ); if ( ismember ) { std :: wcout << L" Suche " << word << L"... Gefunden!" << std :: endl ; else { std :: wcout << L" Suche " << word << L"... Nicht gefunden!" << std :: endl ; delete [] array ; // hier wird das Array gel ö scht bool member ( std :: wstring word, std :: wstring * array, int pos ) { if ( pos > 158) { else if ( word == array [ pos ]) { else { return member ( word, array, pos + 1); // wird jedes Mal die Funktion // aufgerufen bis wir entweder // das Wort finden oder in die // Au ß enposition gelangen. // Position wir um 1 verschoben. 6