getters for statelist; move classes;
This commit is contained in:
120
pr6.cpp
120
pr6.cpp
@@ -4,30 +4,6 @@
|
|||||||
|
|
||||||
using namespace std;
|
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>
|
template<class T>
|
||||||
class Element {
|
class Element {
|
||||||
protected:
|
protected:
|
||||||
@@ -57,6 +33,7 @@ public:
|
|||||||
|
|
||||||
template<class T1> friend class LinkedList;
|
template<class T1> friend class LinkedList;
|
||||||
template<class T1> friend class DoublyLinkedStack;
|
template<class T1> friend class DoublyLinkedStack;
|
||||||
|
template<class T1> friend ostream& operator<<(ostream& s, const Element<T1>& el);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T1>
|
template<class T1>
|
||||||
@@ -79,6 +56,8 @@ public:
|
|||||||
virtual Element<T>* push(const T& value) = 0;
|
virtual Element<T>* push(const T& value) = 0;
|
||||||
|
|
||||||
bool isEmpty() { return count == 0; }
|
bool isEmpty() { return count == 0; }
|
||||||
|
//getter
|
||||||
|
Element<T>* getHead() const { return head; }
|
||||||
|
|
||||||
virtual ~LinkedList() {
|
virtual ~LinkedList() {
|
||||||
cout << "\nBase class destructor\n";
|
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() {
|
int main() {
|
||||||
DoublyLinkedStack<int> stack;
|
DoublyLinkedStack<int> stack;
|
||||||
|
|
||||||
@@ -439,5 +495,21 @@ int main() {
|
|||||||
}
|
}
|
||||||
cout << endl;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user