#include #include #include #include #include using namespace std; class Character { private: int _hp; string _nazwa; public: void setHp(int hp) { _hp = hp; } int getHp() { return _hp; } void setName(string nazwa) { _nazwa = nazwa; } string getName() { return _nazwa; } string to_String() { stringstream s; s << "Postac nazywa sie " << _nazwa << " i ma " << _hp << " punktow zycia" << endl; return s.str(); } }; class CharacterEditor { private: vector characters; public: Character createCharacter() // tworzenie postaci { Character temp; cout << "Podaj nazwe: "; string nazwa; cin >> nazwa; cout << "Podaj ilosc zycia: "; int hp; cin >> hp; temp.setName(nazwa); temp.setHp(hp); characters.push_back(temp); return temp; } string to_String() // tworzy jeden spojny string { cout << "Nasze postacie:" << endl; stringstream s; for (unsigned int i = 0; i < characters.size(); i++) { s << characters[i].to_String(); } return s.str(); } void saveToFile(string zapis) // zapisywanie postaci { fstream plik; plik.open(zapis, ios::out); for (unsigned int i = 0; i < characters.size(); i++) { plik << characters[i].getName(); plik << ":"; plik << characters[i].getHp() << ";" << endl; // rozdzielenie elementów obiektu dwukropek, koniec obiektu średnik; } plik.close(); } void loadFromFile(string wczytaj) // wczytywanie postaci { characters.clear(); int y = 0; string linia, tekst, tekst2; fstream plik2; plik2.open(wczytaj, ios::in); if (plik2.good() == false) { cout << "Plik nie istnieje!" << endl; } while (getline(plik2, linia)) { Character temp; tekst = ""; tekst2 = ""; size_t dwukropek_pozycja = linia.find(':'); size_t srednik_pozycja = linia.find(';'); tekst.insert(0, linia, 0, dwukropek_pozycja); tekst2.insert(0, linia, dwukropek_pozycja + 1, srednik_pozycja - dwukropek_pozycja - 1); y = atoi(tekst2.c_str()); temp.setName(tekst); temp.setHp(y); characters.push_back(temp); } plik2.close(); } }; int main() { CharacterEditor postacie; char wybor = 'a'; for (;;) { cout << endl; cout << "MENU GLOWNE" << endl; cout << "-----------------" << endl; cout << "1. Dodaj postac" << endl; cout << "2. Zapisz" << endl; cout << "3. Wczytaj" << endl; cout << "4. Wyswietl postacie" << endl; cout << "5. Koniec programu" << endl; cout << endl; wybor = getchar(); switch (wybor) { case '1': postacie.createCharacter(); break; case '2': postacie.saveToFile("postacie.dat"); break; case '3': postacie.loadFromFile("postacie.dat"); break; case '4': postacie.to_String(); system("pause"); break; case '5': exit(0); break; default: cout << "Nie ma takiej opcji w menu!"; } cin.get(); system("cls"); } cin.get(); cin.get(); }