Facebook
From Mammoth Agouti, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 259
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8.  
  9. class Character
  10. {
  11. private:
  12.         int _hp;
  13.         string _nazwa;
  14.  
  15. public:
  16.  
  17.         void setHp(int hp)
  18.         {
  19.                 _hp = hp;
  20.         }
  21.  
  22.         int getHp()
  23.         {
  24.                 return _hp;
  25.         }
  26.  
  27.         void setName(string nazwa)
  28.         {
  29.                 _nazwa = nazwa;
  30.         }
  31.  
  32.  
  33.         string getName()
  34.         {
  35.                 return _nazwa;
  36.         }
  37.  
  38.         string to_String()
  39.         {
  40.                 stringstream s;
  41.                 s << "Postac nazywa sie " << _nazwa << " i ma " << _hp << " punktow zycia" << endl;
  42.                 return s.str();
  43.         }
  44. };
  45.  
  46.  
  47. class CharacterEditor
  48. {
  49. private:
  50.         vector<Character> characters;
  51.  
  52. public:
  53.  
  54.         Character createCharacter()                                                  // tworzenie postaci
  55.         {
  56.                 Character temp;
  57.  
  58.                 cout << "Podaj nazwe: ";
  59.                 string nazwa;
  60.                 cin >> nazwa;
  61.                 cout << "Podaj ilosc zycia: ";
  62.                 int hp;
  63.                 cin >> hp;
  64.                 temp.setName(nazwa);
  65.                 temp.setHp(hp);
  66.                 characters.push_back(temp);
  67.  
  68.                 return temp;
  69.         }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.         string to_String()                                                          // tworzy jeden spojny string
  76.         {
  77.                 cout << "Nasze postacie:" << endl;
  78.  
  79.                 stringstream s;
  80.                 for (unsigned int i = 0; i < characters.size(); i++)
  81.                 {
  82.                         s << characters[i].to_String();
  83.                 }
  84.                 return s.str();
  85.         }
  86.  
  87.  
  88.  
  89.         void saveToFile(string zapis)                                              // zapisywanie postaci
  90.         {
  91.                 fstream plik;
  92.                 plik.open(zapis, ios::out);
  93.  
  94.                 for (unsigned int i = 0; i < characters.size(); i++)
  95.                 {
  96.  
  97.                         plik << characters[i].getName();
  98.                         plik << ":";
  99.                         plik << characters[i].getHp() << ";" << endl;                      // rozdzielenie elementów obiektu dwukropek, koniec obiektu średnik;
  100.  
  101.                 }
  102.  
  103.                 plik.close();
  104.         }
  105.  
  106.  
  107.         void loadFromFile(string wczytaj)                                         // wczytywanie postaci
  108.         {
  109.                 characters.clear();
  110.  
  111.                 int y = 0;
  112.                 string linia, tekst, tekst2;
  113.  
  114.                 fstream plik2;
  115.                 plik2.open(wczytaj, ios::in);
  116.  
  117.                 if (plik2.good() == false)
  118.                 {
  119.                         cout << "Plik nie istnieje!" << endl;
  120.                 }
  121.  
  122.                 while (getline(plik2, linia))
  123.                 {
  124.                         Character temp;
  125.  
  126.                         tekst = "";
  127.                         tekst2 = "";
  128.                         size_t dwukropek_pozycja = linia.find(':');
  129.                         size_t srednik_pozycja = linia.find(';');
  130.                         tekst.insert(0, linia, 0, dwukropek_pozycja);
  131.                         tekst2.insert(0, linia, dwukropek_pozycja + 1, srednik_pozycja - dwukropek_pozycja - 1);
  132.                         y = atoi(tekst2.c_str());
  133.  
  134.                         temp.setName(tekst);
  135.                         temp.setHp(y);
  136.                         characters.push_back(temp);
  137.                 }
  138.  
  139.  
  140.                 plik2.close();
  141.         }
  142.  
  143. };
  144.  
  145.  
  146. int main()
  147. {
  148.         CharacterEditor postacie;
  149.         char wybor = 'a';
  150.  
  151.  
  152.         for (;;)
  153.         {
  154.  
  155.                 cout << endl;
  156.                 cout << "MENU GLOWNE" << endl;
  157.                 cout << "-----------------" << endl;
  158.                 cout << "1. Dodaj postac" << endl;
  159.                 cout << "2. Zapisz" << endl;
  160.                 cout << "3. Wczytaj" << endl;
  161.                 cout << "4. Wyswietl postacie" << endl;
  162.                 cout << "5. Koniec programu" << endl;
  163.  
  164.  
  165.                 cout << endl;
  166.                 wybor = getchar();
  167.  
  168.                 switch (wybor)
  169.                 {
  170.                 case '1':
  171.                         postacie.createCharacter();
  172.                         break;
  173.  
  174.                 case '2':
  175.                         postacie.saveToFile("postacie.dat");
  176.                         break;
  177.  
  178.                 case '3':
  179.                         postacie.loadFromFile("postacie.dat");
  180.                         break;
  181.  
  182.                 case '4':
  183.                         postacie.to_String();
  184.                         system("pause");
  185.                         break;
  186.  
  187.                 case '5':
  188.                         exit(0);
  189.                         break;
  190.  
  191.                 default: cout << "Nie ma takiej opcji w menu!";
  192.                 }
  193.  
  194.                 cin.get();
  195.                 system("cls");
  196.         }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.         cin.get();
  203.         cin.get();
  204.  
  205. }