Advertisement
Guest User

polab4

a guest
Nov 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. #include <istream>
  7. #include <cstdlib>
  8. #include <sstream>
  9. #include <time.h>
  10. using namespace std;
  11.  
  12.  
  13. class Player
  14. {
  15. public:
  16.     //metody
  17.     Player(string name, string sname, string height, string age, string handling);
  18.     Player(const Player &source);
  19.     void review();
  20.     void modify();
  21.     //pola
  22.     string retName() { return this->name; }
  23.     string retSname() { return this->sname; }
  24.     string retAge() { return this->age; }
  25.     string retHeight() { return this->height; }
  26.     string retHandling() { return this->handling; }
  27.  
  28. private:
  29.     string name, sname;
  30.     string height;
  31.     string age;
  32.     string handling;
  33.  
  34. };
  35.  
  36. void Player::modify()
  37. {
  38.     this->name = "Sent to";
  39.     this->sname = "Gulag (TM)";
  40.     this->age = "nA";
  41.     this->handling = "nA";
  42.     this->height = "nA";
  43. }
  44. void Player::review()
  45. {
  46.     cout.width(15); cout << this->name;
  47.     cout.width(20); cout << this->sname;
  48.     cout.width(10); cout << this->height;
  49.     cout.width(9); cout << this->age;
  50.     cout.width(10); cout << this->handling;
  51.     cout << "\n";
  52.  
  53. }
  54. Player::Player(const Player &source)
  55. {
  56.  
  57.     this->name = source.name;
  58.     this->sname = source.sname;
  59.     this->height = source.height;
  60.     this->age = source.age;
  61.     this->handling = source.handling;
  62.  
  63.  
  64. }
  65. Player::Player(string name, string sname, string _height, string age, string handling)
  66. {
  67.  
  68.     this->name = name;
  69.     this->sname = sname;
  70.     this->height = _height;
  71.     this->age = age;
  72.     this->handling = handling;
  73. }
  74.  
  75.  
  76.  
  77. class Team
  78. {
  79. public:
  80.     vector<Player> players;
  81.     int add(Player p1, Player p2, Player p3, Player p4, Player p5)
  82.     {
  83.         players.push_back(p1);
  84.         players.push_back(p2);
  85.         players.push_back(p3);
  86.         players.push_back(p4);
  87.         players.push_back(p5);
  88.         return 0;
  89.     }
  90.     void addOne(Player P)
  91.     {
  92.         players.push_back(P);
  93.     }
  94.  
  95.     Team() { ; }
  96.     Team(const Team &source);
  97.     void Treview();
  98.     void modPlayer(int who);
  99.     int readFromFile();
  100.     void saveToFile(string teamname);
  101.  
  102. };
  103.  
  104. void Team::modPlayer(int who)
  105. {
  106.     players[who].modify();
  107. }
  108.  
  109. Team::Team(const Team &source)
  110. {
  111.     this->players = source.players;
  112. }
  113.  
  114. void Team::Treview()
  115. {
  116.     cout.width(15); cout << "Name: ";
  117.     cout.width(20); cout << "Surname: ";
  118.     cout.width(9); cout << "Height: ";
  119.     cout.width(9); cout << "Age: ";
  120.     cout.width(15); cout << "Handling: ";
  121.     cout << endl;
  122.     for (int i = 0; i != players.size(); i++)
  123.     {
  124.         players[i].review();
  125.     }
  126.     cout << "\n";
  127. }
  128.  
  129. void Team::saveToFile(string teamname)
  130. {
  131.     time_t t = time(0);
  132.     struct tm now;
  133.     localtime_s(&now, &t);
  134.     string currenttime;
  135.     //cout << now.tm_hour << now.tm_min << now.tm_sec << endl;
  136.     currenttime = now.tm_hour + now.tm_min + now.tm_sec;
  137.     cout << currenttime;
  138.     string filename = "";
  139.     filename = filename + "druzyna " + teamname + " " + currenttime + ".txt";
  140.     ofstream outputFile;
  141.     outputFile.open(filename.c_str(), ios::app);
  142.     if (outputFile.good() == false)
  143.     {
  144.         cout << "Could not save this file!" << endl;
  145.     }
  146.     else
  147.     {
  148.         string line;
  149.         int i = 0;
  150.         while (i<players.size())
  151.         {
  152.             line.clear();
  153.             line = players[i].retName() + ';' + players[i].retSname() + ';' + players[i].retHeight() + ';' + players[i].retAge() + ';' + players[i].retHandling();
  154.             outputFile << line << endl;
  155.             ++i;
  156.         }
  157.     }
  158.     outputFile.close();
  159. }
  160. /*void Team::saveToFile()
  161. {
  162.     string filetitle;
  163.     stringstream ss;
  164.  
  165.  
  166. }*/
  167.  
  168. int Team::readFromFile() //dziala git
  169. {
  170.     fstream inputFile;
  171.     inputFile.open("indata.txt", ios_base::in);
  172.     if (inputFile.good() == false)
  173.     {
  174.         cout << "Could not open this file!" << endl;
  175.         return 0;
  176.     }
  177.     else
  178.     {
  179.         string lineAsString;
  180.         while (!inputFile.eof())
  181.         {
  182.             getline(inputFile, lineAsString);
  183.             int length = lineAsString.length();
  184.             int i = 0;
  185.             int j = 0;
  186.             int columnID = 0;
  187.             string a, b, c, d, e;
  188.             do
  189.             {
  190.                 while (i < length)
  191.                 {
  192.                     if (lineAsString[i] != ';' && columnID == 0) a = a + lineAsString[i];
  193.                     if (lineAsString[i] != ';' && columnID == 1) b = b + lineAsString[i];
  194.                     if (lineAsString[i] != ';' && columnID == 2) c = c + lineAsString[i];
  195.                     if (lineAsString[i] != ';' && columnID == 3) d = d + lineAsString[i];
  196.                     if (lineAsString[i] != ';' && columnID == 4) e = e + lineAsString[i];
  197.                     if (lineAsString[i] == ';') columnID++;
  198.                     i++;
  199.                 }
  200.  
  201.                 if (e.length() != 0)
  202.                 {
  203.                     Player P(a, b, c, d, e);
  204.                     this->addOne(P);
  205.                 }
  206.  
  207.                 columnID = 0;
  208.                 a.clear(); b.clear(); c.clear(); d.clear(); e.clear();
  209.                 j++;
  210.             } while (j < 5);
  211.         }
  212.     }
  213.     inputFile.close();
  214.  
  215. }
  216.  
  217.  
  218. int main()
  219. {
  220.     Player p1("Jaroslaw", "Psikuta", "180", "30", "90");
  221.     Player p2("Stachu", "Jones", "182", "70", "9001");
  222.     Player p3("Nie", "Mam", "18", "3", "9");
  223.     Player p4("Juz", "Pomyslow", "184", "29", "70");
  224.     Player p5("Na", "Zawodnikow", "166", "66", "66");
  225.     Player p6("Lew", "Trocki", "150", "63", "117");
  226.     Player p7("Iwan", "Grozny", "199", "24", "191");
  227.     Player p8("Adolf", "Hitler", "175", "45", "200");
  228.     Player p9("Wladimir", "Putin", "180", "68", "195");
  229.     Player p10("Josif", "Stalin", "160", "74", "123");
  230.  
  231.     Team T1;
  232.     T1.add(p1, p2, p3, p4, p5);
  233.     Team T2;
  234.     T2.add(p6, p7, p8, p9, p10);
  235.     Team T3(T2);
  236.     Team T4;
  237.     T4.readFromFile();
  238.  
  239.     /*cout << "T1:" << endl;
  240.     T1.Treview();
  241.     cout << "T2:" << endl;
  242.     T2.Treview();
  243.     cout << "T3 -(kopia T2)- :" << endl;
  244.     T3.Treview();
  245.     */
  246.  
  247.  
  248.  
  249.     T4.modPlayer(2);        //modyfikacja (od 0 do 4) zawodnika drużyny od 1 do 4
  250.  
  251.     cout << "Po modyfikacji: " << endl;
  252.     cout << "T1:" << endl;
  253.     T1.Treview();
  254.     cout << "T2:" << endl;
  255.     T2.Treview();
  256.     cout << "T3 -(kopia T2)- :" << endl;
  257.     T3.Treview();
  258.     cout << "T4 -(z pliku)- :" << endl;
  259.     T4.Treview();
  260.     //zapisywanie drużyn
  261.     //WORK IN PROGRESS
  262.     T1.saveToFile("T1");
  263.     T2.saveToFile("T2");
  264.     T3.saveToFile("T3");
  265.     T4.saveToFile("T4");
  266.     cout << endl << "Koniec" << endl;
  267.     system("pause");
  268.     return 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement