fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <ostream>
  5.  
  6. class Kot {
  7. private:
  8. std::string gatunek;
  9. int wielkosc;
  10. public:
  11. Kot(std::string gatunek,int wielkosc) :
  12. gatunek(gatunek), wielkosc(wielkosc) {}
  13. Kot() : gatunek(""), wielkosc(0) {}
  14. Kot& operator+=(const Kot& k) {
  15. wielkosc += k.wielkosc;
  16. gatunek += (gatunek.empty() ? k.gatunek : " "+k.gatunek);
  17. return *this;
  18. }
  19. friend std::ostream& operator<<(std::ostream& out,Kot &k) {
  20. std::stringstream w;
  21. w << k.wielkosc;
  22. out << k.gatunek+" "+w.str();
  23. return out;
  24. }
  25. };
  26.  
  27. class ListaKotow {
  28. private:
  29. typedef struct Element {
  30. Element *nastepny;
  31. Kot kot;
  32. } Element;
  33. Element *pierwszy;
  34. Element *ostatni;
  35. public:
  36. ListaKotow() : pierwszy(NULL), ostatni(NULL) {}
  37. void push_back(Kot k) {
  38. Element *e = new Element;
  39. e->kot = k;
  40. e->nastepny = NULL;
  41. if(!pierwszy && !ostatni)
  42. pierwszy = e;
  43. else
  44. ostatni->nastepny = e;
  45. ostatni = e;
  46. }
  47. ~ListaKotow() {
  48. Element *ns, *s = pierwszy;
  49. while(s) {
  50. ns = s->nastepny;
  51. delete s;
  52. s = ns;
  53. }
  54. }
  55. Kot dajSuperKotaMieszanca() {
  56. Kot superKotMieszaniec;
  57. Element *e;
  58. for(e = pierwszy; e; e=e->nastepny)
  59. superKotMieszaniec += e->kot;
  60. return superKotMieszaniec;
  61. }
  62. };
  63.  
  64. int main(void) {
  65. ListaKotow koty;
  66. koty.push_back(Kot("Perski",12));
  67. koty.push_back(Kot("Brytyjski",10));
  68. koty.push_back(Kot("Egzotyczny",8));
  69. koty.push_back(Kot("Ragdoll",10));
  70. koty.push_back(Kot("Norweski",22));
  71. Kot mieszaniec = koty.dajSuperKotaMieszanca();
  72. std::cout << mieszaniec << std::endl;
  73. return 0;
  74. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Perski Brytyjski Egzotyczny Ragdoll Norweski 62