mikewazowski Temat założony przez niniejszego użytkownika |
sortowanie alfabetyczne struktur na liscie dwukierunkowej » 2017-01-14 20:07:26 w funkcji sortowania usuwa dane zamiast posortować alfabetycznie według imienia i nazwiska, nie mam pomysłów co mogłabym zmienić #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <cstdio> #include <cstring> using namespace std; struct Dane { string imie; string nazwisko; string dataur; string ulica; string miasto; string inne; int id; }; struct Student { Dane dane; Student * next; Student * prev; static int num_of_students; }; int Student::num_of_students = 0;
Student * stworzelement() { Student * element; string imie; string nazwisko; string dataur; string ulica; string miasto; string inne; int id = ++Student::num_of_students; cout << "ID: " << id << endl; cout << "podaj imie: "; cin >> imie; cout << "podaj nazwisko: "; cin >> nazwisko; cout << "podaj date urodzenia: "; cin.sync(); getline( cin, dataur ); cout << "podaj ulice i numer domu: "; cin.sync(); getline( cin, ulica ); cout << "podaj miasto: "; cin.sync(); getline( cin, miasto ); cout << "podaj inne dane: "; cin.sync(); getline( cin, inne ); element = new Student(); element->next = NULL; element->prev = NULL; element->dane.id = id; element->dane.imie = imie; element->dane.nazwisko = nazwisko; element->dane.dataur = dataur; element->dane.ulica = ulica; element->dane.miasto = miasto; element->dane.inne = inne; return element; } int dodajkoniec( Student ** head ) { Student * pom; if( * head == NULL ) { Student * tmp = stworzelement(); * head = tmp; cout << "***dodano do bazy***" << endl; } else { Student * tmp = stworzelement(); pom = * head; while( pom->next != NULL ) { pom = pom->next; } tmp->prev = pom; pom->next = tmp; cout << "***dodano do bazy***" << endl; } } void wyswietl( Student ** head ) { Student * tmp; tmp = * head; if( tmp == NULL ) cout << "***brak elementow do wyswietlenia***"; else { while( tmp != NULL ) { cout << endl << "imie: " << tmp->dane.imie << endl; cout << "nazwisko: " << tmp->dane.nazwisko << endl; cout << "ID: " << tmp->dane.id << endl; cout << "data urodzenia: " << tmp->dane.dataur << endl; cout << "Ulica: " << tmp->dane.ulica << endl; cout << "Miasto: " << tmp->dane.miasto << endl; cout << "Inne: " << tmp->dane.inne << endl; tmp = tmp->next; } } }
void sortowanie_listy( Student ** head ) { Student * nowa = NULL, * tmp, * pom; while( * head != NULL ) { string imie, nazwisko; tmp = new Student(); tmp->dane.imie = imie; tmp->dane.nazwisko = nazwisko; tmp->next = NULL; tmp->prev = NULL; if( nowa == NULL ) nowa = tmp; else if((( nowa->dane.imie ).compare( tmp->dane.imie ) ) > 0 ) { tmp->next = nowa; nowa->prev = tmp; nowa = tmp; } else { pom = nowa; while( pom->next != NULL &&(( pom->next->dane.imie ).compare( tmp->dane.imie ) ) < 0 ) pom = pom->next; tmp->prev = pom; tmp->next = pom->next; if( pom->next != NULL ) pom->next->prev = tmp; pom->next = tmp; } pom = * head; * head =( * head )->next; if( pom->dane.imie ) delete( pom->dane.imie ); if( pom->dane.nazwisko ) delete( pom->dane.nazwisko ); delete( pom ); } * head = nowa; }
int main() { }
|
|
RazzorFlame |
» 2017-01-14 20:40:21 Ten kod nawet się nie kompiluje więc skąd wiesz jak działa. Dlaczego usuwasz studentów, przecież ich kolejność zależy tylko od składowych wskaźników "prev" i "next". Tylko je zamieniaj. |
|
mikewazowski Temat założony przez niniejszego użytkownika |
» 2017-01-14 20:44:32 u mnie kod się kompiluje, co dokładnie proponujesz zmienić w kodzie? |
|
karambaHZP |
» 2017-01-14 20:57:40 Sortowanie polega na zamianie odpowiednich wskaźników.
edit: Napisz funkcję (metodę), która przyjmie dwa obiekty i zamieni im wskaźniki. |
|
mikewazowski Temat założony przez niniejszego użytkownika |
» 2017-01-14 20:59:23 jakaś konkretniejsza wskazówka? :) gdybym była w stanie sama poradzić sobie z sortowaniem to nie zaśmiecałabym forum, także proszę o wyrozumiałość, bo dopiero zaczynam z programowaniem
Edit: Metody są jeszcze ponad moje siły |
|
RazzorFlame |
» 2017-01-14 21:37:17 Ten kod nie ma prawa się kompilować. Chyba, że istnieje jakiś cudpwny trick który pozwala usuwać std::string operatorem delete i inny trick, który pozwala na: std::string str; if( str ) { }
Ale o ile wiem to takich nie ma. if( pom->dane.imie ) delete( pom->dane.imie );
if( pom->dane.nazwisko ) delete( pom->dane.nazwisko );
Co Twoim zdaniem miałoby to robić? |
|
mokrowski |
» 2017-01-14 21:46:23 Proszę uruchom diagnostykę i ostrzeżenia kompilatora. Jeśli twierdzisz że kod się kompiluje to przepraszam ale u mnie także nie działa. Jeśli masz gcc to przełączniki: -Wall -Wextra -pedantic
Oto błędy kompilatora które wyrzuca z Twojego kodu. Dopisałem do niego komentarze abyś mogła usunąć błędy. bools.cpp:93:1: warning: control reaches end of non-void function [-Wreturn-type] } ^ bools.cpp:154:13: error: value of type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') is not contextually convertible to 'bool' if( pom->dane.imie ) ^~~~~~~~~~~~~~ bools.cpp:155:14: error: cannot delete expression of type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') delete( pom->dane.imie ); ^ ~~~~~~~~~~~~~~~~~~ bools.cpp:158:13: error: value of type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') is not contextually convertible to 'bool' if( pom->dane.nazwisko ) ^~~~~~~~~~~~~~~~~~ bools.cpp:159:14: error: cannot delete expression of type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') delete( pom->dane.nazwisko ); ^ ~~~~~~~~~~~~~~~~~~~~~~ 1 warning and 4 errors generated.
Kod z komentarzem miejsc błędu: #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <cstdio> #include <cstring> using namespace std; struct Dane { string imie; string nazwisko; string dataur; string ulica; string miasto; string inne; int id; }; struct Student { Dane dane; Student * next; Student * prev; static int num_of_students; }; int Student::num_of_students = 0;
Student * stworzelement() { Student * element; string imie; string nazwisko; string dataur; string ulica; string miasto; string inne; int id = ++Student::num_of_students; cout << "ID: " << id << endl; cout << "podaj imie: "; cin >> imie; cout << "podaj nazwisko: "; cin >> nazwisko; cout << "podaj date urodzenia: "; cin.sync(); getline( cin, dataur ); cout << "podaj ulice i numer domu: "; cin.sync(); getline( cin, ulica ); cout << "podaj miasto: "; cin.sync(); getline( cin, miasto ); cout << "podaj inne dane: "; cin.sync(); getline( cin, inne ); element = new Student(); element->next = NULL; element->prev = NULL; element->dane.id = id; element->dane.imie = imie; element->dane.nazwisko = nazwisko; element->dane.dataur = dataur; element->dane.ulica = ulica; element->dane.miasto = miasto; element->dane.inne = inne; return element; } int dodajkoniec( Student ** head ) //dodawanie nowej osoby na koniec { Student * pom; if( * head == NULL ) { Student * tmp = stworzelement(); * head = tmp; cout << "***dodano do bazy***" << endl; } else { Student * tmp = stworzelement(); pom = * head; while( pom->next != NULL ) { pom = pom->next; } tmp->prev = pom; pom->next = tmp; cout << "***dodano do bazy***" << endl; } // FIXME: Zadeklarowałaś w funkcji że zwracasz jakiś int. Gdzie jest return? } void wyswietl( Student ** head ) //wyswietla cala liste { Student * tmp; tmp = * head; if( tmp == NULL ) cout << "***brak elementow do wyswietlenia***"; else { while( tmp != NULL ) { cout << endl << "imie: " << tmp->dane.imie << endl; cout << "nazwisko: " << tmp->dane.nazwisko << endl; cout << "ID: " << tmp->dane.id << endl; cout << "data urodzenia: " << tmp->dane.dataur << endl; cout << "Ulica: " << tmp->dane.ulica << endl; cout << "Miasto: " << tmp->dane.miasto << endl; cout << "Inne: " << tmp->dane.inne << endl; tmp = tmp->next; } } }
void sortowanie_listy( Student ** head ) { Student * nowa = NULL, * tmp, * pom; while( * head != NULL ) { string imie, nazwisko; tmp = new Student(); tmp->dane.imie = imie; tmp->dane.nazwisko = nazwisko; tmp->next = NULL; tmp->prev = NULL; if( nowa == NULL ) nowa = tmp; else if((( nowa->dane.imie ).compare( tmp->dane.imie ) ) > 0 ) { tmp->next = nowa; nowa->prev = tmp; nowa = tmp; } else { pom = nowa; while( pom->next != NULL &&(( pom->next->dane.imie ).compare( tmp->dane.imie ) ) < 0 ) pom = pom->next; tmp->prev = pom; tmp->next = pom->next; if( pom->next != NULL ) pom->next->prev = tmp; pom->next = tmp; } pom = * head; * head =( * head )->next; // FIXME: Jak widać w strukturze pom->dane.imie to jest string. // Co sprawdzasz tym if'em? Druga sprawa to to że nie jest to // wskaźnik a robisz na nim delete if( pom->dane.imie ) delete( pom->dane.imie ); // FIXME: To samo co wyżej.. if( pom->dane.nazwisko ) delete( pom->dane.nazwisko ); delete( pom ); } * head = nowa; }
int main() { //menu }
PS. O @RazzorFlame mnie wyprzedził. Zostawię jednak bo jest dokumentacja ostrzeżeń kompilatora. |
|
« 1 » |