Advertisement
MeehoweCK

Untitled

Apr 10th, 2021
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. // Zadanie 4.
  2. #include <iostream>
  3. #include <fstream>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. class Komputer
  9. {
  10.     string nazwa;
  11.     double cena;
  12. public:
  13.     Komputer(string, double);
  14.     void wyswietl() const;
  15.     string get_nazwa() const;
  16.     double get_cena() const;
  17. };
  18.  
  19. Komputer::Komputer(string n, double c) : nazwa(n), cena(c) {}
  20.  
  21. void Komputer::wyswietl() const
  22. {
  23.     cout << nazwa << '\t' << cena << endl;
  24. }
  25.  
  26. string Komputer::get_nazwa() const
  27. {
  28.     return nazwa;
  29. }
  30.  
  31. double Komputer::get_cena() const
  32. {
  33.     return cena;
  34. }
  35.  
  36. int main()
  37. {
  38.     Komputer* tablica[25];
  39.     unsigned ile = 0;
  40.     bool flaga = true;
  41.     char komenda;
  42.     string nazwa_pliku, nazwa;
  43.     double cena;
  44.     ofstream plik_out;
  45.     ifstream plik_in;
  46.  
  47.     while(flaga)
  48.     {
  49.         cout << "Co chcesz zrobic?\n";
  50.         cout << "\tR - wczytaj komputery z pliku\n";
  51.         cout << "\tN - dodaj nowy komputer\n";
  52.         cout << "\tW - wyswietl wszystkie komputery\n";
  53.         cout << "\tZ - zapisz dane do pliku\n";
  54.         cout << "\tK - wyjdz z programu\n";
  55.  
  56.         do
  57.         {
  58.             komenda = _getch();
  59.             komenda = toupper(komenda);
  60.         } while(komenda != 'R' && komenda != 'N' && komenda != 'W' && komenda != 'Z' && komenda != 'K');
  61.  
  62.         switch(komenda)
  63.         {
  64.         case 'R':
  65.             cout << "Podaj nazwe pliku (wraz z rozszerzeniem): ";
  66.             cin >> nazwa_pliku;
  67.             plik_in.open(nazwa_pliku);
  68.             if(plik_in.fail())
  69.                 cout << "Nie udalo sie otworzyc takiego pliku\n";
  70.             else
  71.             {
  72.                 for(unsigned i = 0; i < ile; ++i)
  73.                     delete tablica[i];
  74.                 plik_in >> ile;
  75.                 for(unsigned i = 0; i < ile; ++i)
  76.                 {
  77.                     plik_in >> nazwa >> cena;
  78.                     tablica[i] = new Komputer(nazwa, cena);
  79.                 }
  80.                 cout << "Pomyslnie pobrano komputery z pliku\n";
  81.                 plik_in.close();
  82.             }
  83.             break;
  84.         case 'N':
  85.             cout << "Podaj nazwe i cene komputera: ";
  86.             cin >> nazwa >> cena;
  87.             tablica[ile] = new Komputer(nazwa, cena);
  88.             ++ile;
  89.             break;
  90.         case 'W':
  91.             for(unsigned i = 0; i < ile; ++i)
  92.                 tablica[i]->wyswietl();
  93.             break;
  94.         case 'Z':
  95.             cout << "Podaj nazwe pliku (wraz z rozszerzeniem): ";
  96.             cin >> nazwa_pliku;
  97.             plik_out.open(nazwa_pliku);
  98.             plik_out << ile << endl;
  99.             for(unsigned i = 0; i < ile; ++i)
  100.                 plik_out << tablica[i]->get_nazwa() << ' ' << tablica[i]->get_cena() << endl;
  101.             plik_out.close();
  102.             break;
  103.         case 'K':
  104.             cout << "Nastapi zamkniecie programu. Wcisnij dowolny przycisk...";
  105.             _getch();
  106.             flaga = false;
  107.         }
  108.     }
  109.  
  110.     for(unsigned i = 0; i < ile; ++i)
  111.         delete tablica[i];
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement