AngrySkarpeta Temat założony przez niniejszego użytkownika |
» 2014-05-26 16:32:15 @michal11 u mnie wywalało nadal ;c Tak czy owak, działa już. Trzeba było zmienić getery adresów na string get_Adres_Zamieszkania() { if( Adres_Zamieszkania == NULL ) { return ""; } return Adres_Zamieszkania->get_Miasto() + " " + Adres_Zamieszkania->get_Ulica() + " " + Adres_Zamieszkania->get_Numer(); } string get_Adres_Korespondencyjny() { if( Adres_Korespondencyjny == NULL ) { return ""; } return Adres_Korespondencyjny->get_Miasto() + " " + Adres_Korespondencyjny->get_Ulica() + " " + Adres_Korespondencyjny->get_Numer(); } string get_Adres_Dodatkowy() { if( Adres_Dodatkowy == NULL ) { return ""; } return Adres_Dodatkowy->get_Miasto() + " " + Adres_Dodatkowy->get_Ulica() + " " + Adres_Dodatkowy->get_Numer(); }
i sam zapis void zapisz_pracownikow() { ofstream zapis; zapis.open( "PRACOWNICY.txt" ); for( int i = 0; i < Pracownik::ile_P; i++ ) { zapis << Vec_Pracownik[ i ]->get_Imie() << " " << Vec_Pracownik[ i ]->get_Nazwisko() << " " << Vec_Pracownik[ i ]->get_Wiek() << " " << Vec_Pracownik[ i ]->get_Kod_Pracownika() << " " << Vec_Pracownik[ i ]->get_Pensja() << " "; if( Vec_Pracownik[ i ]->get_Adres_Zamieszkania() == "" ) { zapis << "NULL" << " " << "NULL" << " " << "NULL" << " "; } else { zapis << Vec_Pracownik[ i ]->get_Adres_Zamieszkania() << " "; } if( Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() == "" ) { zapis << "NULL" << " " << "NULL" << " " << "NULL" << " "; } else { zapis << Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() << " "; } if( Vec_Pracownik[ i ]->get_Adres_Dodatkowy() == "" ) { zapis << "NULL" << " " << "NULL" << " " << "NULL" << " " << endl; } else { zapis << Vec_Pracownik[ i ]->get_Adres_Dodatkowy() << " "; } zapis << "\n"; } zapis.close(); }
Jakby ktoś potrzebował na koniec wstawiam jeszcze całość ;-) Odczyt z pliku robić należy analogicznie? tj. z pliku na seter wrzucać jakoś, czy przypisywać do zmiennych i tworzyć obiekty? #include <iostream> #include <vector> #include <string> #include <cstdlib> #include <fstream> using namespace std;
class Adres { private: string Miasto; string Ulica; string Numer; public: Adres( string M, string U, string N ) { Miasto = M; Ulica = U; Numer = N; }; string get_Miasto() { return this->Miasto; } string get_Ulica() { return this->Ulica; } string get_Numer() { return this->Numer; } void set_Miasto( string M ) { this->Miasto = M; } void set_Ulica( string U ) { this->Ulica = U; } void set_Numer( string N ) { this->Numer = N; } }; class Osoba { private: string Imie; string Nazwisko; int Wiek; Adres * Adres_Zamieszkania; Adres * Adres_Korespondencyjny; Adres * Adres_Dodatkowy; protected: Osoba( string I, string N, int W ) { Imie = I; Nazwisko = N; Wiek = W; Adres_Zamieszkania = NULL; Adres_Korespondencyjny = NULL; Adres_Dodatkowy = NULL; ++ile_O; }; public: static int ile_O; ~Osoba() { --ile_O; } string get_Imie() { return this->Imie; } string get_Nazwisko() { return this->Nazwisko; } int get_Wiek() { return this->Wiek; } string get_Adres_Zamieszkania() { if( Adres_Zamieszkania == NULL ) { return ""; } return Adres_Zamieszkania->get_Miasto() + " " + Adres_Zamieszkania->get_Ulica() + " " + Adres_Zamieszkania->get_Numer(); } string get_Adres_Korespondencyjny() { if( Adres_Korespondencyjny == NULL ) { return ""; } return Adres_Korespondencyjny->get_Miasto() + " " + Adres_Korespondencyjny->get_Ulica() + " " + Adres_Korespondencyjny->get_Numer(); } string get_Adres_Dodatkowy() { if( Adres_Dodatkowy == NULL ) { return ""; } return Adres_Dodatkowy->get_Miasto() + " " + Adres_Dodatkowy->get_Ulica() + " " + Adres_Dodatkowy->get_Numer(); } void set_Imie( string I ) { this->Imie = I; } void set_Nazwisko( string N ) { this->Nazwisko = N; } void set_Wiek( int W ) { this->Wiek = W; } void set_Adres_Zamieszkania( string M, string U, string N ) { if( Adres_Zamieszkania == NULL ) { Adres_Zamieszkania = new Adres( M, U, N ); } else { this->Adres_Zamieszkania->set_Miasto( M ); this->Adres_Zamieszkania->set_Ulica( U ); this->Adres_Zamieszkania->set_Numer( N ); } } void set_Adres_Korespondencyjny( string M, string U, string N ) { if( Adres_Korespondencyjny == NULL ) { Adres_Korespondencyjny = new Adres( M, U, N ); } else { this->Adres_Korespondencyjny->set_Miasto( M ); this->Adres_Korespondencyjny->set_Ulica( U ); this->Adres_Korespondencyjny->set_Numer( N ); } } void set_Adres_Dodatkowy( string M, string U, string N ) { if( Adres_Dodatkowy == NULL ) { Adres_Dodatkowy = new Adres( M, U, N ); } else { this->Adres_Dodatkowy->set_Miasto( M ); this->Adres_Dodatkowy->set_Ulica( U ); this->Adres_Dodatkowy->set_Numer( N ); } } static int ile_Osob() { return ile_O; } }; class Pracownik : public Osoba { private: string Kod_Pracownika; double Pensja; public: static int ile_P; Pracownik( string I, string N, int W, string K, double P ) : Osoba( I, N, W ) { Kod_Pracownika = K; Pensja = P; ++ile_P; }; ~Pracownik() { --ile_P; } string get_Kod_Pracownika() { return this->Kod_Pracownika; } double get_Pensja() { return this->Pensja; } void set_Kod_Pracownika( string K ) { this->Kod_Pracownika = K; } void set_Pensja( double P ) { this->Pensja = P; } static int ile_Pracownikow() { return ile_P; } void srednia_Pracownicy(); }; class Student : public Osoba { private: string Numer_Albumu; double Srednia; public: static int ile_S; Student( string I, string N, int W, string NA, double S ) : Osoba( I, N, W ) { Numer_Albumu = NA; Srednia = S; ++ile_S; }; ~Student() { --ile_S; } string get_Numer_Albumu() { return this->Numer_Albumu; } double get_Srednia() { return this->Srednia; } void set_Numer_Albumu( string NA ) { this->Numer_Albumu = NA; } void set_Srednia( double S ) { this->Srednia = S; } static int ile_Studentow() { return ile_S; } void srednia_Studenci(); }; vector < Student *> Vec_Student; vector < Pracownik *> Vec_Pracownik; void dodaj_pracownika(); void wyswietl_pracownikow(); void wykasuj_Adres_Pracownika(); void dodaj_studenta(); void wykasuj_Adres_Studenta(); void wyswietl_studentow(); void wyswietl_adresy_pracownika(); void wyswietl_adresy_studenta(); void zapisz_pracownikow(); int Pracownik::ile_P = 0; int Student::ile_S = 0; int Osoba::ile_O = 0; int main() { for(;; ) { cout << "Menu" << endl << "1.Dodaj Pracownika" << endl << "2.Dodaj Studenta" << endl << "3.Wyswietl Pracownikow" << endl << "4.Wyswietl Studentow" << endl << "5.Wykasuj adres Pracownika" << endl << "6.Wykasuj adres Studenta" << endl << "7.Pokaz Adres Pracownika" << endl << "8.Pokaz Adres Studenta" << endl << "9.Ile pracownikow?" << endl << "10.Ile studentow?" << endl << "11.Ile osob?" << endl << "12.Srednia pensja" << endl << "13.Srednia ocen" << endl << "14.Zapis do pliku" << endl << "15.Odczyt z pliku" << endl << "k.Wyjscie z programu" << endl; string buf; getline( cin, buf ); cout << endl; if( buf == "1" ) { dodaj_pracownika(); cin.ignore(); system( "PAUSE" ); } else if( buf == "2" ) { dodaj_studenta(); cin.ignore(); system( "PAUSE" ); } else if( buf == "3" ) { wyswietl_pracownikow(); cin.ignore(); system( "PAUSE" ); } else if( buf == "4" ) { wyswietl_studentow(); cin.ignore(); system( "PAUSE" ); } else if( buf == "5" ) { wykasuj_Adres_Pracownika(); cin.ignore(); system( "PAUSE" ); } else if( buf == "6" ) { wykasuj_Adres_Studenta(); cin.ignore(); system( "PAUSE" ); } else if( buf == "7" ) { wyswietl_adresy_pracownika(); cin.ignore(); system( "PAUSE" ); } else if( buf == "8" ) { wyswietl_adresy_studenta(); cin.ignore(); system( "PAUSE" ); } else if( buf == "9" ) { cout << "Pracownikow w bazie : " << Pracownik::ile_Pracownikow() << endl; system( "PAUSE" ); } else if( buf == "10" ) { cout << "Studentow w bazie : " << Student::ile_Studentow() << endl; system( "PAUSE" ); } else if( buf == "11" ) { cout << "Osob w bazie : " << Osoba::ile_Osob() << endl; system( "PAUSE" ); } else if( buf == "12" ) { Vec_Pracownik[ 0 ]->srednia_Pracownicy(); } else if( buf == "13" ) { Vec_Student[ 0 ]->srednia_Studenci(); system( "PAUSE" ); } else if( buf == "14" ) { zapisz_pracownikow(); system( "PAUSE" ); } else if( buf == "k" ) { cout << "Nastapi wyjscie z programu" << endl; break; } else { cout << "Wybrano nieprawidlowa opcje" << endl; } } } void dodaj_pracownika() { string I, N, K, M, U; int W; double P; char pytanie; cout << "Podaj Imie" << endl; cin >> I; cout << "Podaj Nazwisko" << endl; cin >> N; cout << "Podaj Wiek" << endl; cin >> W; cout << "Podaj Kod Pracownika" << endl; cin >> K; cout << "Podaj pensje" << endl; cin >> P; Vec_Pracownik.push_back( new Pracownik( I, N, W, K, P ) ); cout << "Czy chcesz dodac Adres Zamieszkania?(t-Tak)" << endl; cin >> pytanie; if( pytanie == 't' ) { cout << "Miasto zamieszkania" << endl; cin >> M; cout << "Ulica zamieszkania" << endl; cin >> U; cout << "Numer zamieszkania" << endl; cin >> N; Vec_Pracownik.back()->set_Adres_Zamieszkania( M, U, N ); } cout << "Czy chcesz dodac Adres Korespondencyjny(t-Tak)" << endl; cin >> pytanie; if( pytanie == 't' ) { cout << "Miasto zamieszkania" << endl; cin >> M; cout << "Ulica zamieszkania" << endl; cin >> U; cout << "Numer zamieszkania" << endl; cin >> N; Vec_Pracownik.back()->set_Adres_Korespondencyjny( M, U, N ); } cout << "Czy chcesz dodac Adres Dodatkowy(t-Tak)" << endl; cin >> pytanie; if( pytanie == 't' ) { cout << "Miasto zamieszkania" << endl; cin >> M; cout << "Ulica zamieszkania" << endl; cin >> U; cout << "Numer zamieszkania" << endl; cin >> N; Vec_Pracownik.back()->set_Adres_Dodatkowy( M, U, N ); } } void wyswietl_pracownikow() { cout << "Rekord / Imie / Nazwisko / Wiek / Kod Pracownika / Pensja" << endl; for( int i = 0; i < Vec_Pracownik.size(); i++ ) { cout << i << " / " << Vec_Pracownik[ i ]->get_Imie() << " / " << Vec_Pracownik[ i ]->get_Nazwisko() << " / " << Vec_Pracownik[ i ]->get_Wiek() << " / " << Vec_Pracownik[ i ]->get_Kod_Pracownika() << " / " << Vec_Pracownik[ i ]->get_Pensja() << endl; } } void wykasuj_Adres_Pracownika() { int i; char pytanie; cout << "Ktorego Pracownika adres chcesz wykasowac?" << endl; cin >> i; cout << "Ktory Adres chcesz usunac? z-zamieszkania,k-korespondencyjny,d-dodatkowy" << endl; cin >> pytanie; if( pytanie == 'z' ) Vec_Pracownik[ i ]->set_Adres_Zamieszkania( NULL, NULL, NULL ); else if( pytanie == 'k' ) Vec_Pracownik[ i ]->set_Adres_Korespondencyjny( NULL, NULL, NULL ); else if( pytanie == 'd' ) Vec_Pracownik[ i ]->set_Adres_Dodatkowy( NULL, NULL, NULL ); else cout << "Wybrales zla opcje" << endl; } void dodaj_studenta() { string I, N, K, M, U; int W; double P; char pytanie; cout << "Podaj Imie" << endl; cin >> I; cout << "Podaj Nazwisko" << endl; cin >> N; cout << "Podaj Wiek" << endl; cin >> W; cout << "Podaj Numer Albumu" << endl; cin >> K; cout << "Podaj Srednia" << endl; cin >> P; Vec_Student.push_back( new Student( I, N, W, K, P ) ); cout << "Czy chcesz dodac Adres Zamieszkania?(t-Tak)" << endl; cin >> pytanie; if( pytanie == 't' ) { cout << "Miasto zamieszkania" << endl; cin >> M; cout << "Ulica zamieszkania" << endl; cin >> U; cout << "Numer zamieszkania" << endl; cin >> N; Vec_Student.back()->set_Adres_Zamieszkania( M, U, N ); } cout << "Czy chcesz dodac Adres Korespondencyjny(t-Tak)" << endl; cin >> pytanie; if( pytanie == 't' ) { cout << "Miasto zamieszkania" << endl; cin >> M; cout << "Ulica zamieszkania" << endl; cin >> U; cout << "Numer zamieszkania" << endl; cin >> N; Vec_Student.back()->set_Adres_Korespondencyjny( M, U, N ); } cout << "Czy chcesz dodac Adres Dodatkowy(t-Tak)" << endl; cin >> pytanie; if( pytanie == 't' ) { cout << "Miasto zamieszkania" << endl; cin >> M; cout << "Ulica zamieszkania" << endl; cin >> U; cout << "Numer zamieszkania" << endl; cin >> N; Vec_Student.back()->set_Adres_Dodatkowy( M, U, N ); } } void wyswietl_studentow() { cout << "Rekord / Imie / Nazwisko / Wiek / Numer Albumu / Srednia" << endl; for( int i = 0; i < Vec_Student.size(); i++ ) { cout << i << " / " << Vec_Student[ i ]->get_Imie() << " / " << Vec_Student[ i ]->get_Nazwisko() << " / " << Vec_Student[ i ]->get_Wiek() << " / " << Vec_Student[ i ]->get_Numer_Albumu() << " / " << Vec_Student[ i ]->get_Srednia() << endl; } } void wykasuj_Adres_Studenta() { int i; char pytanie; cout << "Ktorego Pracownika adres chcesz wykasowac?" << endl; cin >> i; cout << "Ktory Adres chcesz usunac? z-zamieszkania,k-korespondencyjny,d-dodatkowy" << endl; cin >> pytanie; if( pytanie == 'z' ) Vec_Student[ i ]->set_Adres_Zamieszkania( NULL, NULL, NULL ); else if( pytanie == 'k' ) Vec_Student[ i ]->set_Adres_Korespondencyjny( NULL, NULL, NULL ); else if( pytanie == 'd' ) Vec_Student[ i ]->set_Adres_Dodatkowy( NULL, NULL, NULL ); else cout << "Wybrales zla opcje" << endl; } void wyswietl_adresy_pracownika() { int i; cout << "Ktorego Pracownika adresy Cie interesuja?" << endl; cin >> i; cout << "Imie : " << Vec_Pracownik[ i ]->get_Imie() << endl; cout << "Nazwisko : " << Vec_Pracownik[ i ]->get_Nazwisko() << endl; cout << "Wiek : " << Vec_Pracownik[ i ]->get_Wiek() << endl; if( Vec_Pracownik[ i ]->get_Adres_Zamieszkania().empty() == false ) cout << "Adres Zamieszkania : " << Vec_Pracownik[ i ]->get_Adres_Zamieszkania() << endl; if( Vec_Pracownik[ i ]->get_Adres_Korespondencyjny().empty() == false ) cout << "Adres Korespondencyjny : " << Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() << endl; if( Vec_Pracownik[ i ]->get_Adres_Dodatkowy().empty() == false ) cout << "Adres Dodatkowy : " << Vec_Pracownik[ i ]->get_Adres_Dodatkowy() << endl; } void wyswietl_adresy_studenta() { int i; cout << "Ktorego Studenta adresy Cie interesuja?" << endl; cin >> i; cout << "Imie : " << Vec_Student[ i ]->get_Imie() << endl; cout << "Nazwisko : " << Vec_Student[ i ]->get_Nazwisko() << endl; cout << "Wiek : " << Vec_Student[ i ]->get_Wiek() << endl; cout << "Adres Zamieszkania : " << Vec_Student[ i ]->get_Adres_Zamieszkania() << endl; cout << "Adres Korespondencyjny : " << Vec_Student[ i ]->get_Adres_Korespondencyjny() << endl; cout << "Adres Dodatkowy : " << Vec_Student[ i ]->get_Adres_Dodatkowy() << endl; } void zapisz_pracownikow() { ofstream zapis; zapis.open( "PRACOWNICY.txt" ); for( int i = 0; i < Pracownik::ile_P; i++ ) { zapis << Vec_Pracownik[ i ]->get_Imie() << " " << Vec_Pracownik[ i ]->get_Nazwisko() << " " << Vec_Pracownik[ i ]->get_Wiek() << " " << Vec_Pracownik[ i ]->get_Kod_Pracownika() << " " << Vec_Pracownik[ i ]->get_Pensja() << " "; if( Vec_Pracownik[ i ]->get_Adres_Zamieszkania() == "" ) { zapis << "NULL" << " " << "NULL" << " " << "NULL" << " "; } else { zapis << Vec_Pracownik[ i ]->get_Adres_Zamieszkania() << " "; } if( Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() == "" ) { zapis << "NULL" << " " << "NULL" << " " << "NULL" << " "; } else { zapis << Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() << " "; } if( Vec_Pracownik[ i ]->get_Adres_Dodatkowy() == "" ) { zapis << "NULL" << " " << "NULL" << " " << "NULL" << " " << endl; } else { zapis << Vec_Pracownik[ i ]->get_Adres_Dodatkowy() << " "; } zapis << "\n"; } zapis.close(); } void Student::srednia_Studenci() { float srednia_ocen = 0; for( int i = 0; i < Vec_Student.size(); i++ ) { srednia_ocen = srednia_ocen + Vec_Student[ i ]->get_Srednia(); } srednia_ocen = srednia_ocen / Student::ile_S; cout << "Srednia ocen wszystkich studentow wynosi: " << srednia_ocen << endl; } void Pracownik::srednia_Pracownicy() { float srednia_pensja = 0; for( int i = 0; i < Vec_Pracownik.size(); i++ ) { srednia_pensja = srednia_pensja + Vec_Pracownik[ i ]->get_Pensja(); } srednia_pensja = srednia_pensja / Pracownik::ile_P; cout << "Srednia pensja wszystkich pracownikow wynosi: " << srednia_pensja << endl; }
|