Witam kończę właśnie projekt programistyczny i mam dwa pytania:
1.Dlaczego nie chce zadziałać mi zwalnianie pamięci?
2.Jak utworzyć funkcję odpowiadającą za zapis list jednkoierunkowych(pasażerów na dany lot) do plików(W moim przypadku to funkcja saveToFiles, jednak to raczej nie ma prawa działać, a może wystarczy tylko jakaś korekta?)
Oto kod:
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <optional>
#include <conio.h>
#include <cassert>
using namespace std;
//pozycja na liście pasażerów
struct PasazerListItem {
string imie;
string nazwisko;
unsigned numerMiejsca;
PasazerListItem* nextPasazer;
};
//pozycja na liście lotów
struct LotListItem {
string symbol;
PasazerListItem* firstPasazer;
LotListItem* nextLot;
};
//dane z pliku
struct Record {
string symbol;
tm data;
string odlot;
string przylot;
string imie;
string nazwisko;
unsigned numerMiejsca;
Record* next;
};
Record parse_record(const string& str)
{
Record record{}; //tworzenie obiektu struktury o nazwie record
istringstream ss{ str };
ss >> record.symbol
>> get_time(&(record.data), "%d.%m.%Y")
>> record.odlot
>> record.przylot
>> record.imie
>> record.nazwisko
>> record.numerMiejsca;
if (!ss)
{
cerr << "Bledny rekord danych!";
exit(0);
}
return record;
}
void show_record(const Record& record) {
cout << "Symbol Lotu: " << record.symbol << '\n'
<< "Data: " << put_time(&(record.data), "%d-%m-%Y") << '\n'
<< "Odlot: " << record.odlot << '\n'
<< "Przylot: " << record.przylot << '\n'
<< "Imie: " << record.imie << '\n'
<< "Nawisko: " << record.nazwisko << '\n'
<< "Miejsce: " << record.numerMiejsca << '\n';
}
//funkcja dodaje lot i pasażera do lotu
void add_lot_pasazer(LotListItem* head, const Record& record)
{
LotListItem* itemLot = head;
LotListItem* nowyLot = nullptr;
PasazerListItem* itemPasazer = nullptr;
PasazerListItem* nowyPasazer = nullptr;
if (itemLot->symbol == "")
{
itemLot->symbol = record.symbol;
}
else
{
while (itemLot->symbol != record.symbol)
{
if (itemLot->nextLot == nullptr)
{
nowyLot = new LotListItem();
nowyLot->symbol = record.symbol;
nowyLot->firstPasazer = nullptr;
nowyLot->nextLot = nullptr;
itemLot->nextLot = nowyLot;
itemLot = nowyLot;
break;
}
else
{
itemLot = itemLot->nextLot;
}
}
}
if (itemLot->firstPasazer == nullptr)
{
PasazerListItem* nowyPasazer = new PasazerListItem();
nowyPasazer->imie = record.imie;
nowyPasazer->nazwisko = record.nazwisko;
nowyPasazer->numerMiejsca = record.numerMiejsca;
nowyPasazer->nextPasazer = nullptr;
itemLot->firstPasazer = nowyPasazer;
}
else
{
itemPasazer = itemLot->firstPasazer;
while (itemPasazer->nextPasazer != nullptr)
itemPasazer = itemPasazer->nextPasazer;
PasazerListItem* nowyPasazer = new PasazerListItem();
nowyPasazer->imie = record.imie;
nowyPasazer->nazwisko = record.nazwisko;
nowyPasazer->numerMiejsca = record.numerMiejsca;
nowyPasazer->nextPasazer = nullptr;
itemPasazer->nextPasazer = nowyPasazer;
}
}
//wyświetla na ekranie listę lotów wraz z przypisanymi pasażerami
void show_all(LotListItem* head)
{
LotListItem* itemLot = head;
PasazerListItem* itemPasazer = nullptr;
if (itemLot->symbol != "")
{
do
{
cout << "Symbol Lotu: " << itemLot->symbol << endl;
if (itemLot->firstPasazer != nullptr)
{
itemPasazer = itemLot->firstPasazer;
while (itemPasazer->nextPasazer != nullptr)
{
cout << " PASAZER: " << itemPasazer->imie << " " << itemPasazer->nazwisko << " " << itemPasazer->numerMiejsca << endl;
itemPasazer = itemPasazer->nextPasazer;
}
cout << " PASAZER: " << itemPasazer->imie << " " << itemPasazer->nazwisko << " " << itemPasazer->numerMiejsca << endl;
}
else
{
cout << " * BRAK PASAZEROW *" << '\n';
}
itemLot = itemLot->nextLot;
} while (itemLot != nullptr);
}
}
void saveToFiles(const Record& record) //liste pasazerow zapisac do plikow BER LON MAD
{
while (!false)
{
fstream file{ "DaneLotu.txt" };
if (!file) {
cout << "Blad otwarcia pliku!\n";
}
string line;
getline(file, line);
const auto record = parse_record(line);
//show_record(record);
//cout << '\n';
//_getch();
string BER = "BER", AMS = "AMS", LON = "LON", MAD = "MAD";
if (record.symbol == BER)
{
fstream pliczek;
pliczek.open("BER.txt", ios::app);
cout << record.symbol;
pliczek.close();
}
if (record.symbol == AMS)
{
fstream pliczek;
pliczek.open("AMS.txt", ios::app);
pliczek.close();
}
if (record.symbol == LON)
{
fstream pliczek;
pliczek.open("LON.txt", ios::app);
pliczek.close();
}
if (record.symbol == MAD)
{
fstream pliczek;
pliczek.open("MAD.txt", ios::app);
pliczek.close();
}
_getch();
}
}
void usunListeIteracyjnie(LotListItem* head)
{
LotListItem* itemLot = head;
PasazerListItem* itemPasazer = nullptr;
while (head)
{
auto p = head->nextLot;
delete head;
head = p;
}
}
int main()
{
LotListItem* head = new LotListItem;
head->symbol = "";
head->firstPasazer = nullptr;
head->nextLot = nullptr;
fstream file{ "DaneLotu.txt" };
if (!file) {
cout << "Blad otwarcia pliku!\n";
return 0;
}
while (!false)
{
string line;
getline(file, line);
if (line == "")
break;
const auto record = parse_record(line);
show_record(record);
add_lot_pasazer(head, record);
cout << '\n';
}
show_all(head);
//zapisanie do plików
//saveToFiles(record);
//zwolnienie pamięci
usunListeIteracyjnie(head);
cout << _CrtDumpMemoryLeaks();
}
Program to generalnie baza lotów na lotnisku, zczytuje z jednego pliku i zapisuje do mniejszych z podziałem na symbole. Ponadto w tych mniejszych plikach osoby w pliku muszą być posortowane ze wzgledu na miejsca.