Witam,
Czy mógłby ktoś bardziej doświadczony ocenić ten kod pod kątem poprawności , estetyki czy też nie ma żadnych wycieków pamięci ? Po kompilacji konsola nie wspomina o żadnych błędach ani wyciekach pamięci.
Program ma za zadanie wczytać z pliku zdania które są zapisane od tyłu ( zamiast "Ala ma kota." to "kota ma Ala."
i wypisać w odpowiedniej kolejności.
#include <iostream>
#include <fstream>
using namespace std;
struct stos
{
string slowo;
stos* next;
};
void add_stos(string slowo, stos*& top);
void show_stos(stos*& top, char znak_konca);
void remove_from_stos(stos*& top);
void wczytywanko(string sciezka, stos*& top);
int main()
{
stos* top = nullptr;
string sciezka = "dane.txt";
wczytywanko(sciezka, top);
return 0;
}
void wczytywanko(string sciezka, stos*& top)
{
ifstream plik;
string tekst_temp;
int d;
char znak_konca;
plik.open(sciezka);
while (!plik.eof())
{
getline(plik, tekst_temp, ' ');
d = tekst_temp.length();
if (tekst_temp[d - 1] == '.' || tekst_temp[d - 1] == '!' || tekst_temp[d - 1] == '?')
{
znak_konca = tekst_temp[d - 1];
tekst_temp[d - 1] = ' ';
add_stos(tekst_temp, top);
show_stos(top, znak_konca);
}
else
{
add_stos(tekst_temp, top);
}
}
znak_konca = ' \b';
show_stos(top, znak_konca);
plik.close();
}
void add_stos(string slowo, stos*& top)
{
stos* temp = new stos;
temp->slowo = slowo;
temp->next = top;
top = temp;
}
void show_stos(stos*& top, char znak_konca)
{
int i = 0;
while (top != nullptr)
{
if (i == 0)
{
cout << top->slowo;
top = top->next;
i++;
}
else
{
cout << top->slowo << " ";
top = top->next;
i++;
}
}
cout << "\b" << znak_konca << endl;
}
void remove_from_stos(stos*& top)
{
if (top == nullptr)
{
cout << "Pusty stos" << endl;
}
else
{
stos* temp = top;
top = top->next;
delete temp;
}
}