Untitled
unknown
plain_text
2 years ago
4.0 kB
6
Indexable
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>
using namespace std;
// ZADANIE 1 //
class Slowa{
protected:
vector<string> zestaw;
int dlg;
int ile;
public:
Slowa(int dlugosc) : dlg(dlugosc), ile{0} {}
virtual ~Slowa() {}
virtual void wstaw (const string & slowo){
if(slowo.length() == dlg){
zestaw.push_back(slowo);
ile++;
}
}
virtual void zmodyfikuj_zestaw() = 0;
}
// ZADANIE 2 //
class Slowa_wybrane : public Slowa {
private:
char litera;
public:
Slowa_wybrane(int dlugosc, char litera) : Slowa(dlugosc), litera(litera) {}
virtual void wstaw(const string& slowo) {
if (slowo.length() == dlg) {
if (slowo[0] != litera) {
if (find(zestaw.begin(), zestaw.end(), slowo) == zestaw.end()) {
zestaw.push_back(slowo);
ile++;
} else {
cout << "Słowo '" << slowo << "' już istnieje w zestawie." << endl;
}
} else {
cout << "Słowo '" << slowo << "' zaczyna się na literę '" << litera << "' i nie może być dodane." << endl;
}
}
}
virtual void zmodyfikuj_zestaw() {
sort(zestaw.begin(), zestaw.end());
}
Slowa_wybrane operator-(char litera_bis) {
Slowa_wybrane nowy_obiekt(dlg, litera);
for (const string& slowo : zestaw) {
if (slowo[0] != litera_bis) {
nowy_obiekt.wstaw(slowo);
}
}
return nowy_obiekt;
}
void info() {
for (const auto& s : zestaw) {
cout << s << endl;
}
}
};
class Pojemnik_slow {
private:
map<int, Slowa_wybrane*> pojemnik;
public:
void dodaj_slowo(const string& slowo) {
int dlugosc = slowo.length();
if (pojemnik.find(dlugosc) == pojemnik.end()) {
pojemnik[dlugosc] = new Slowa_wybrane(dlugosc, 's');
}
pojemnik[dlugosc]->wstaw(slowo);
}
void wyswietl() {
for (auto it = pojemnik.begin(); it != pojemnik.end(); ++it) {
cout << it->first << ": ";
it->second->info();
cout << endl;
}
}
};
int main(){
bool on = true;
do {
int zadanie;
cout << "Prosze wybrac numer zadania (1-3) - 4 konczy dzialanie programu" << endl;
cin >> zadanie;
switch (zadanie) {
case 1: {
cout << "------------------------" << endl;
cout << "Zadanie 1" << endl;
cout << "To zadanie nie wymaga wyswietlania niczego w konsoli" << endl;
cout << "------------------------" << endl;
break;
}
case 2: {
cout << "------------------------" << endl;
cout << "Zadanie 1" << endl;
Slowa_wybrane sw(5, 'p');
string slowa[] = {"Kolos", "arras", "Wisla", "stora", "ogrom", "Libia", "patio", "kwiat", "sanie"};
for (const auto& s : slowa) {
sw.wstaw(s);
}
sw.info();
sw = sw - 's';
sw.info();
sw.zmodyfikuj_zestaw();
sw.info();
cout << "------------------------" << endl;
break;
}
case 3: {
cout << "------------------------" << endl;
cout << "Zadanie 1" << endl;
Pojemnik_slow zestawy_slow;
ifstream plik("plik.txt");
string slowo;
while (plik >> slowo) {
zestawy_slow.dodaj_slowo(slowo);
}
zestawy_slow.wyswietl();
cout << "------------------------" << endl;
break;
}
case 4: {
on = false;Editor is loading...