getters for statelist; move classes;

This commit is contained in:
gandc 2024-05-23 00:49:29 +03:00
parent 9284b28471
commit 5ed52b5e02
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

120
pr6.cpp
View File

@ -4,30 +4,6 @@
using namespace std;
class State {
public:
string name;
string capital;
string language;
long population;
double area;
State() : name(""), capital(""), language(""), population(0), area(0.0) {}
State(string n, string c, string l, long p, double a)
: name(n), capital(c), language(l), population(p), area(a) {}
friend ostream& operator<<(ostream& os, const State& state) {
os << "State: " << state.name << ", Capital: " << state.capital
<< ", Language: " << state.language << ", Population: " << state.population
<< ", Area: " << state.area;
return os;
}
bool operator==(const State& other) const {
return name == other.name;
}
};
template<class T>
class Element {
protected:
@ -57,6 +33,7 @@ public:
template<class T1> friend class LinkedList;
template<class T1> friend class DoublyLinkedStack;
template<class T1> friend ostream& operator<<(ostream& s, const Element<T1>& el);
};
template<class T1>
@ -79,6 +56,8 @@ public:
virtual Element<T>* push(const T& value) = 0;
bool isEmpty() { return count == 0; }
//getter
Element<T>* getHead() const { return head; }
virtual ~LinkedList() {
cout << "\nBase class destructor\n";
@ -389,6 +368,83 @@ public:
}
};
class State {
protected:
string name;
string capital;
string language;
long population;
double area;
public:
State() : name(""), capital(""), language(""), population(0), area(0.0) {}
State(string n, string c, string l, long p, double a)
: name(n), capital(c), language(l), population(p), area(a) {}
friend ostream& operator<<(ostream& os, const State& state) {
os << "State: " << state.name << ", Capital: " << state.capital
<< ", Language: " << state.language << ", Population: " << state.population
<< ", Area: " << state.area;
return os;
}
bool operator==(const State& other) const {
return name == other.name;
}
// geters
string getName() const { return name; }
double getArea() const { return area; }
};
class StateList {
protected:
SinglyLinkedListQueue<State> stateList;
public:
// Добавление нового государства в начало списка
void addState(const State& state) {
stateList.push(state);
}
// Удаление государства из начала списка
State removeState() {
Element<State>* element = stateList.pop();
if (element) {
State state = element->getInfo();
delete element;
return state;
}
throw logic_error("State list is empty");
}
// Поиск государства по названию
State findStateByName(const string& name) {
Element<State>* current = stateList.getHead();
while (current) {
if (current->getInfo().getName() == name) {
return current->getInfo();
}
current = current->getNext();
}
throw logic_error("State not found");
}
// Фильтрация государств по площади
void printStatesByArea(double minArea, double maxArea) {
Element<State>* current = stateList.getHead();
while (current != nullptr) {
const State& state = current->getInfo();
if (state.getArea() >= minArea && state.getArea() <= maxArea) {
cout << state << endl;
}
current = current->getNext();
}
}
};
int main() {
DoublyLinkedStack<int> stack;
@ -439,5 +495,21 @@ int main() {
}
cout << endl;
//StateList
StateList stateList;
// Пример использования
State usa("USA", "Washington D.C.", "English", 328000000, 9833510);
State india("India", "New Delhi", "Hindi", 1380000000, 3287263);
stateList.addState(usa);
stateList.addState(india);
cout << "List of states:" << endl;
cout << stateList.findStateByName("USA") << endl;
cout << "Removing state:" << endl;
cout << stateList.removeState() << endl;
return 0;
}