Stos i usuwanie ostatniego znaku ze słowa

0

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;
}
0

Zmień definicję funkcji top na string& top(stos *s) gdyż teraz usuwasz ostatni element z kopii a nie z orginału.

0
while (file >> word) {
	push(st1, word);
}

while (st1) {
	auto w = top(st1);

	if (ispunct(w.back())) {
		cout << '\b' << w.back() << '\n';
		w.pop_back();
	}

	cout << w << ' ';
	pop(st1);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1