Jednostavni invertar

Napraviti funkcionalnost jednostavnog invertara u RPG igri. Invertar sadrži predmete, gde svaki predmet ima svoj naziv, težinu i vrednost.

Zadatak rešiti u više fajlova: predmet.hpp, predmet.cpp, invertar.hpp, invertar.cpp, main.cpp.

Predmet (Predmet) treba da podržava sledeće funkcionalnosti:

Invertar (Invertar) treba da podržava sledeće funkcionalnosti:

Dodatno: Proširiti invertar funkcijama po sopstvenom nahođenju (npr. uklanjanje predmeta po nazivu, sortiranje po vrednosti).

Ulaz

Sa standardnog ulaza se učitava broj upita $q$ $(1 \le q \le 1000)$. Kapacitet invertara je $1000$. Zatim se učitava $q$ upita oblika:

Garantovano je da broj upita a ne prelazi KAPACITET_INVERTARA, i da se upiti W i V javljaju samo kada invertar nije prazan.

Izlaz

Za upite w i v ispisati odgovarajuću vrednost. Za upite W i V ispisati odgovarajući predmet u formatu <naziv>{w=<tezina>,v=<vrednost>}. Za upit p ispisati invertar u formatu [ <p1>, <p2>, ..., <pn> ], gde je svaki <pi> predmet ispisan u istom formatu.

Primer

Ulaz

5
a sword 10 100
a shield 15 80
p
w
v

Izlaz

[ sword{w=10,v=100}, shield{w=15,v=80} ]
25
180

Primer

Ulaz

6
a apple 1 2
a rock 30 1
a gold 2 50
W
V
p

Izlaz

rock{w=30,v=1}
gold{w=2,v=50}
[ apple{w=1,v=2}, rock{w=30,v=1}, gold{w=2,v=50} ]

Rešenje

predmet.hpp

#ifndef PREDMET_HPP
#define PREDMET_HPP

#include <string>

namespace matf
{
  struct Predmet {
    std::string naziv;
    int tezina;
    int vrednost;
  };

  Predmet napravi_predmet(const std::string& naziv, int tezina, int vrednost);

  void ispisi_predmet(const Predmet& p);

  int uporedi_po_tezini(const Predmet& a, const Predmet& b);
  int uporedi_po_vrednosti(const Predmet& a, const Predmet& b);

} // namespace matf

#endif // PREDMET_HPP

invertar.hpp

#ifndef INVERTAR_HPP
#define INVERTAR_HPP

#include "predmet.hpp"

#include <cstddef>

namespace matf
{
  const std::size_t KAPACITET_INVERTARA = 1000;

  struct Invertar {
    Predmet predmeti[KAPACITET_INVERTARA];
    std::size_t broj = 0;
  };

  // preduslov: inv.broj < KAPACITET_INVERTARA
  void dodaj_predmet(Invertar& inv, const Predmet& p);

  void ispisi_invertar(const Invertar& inv);

  int ukupna_tezina(const Invertar& inv);
  int ukupna_vrednost(const Invertar& inv);

  // preduslov: inv.broj > 0
  const Predmet& najtezi_predmet(const Invertar& inv);
  const Predmet& najvredniji_predmet(const Invertar& inv);

} // namespace matf

#endif // INVERTAR_HPP

predmet.cpp

#include "predmet.hpp"

#include <iostream>

namespace matf
{

  Predmet napravi_predmet(const std::string& naziv, int tezina, int vrednost)
  {
    return Predmet{naziv, tezina, vrednost};
  }

  void ispisi_predmet(const Predmet& p)
  {
    std::cout << p.naziv << "{w=" << p.tezina << ",v=" << p.vrednost << "}";
  }

  int uporedi_po_tezini(const Predmet& a, const Predmet& b)
  {
    return a.tezina - b.tezina;
  }

  int uporedi_po_vrednosti(const Predmet& a, const Predmet& b)
  {
    return a.vrednost - b.vrednost;
  }

} // namespace matf

invertar.cpp

#include "invertar.hpp"

#include <cassert>
#include <iostream>

namespace matf
{

  void dodaj_predmet(Invertar& inv, const Predmet& p)
  {
    assert(inv.broj < KAPACITET_INVERTARA);
    inv.predmeti[inv.broj] = p;
    ++inv.broj;
  }

  void ispisi_invertar(const Invertar& inv)
  {
    std::cout << "[ ";
    for (std::size_t i = 0; i < inv.broj; ++i) {
      ispisi_predmet(inv.predmeti[i]);
      if (i + 1 < inv.broj)
        std::cout << ", ";
    }
    std::cout << " ]" << std::endl;
  }

  int ukupna_tezina(const Invertar& inv)
  {
    int zbir = 0;
    for (std::size_t i = 0; i < inv.broj; ++i)
      zbir += inv.predmeti[i].tezina;
    return zbir;
  }

  int ukupna_vrednost(const Invertar& inv)
  {
    int zbir = 0;
    for (std::size_t i = 0; i < inv.broj; ++i)
      zbir += inv.predmeti[i].vrednost;
    return zbir;
  }

  const Predmet& najtezi_predmet(const Invertar& inv)
  {
    assert(inv.broj > 0);
    std::size_t poz = 0;
    for (std::size_t i = 1; i < inv.broj; ++i)
      if (uporedi_po_tezini(inv.predmeti[i], inv.predmeti[poz]) > 0)
        poz = i;
    return inv.predmeti[poz];
  }

  const Predmet& najvredniji_predmet(const Invertar& inv)
  {
    assert(inv.broj > 0);
    std::size_t poz = 0;
    for (std::size_t i = 1; i < inv.broj; ++i)
      if (uporedi_po_vrednosti(inv.predmeti[i], inv.predmeti[poz]) > 0)
        poz = i;
    return inv.predmeti[poz];
  }

} // namespace matf

main.cpp

#include "invertar.hpp"
#include "predmet.hpp"

#include <iostream>

int main()
{
  int q;
  std::cin >> q;

  matf::Invertar inv;

  while (q--) {
    char op;
    std::cin >> op;

    if (op == 'a') {
      std::string naziv;
      int tezina, vrednost;
      std::cin >> naziv >> tezina >> vrednost;
      matf::dodaj_predmet(inv, matf::napravi_predmet(naziv, tezina, vrednost));
    } else if (op == 'p') {
      matf::ispisi_invertar(inv);
    } else if (op == 'w') {
      std::cout << matf::ukupna_tezina(inv) << std::endl;
    } else if (op == 'v') {
      std::cout << matf::ukupna_vrednost(inv) << std::endl;
    } else if (op == 'W') {
      matf::ispisi_predmet(matf::najtezi_predmet(inv));
      std::cout << std::endl;
    } else if (op == 'V') {
      matf::ispisi_predmet(matf::najvredniji_predmet(inv));
      std::cout << std::endl;
    }
  }

  return 0;
}