Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Ich habe da ein problem mit einem Program, und zwar muss das Programm untersuchen ob ein wort nach dem alphabet geordnet ist und wenn dies so ist, dann soll das word in eine datei geschrieben werden.

ich habe eigendlich alles, ich habe alles was davor gemacht werden muss schon in dem program komme aber beim sort nicht weiter.

hier ist das program:

#include <string>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <functional>
#include <cctype>

int main () {
std::vector <std::string> vec;
std::ifstream stream ("wordorder.txt");
if (!stream) {
std::cout << "Can't open file" << std::endl;
return 1;
}
//read the wordlist into the vector
std::copy (std::istream_iterator <std::string> (stream), std::istream_iterator <std::string> (), std::back_inserter(vec));
//sort the list
std::sort (vec.begin (), vec.end () );

//convert all letters to lower case
static struct transform_helper :std::unary_function < std::string, void>

{
void operator () (std::string &value)
{
std::transform (value.begin (), value.end (), value.begin (), &tolower);
}
}
transform;

std::for_each (vec.begin (), vec.end (), transform);


std::





}[/PHP]

Ich habe da ein problem mit einem Program, und zwar muss das Programm untersuchen ob ein wort nach dem alphabet geordnet ist
Um zu prüfen, ob eine Sequenz einem Sortierkriterium genügt, kann man eine Kopie der Sequenz machen, diese sortieren, und prüfen, ob das Ergebnis und die Originalsequenz gleich sind.

using namespace std wäre vielleicht auch etwas gutes

Um zu prüfen, ob eine Sequenz einem Sortierkriterium genügt, kann man eine Kopie der Sequenz machen, diese sortieren, und prüfen, ob das Ergebnis und die Originalsequenz gleich sind.

Und nun kommt ja mein Problem das ich habe.

Ich habe keine Ahnung wie ich dies tun kann, daher welche befehle ich nutzen soll. Das Programm ist schon weit ueber meinen Kenntnissen, und nachdem ich nun die letzten 2 Wochen Krank war, laeuft mir die Zeit davon um das hier und zwei andere fertig zustellen.

bool is_sorted( string const& s )
{
string c = s; // Kopie von s erstellen
sort( c.begin(), c.end() ); // Kopie sortieren
return c == s; // Vergleichen
}[/code]

Ist nicht besonders effizient, aber erfüllt seinen Zweck.

Alternativ kannst du auch Element für Element durchgehen und prüfen, ob es kleiner oder gleich seinem Nachfolger ist.

ok ich habe nun das hier:

#include <string>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <functional>
#include <cctype>
#include <iterator>

using namespace std;

bool is_sorted( string const& s ) {
string c = s; // Kopie von s erstellen
sort( c.begin(), c.end() ); // Kopie sortieren
//int strcmp ();
cout << s << c << endl;
//while (strcmp ( s, c) !=0);
return c == s; // Vergleichen
cout << s << endl;
}

static struct transform_helper :unary_function < string, string> {
string operator () (string &value) {
int i=0;
//Iterate through each character of entire string
while (value[i]) {
//Change character to lowercase
value[i] = tolower(value[i]);
i++;
}
return value;
}
} transform_helper;


int main () {
vector <string> vec;
ifstream stream ("wordorder.txt");
if (!stream) {
cout << "Can't open file" << endl;
return 1;
}


//read the wordlist into the vector
copy (istream_iterator <string> (stream), istream_iterator <string> (), back_inserter(vec));
//sort the list
sort (vec.begin (), vec.end () );

//convert all letters to lower case
// for_each (vec.begin (), vec.end (), transform);
transform (vec.begin (), vec.end (), vec.begin (), transform_helper);

//sort (value.begin (), value.end ());

bool is_sorted(string );
}[/PHP]

Ich habe den Eindruck, dass du keine Ahnung hast, was du da eigentlich machst. Was willst du mit strcmp?

Man kann solche Aufgaben nicht ohne gewisse Grundkenntnisse lösen, und ich vermute, genau diese Grundkenntnisse fehlen dir.

Du kannst übrigens, wenn du statt is_sorted ein umgekehrtes Prädikat (is_not_sorted) benutzt, die Ausgabe aller alphabetisch sortierten Wörter mit zwei Zeilen Code realisieren:

std::ofstream out("output.txt");
std::remove_copy_if( vec.begin(), vec.end(), std::ostream_iterator<std::string>(out, "\n"), is_not_sorted );
[/code]

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.