D
D
DDD2019-03-01 14:03:05
Qt
DDD, 2019-03-01 14:03:05

Why doesn't palindrome work?

Connoisseurs of Qt please help, the task is simple, you just need the program to read the file selected by the user as a palindrome and not a palindrome.
I implemented a selection for a file and a palindrome check algorithm, but it does not work correctly, everything is thrown into NOT Palindorm
widget.cpp

#include "QtNameP.h"
#include "ui_widget.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QString>
#include <algorithm>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    button{new QPushButton{"Add File",this}},
    ListP{new QTextEdit{"Palindrom",this}},
    ListNotP{new QTextEdit{"Not Palindrom",this}}
{
    QHBoxLayout* hlayo{new QHBoxLayout};
    hlayo->setMargin(50);
    hlayo->setSpacing(50);
    hlayo->addWidget(ListP);
    hlayo->addWidget(ListNotP);
    this->setLayout(hlayo);


    connect(button,&QPushButton::clicked,
            this,&Widget::AddFile);
//    connect(this,&Widget::AddFile,
//            this,QOverload<>::of(&Widget::CheckPalindrome));
}

Widget::~Widget()
{

}

void Widget::AddFile()
{
    QString filename=QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                tr("All File (*.*);;Text File(*.txt)"
                ));

    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly))
        QMessageBox::information(this,tr("Unable to open file"),file.errorString());
    Pal(file);
}

void Widget::Pal(QFile& file)
{
    QTextStream in (&file);
    QString line=in.readAll();
//    line.simplified().remove(' ');
    if (std::equal(line.begin(), line.begin() + line.size()/2, line.rbegin()))
    {
    ListP->setText(line);
    }
    else{
    ListNotP->setText(line);
    }
}


void Widget::SaveFile()
{

}

Qtname.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QtWidgets>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    void AddFile();
    void Pal(QFile& file);
    void SaveFile();
protected:
    QPushButton* button;
    QTextEdit* ListP;
    QTextEdit* ListNotP;
};

#endif // WIDGET_H

main.cpp
#include "QtNameP.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pyatibratovaru, 2019-03-23
@pyatibratovaru

In the line if (std::equal(line.begin(), line.begin() + line.size()/2, line.rbegin())) replace line.begin() + line.size()/2 on line.end()
Then it's correct whether it will detect a palindrome or not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question