Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Zadanie 4.
- #include <iostream>
- #include <fstream>
- #include <conio.h>
- using namespace std;
- class Komputer
- {
- string nazwa;
- double cena;
- public:
- Komputer(string, double);
- void wyswietl() const;
- string get_nazwa() const;
- double get_cena() const;
- };
- Komputer::Komputer(string n, double c) : nazwa(n), cena(c) {}
- void Komputer::wyswietl() const
- {
- cout << nazwa << '\t' << cena << endl;
- }
- string Komputer::get_nazwa() const
- {
- return nazwa;
- }
- double Komputer::get_cena() const
- {
- return cena;
- }
- int main()
- {
- Komputer* tablica[25];
- unsigned ile = 0;
- bool flaga = true;
- char komenda;
- string nazwa_pliku, nazwa;
- double cena;
- ofstream plik_out;
- ifstream plik_in;
- while(flaga)
- {
- cout << "Co chcesz zrobic?\n";
- cout << "\tR - wczytaj komputery z pliku\n";
- cout << "\tN - dodaj nowy komputer\n";
- cout << "\tW - wyswietl wszystkie komputery\n";
- cout << "\tZ - zapisz dane do pliku\n";
- cout << "\tK - wyjdz z programu\n";
- do
- {
- komenda = _getch();
- komenda = toupper(komenda);
- } while(komenda != 'R' && komenda != 'N' && komenda != 'W' && komenda != 'Z' && komenda != 'K');
- switch(komenda)
- {
- case 'R':
- cout << "Podaj nazwe pliku (wraz z rozszerzeniem): ";
- cin >> nazwa_pliku;
- plik_in.open(nazwa_pliku);
- if(plik_in.fail())
- cout << "Nie udalo sie otworzyc takiego pliku\n";
- else
- {
- for(unsigned i = 0; i < ile; ++i)
- delete tablica[i];
- plik_in >> ile;
- for(unsigned i = 0; i < ile; ++i)
- {
- plik_in >> nazwa >> cena;
- tablica[i] = new Komputer(nazwa, cena);
- }
- cout << "Pomyslnie pobrano komputery z pliku\n";
- plik_in.close();
- }
- break;
- case 'N':
- cout << "Podaj nazwe i cene komputera: ";
- cin >> nazwa >> cena;
- tablica[ile] = new Komputer(nazwa, cena);
- ++ile;
- break;
- case 'W':
- for(unsigned i = 0; i < ile; ++i)
- tablica[i]->wyswietl();
- break;
- case 'Z':
- cout << "Podaj nazwe pliku (wraz z rozszerzeniem): ";
- cin >> nazwa_pliku;
- plik_out.open(nazwa_pliku);
- plik_out << ile << endl;
- for(unsigned i = 0; i < ile; ++i)
- plik_out << tablica[i]->get_nazwa() << ' ' << tablica[i]->get_cena() << endl;
- plik_out.close();
- break;
- case 'K':
- cout << "Nastapi zamkniecie programu. Wcisnij dowolny przycisk...";
- _getch();
- flaga = false;
- }
- }
- for(unsigned i = 0; i < ile; ++i)
- delete tablica[i];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement