Artmetic Qt/C++ Anagramer. Find words in dictionary file C++/Qt.

Anagramer. Find words in dictionary file C++/Qt.

anagram C++

Below sample of code to find word containing the same letters + one that is not the same but create new word:

tor -> sort -> sport

The code can get dictionary and word that should be found:

#include <iostream>
#include <qapplication>
#include <qstringlist>
#include <qfile>
#include <qdebug></qdebug></qfile></qstringlist></qapplication></iostream>

using namespace std;

// oure game of words that matches pattern
QString findWord(QStringList &amp;list, QString myWord)
{
bool pass;
foreach (QString word, list)
{
pass = true;
int i = 0;
while(i &lt; myWord.size())
        {
            if(!word.contains(myWord.at(i++)))
            {
                //             qDebug() &lt;&lt;    word &lt;<word.at(i-1); pass="false;" break;="" }="" word="" should="" has="" only="" one="" letter="" that="" can="" be="" other="" than="" we="" think="" if(pass="" &&="" myword.size()="" +1="=" word.size())="" return="" word;="" if="" not="" found="" empty="" string="" "";="" struct="" otherway{="" otherway(qstringlist="" list,="" qstringlist="" finds,="" qstring="" alternative)="" :="" list(list),="" finds(finds),="" alternative(alternative){="" qdebug()="" <<="" "alternative:="" "="" list="" finds="" alternative;="" list;="" finds;="" };="" int="" main(int="" argc,="" char="" *argv[])="" {="" qapplication="" a(argc,="" argv);="" args="a.arguments();" start="" for="" finding="" solution="" found;="" file;="" if(args.size()=""> 1)
found = args.at(1);
if(args.size() &gt; 2)
file = args.at(2);</word.at(i-1);>

QStringList list = {"ail", "tennis", "fails", "desk", "stay","plain", "table", "engine", "sail"};

// read the file with the words
if(!file.isEmpty())
{
QFile inputFile(file);
if(inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
// clear default list
list.clear();
QTextStream in(&amp;inputFile);
while (!in.atEnd())
list.append(in.readLine());
inputFile.close();
}

}

// list of finds that match
QStringList finds;

QString tmp;
// append word that we started
finds.append(found);

QList <qstringlist> alternativeList;</qstringlist>

QList <otherway> otherWays;
do
{
do
{
// search for the word
tmp = found;
found = findWord(list, found);
// remove that we found. That protect as from infinite loops
list.removeOne(found);</otherway>

if(!findWord(list,tmp).isEmpty())
{
OtherWay way(list,finds, findWord(list,tmp));
way.finds.append(findWord(list,tmp));
otherWays.append(way);
}

if(!found.isEmpty())
finds.append(found);
}while(!found.isEmpty());

if(otherWays.isEmpty())
break;

alternativeList.append(finds);

list = otherWays.first().list;
found = otherWays.first().alternative;
finds = otherWays.first().finds;
otherWays.removeFirst();

}while(otherWays.size() &gt;= 0);

if(finds.isEmpty())
{
qDebug() &lt;&lt; "No solutions!";
        return 0;
    }

    alternativeList.append(finds);

// And finly we find from all alternatives list the longest that we want to print
    int id = 0;
    int max = 0;
    for(int i =0; i &lt; alternativeList.size(); i++)
        if(max &lt; alternativeList.at(i).size())
        {
            id = i;
            max = alternativeList.at(i).size();
        }
    qDebug() &lt;&lt; "The longest list: " &lt;&lt; alternativeList.at(id);

    return 0;
}

Leave a Reply

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.

Related Post