我有一个单词列表和一个包含许多字谜的文件。这些字谜是在单词列表中找到的单词。我需要开发一种算法来查找匹配的单词并将它们生成在输出文件中。到目前为止,我开发的代码仅适用于前两个单词。此外,我无法让代码与其中任何地方包含数字的字符串一起玩得很好。请告诉我如何修复代码。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (void)
{
int x = 0, y = 0;
int a = 0, b = 0;
int emptyx, emptyy;
int match = 0;
ifstream f1, f2;
ofstream f3;
string line, line1[1500], line2[50];
size_t found;
f1.open ("wordlist.txt");
f2.open ("file.txt");
f3.open ("output.txt");
while (f1.eof() == 0)
{
getline (f1, line);
line1[x] = line;
x++;
}
while (f2.eof() == 0)
{
getline (f2, line);
line2[y] = line;
y++;
}
//finds position of last elements
emptyx = x-1;
emptyy = y-1;
//matching algorithm
for (y = 0; y <= emptyy; y++)
{
for (x = 0; x <= emptyx; x++)
{
if (line2[y].length() == line1[x].length())
{
for (a = 0; a < line1[x].length(); a++)
{
found = line2[y].find(line1[x][a]);
if (found != string::npos)
{
match++;
line2[y].replace(found, 1, 1, '.');
if (match == line1[x].length())
{
f3 << line1[x] << ", ";
match = 0;
}
}
}
}
}
}
f1.close();
f2.close();
f3.close();
return 0;
}