Muszę usunąć ostatni znak ze słowa które ułożone są na stosie niestety w mojej wersji programu nie to nie działa. Zadanie polega na wczytaniu tekstu który jest zapisany odwrotnie i zapisanie go poprawnie za pomocą stosu oraz usunięcie znaków interpunkcyjnych z końca kilku złów i wstawienie ich w odpowiednim miejscu, niestety o ile wstawienie znaku działa, tak usunięcie ostatniego znaku z podanego stringa już nie.
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
using namespace std;
/*
push - dodawanie elemetnow
pop - usuwanie elementu
isEmpty - sprawdzanie czy pusty
top - pobranie bez usuniecia
*/
struct stos
{
string key;
stos *next;
};
bool isEmpty(stos* s);
void push(stos* &s, string newVal);
void pop(stos* &s);
string top(stos *s);
void viewIfEmpty(stos *s);
int main()
{
stos* st1 = nullptr;
ifstream file("inversed.txt");
string word;
while (!file.eof())
{
file>>word;
push(st1,word);
}
pop(st1);
if(st1!=nullptr)
{
while(st1!=nullptr)
{
if (top(st1)[top(st1).size()-1]=='.')
{
cout <<"\b."<<endl;
top(st1).pop_back();
}
if (top(st1)[top(st1).size()-1]=='?')
{
cout <<"\b?"<<endl;
top(st1).pop_back();
}
if (top(st1)[top(st1).size()-1]=='!')
{
cout <<"\b!"<<endl;
top(st1).pop_back();
}
cout<<top(st1)<< " ";
pop(st1);
}
}
return 0;
}
bool isEmpty(stos* s)
{
return (s==nullptr);
}
void push(stos* &s, string newVal)
{
stos* newStos = new stos;
newStos -> key = newVal;
newStos -> next = s;
s = newStos;
}
void pop(stos* &s)
{
if(!isEmpty(s))
{
stos* temp = s; //zapisac w temp adres gory
s = s -> next;//przesunac gore stosu
delete temp;//usunac odlaczany element
}
}
string top(stos *s)
{
return s->key;
}