save();load(); fix for printing "list of states".
This commit is contained in:
parent
d265b73748
commit
38c584323c
76
pr6.cpp
76
pr6.cpp
@ -1,6 +1,9 @@
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -421,6 +424,9 @@ public:
|
||||
void addState(const State& state) {
|
||||
stateList.push(state);
|
||||
}
|
||||
Element<State>* getStateListHead() const {
|
||||
return stateList.getHead();
|
||||
}
|
||||
|
||||
// Удаление государства из начала списка
|
||||
State removeState() {
|
||||
@ -442,7 +448,6 @@ public:
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
/*throw logic_error("State not found");*/
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -469,6 +474,57 @@ public:
|
||||
current = current->getNext();
|
||||
}
|
||||
}
|
||||
|
||||
// Сохранение списка состояний в файл
|
||||
void save(const string& filename) const {
|
||||
ofstream outputFile(filename);
|
||||
if (!outputFile.is_open()) {
|
||||
throw runtime_error("Unable to open file for writing");
|
||||
}
|
||||
|
||||
Element<State>* current = stateList.getHead();
|
||||
while (current != nullptr) {
|
||||
outputFile << current->getInfo().getName() << ","
|
||||
<< current->getInfo().getCapital() << ","
|
||||
<< current->getInfo().getLanguage() << ","
|
||||
<< current->getInfo().getPopulation() << ","
|
||||
<< current->getInfo().getArea() << endl;
|
||||
current = current->getNext();
|
||||
}
|
||||
|
||||
outputFile.close();
|
||||
}
|
||||
|
||||
// Загрузка списка состояний из файла
|
||||
void load(const string& filename) {
|
||||
ifstream file(filename);
|
||||
if (file.is_open()) {
|
||||
// Очищаем текущий список состояний перед загрузкой новых данных
|
||||
while (!stateList.isEmpty()) {
|
||||
stateList.pop();
|
||||
}
|
||||
|
||||
string line;
|
||||
while (getline(file, line)) {
|
||||
stringstream ss(line);
|
||||
string name, capital, language;
|
||||
long population;
|
||||
double area;
|
||||
getline(ss, name, ',');
|
||||
getline(ss, capital, ',');
|
||||
getline(ss, language, ',');
|
||||
ss >> population;
|
||||
ss.ignore(1); // Ignore the comma
|
||||
ss >> area;
|
||||
State state(name, capital, language, population, area);
|
||||
stateList.push(state);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
else {
|
||||
cout << "Unable to open file for loading" << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -530,8 +586,24 @@ int main() {
|
||||
states->addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
|
||||
states->addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
|
||||
|
||||
// Сохраняем список состояний в файл
|
||||
states->save("states.txt");
|
||||
|
||||
// Загружаем список состояний из файла
|
||||
StateList loadedStates;
|
||||
loadedStates.load("states.txt");
|
||||
|
||||
cout << "List of states:" << endl;
|
||||
cout << states->findStateByName("USA") << endl;
|
||||
//cout << states->findStateByName("USA") << endl;
|
||||
Element<State>* current = states->getStateListHead();
|
||||
while (current != nullptr) {
|
||||
if (current->getInfo().getName() != "") {
|
||||
cout << "State: " << current->getInfo().getName() << ", Capital: " << current->getInfo().getCapital()
|
||||
<< ", Language: " << current->getInfo().getLanguage() << ", Population: " << current->getInfo().getPopulation()
|
||||
<< ", Area: " << current->getInfo().getArea() << endl;
|
||||
}
|
||||
current = current->getNext();
|
||||
}
|
||||
|
||||
cout << "States with area between 9800500 and 9987263:" << endl;
|
||||
states->printStatesByArea(9800500, 9987263);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user