如何更改此代码以获取正确的输出(C++)
程序应从用户中获取输入单词,只有一个字母差打印出所有文件中的所有单词。例如:如果输入为:“ way” 输出应为 [例如,五月,战争,战争,蜡,蜡,是...] 我的代码是找到单词仅使用输入的确切单词。就像输入是“ start” ,输出为 [重新启动,启动,启动...] 如何更改它以像上述示例一样获取输出?这是一个代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void fileRead(vector<string>& v, ifstream& fin) {
string line;
while (getline(fin, line)) {
v.push_back(line);
}
}
void search(vector<string>& v, string word) {
for (int i = 0; i < v.size(); i++) {
int index = v[i].find(word);
if (index != -1)
cout << v[i] << endl;
}
}
int main() {
vector<string> wordVector;
ifstream fin("text.txt");
if (!fin) {
cout << "text.txt can't opened" << endl;
return 0;
}
fileRead(wordVector, fin);
fin.close();
cout << "reading text.txt." << endl;
while (true) {
cout << "Input a word: >>";
string word;
getline(cin, word);
if (word == "exit")
break;
search(wordVector, word);
}
cout << "Terminated" << endl;
}
Program should take input word from user and print out all the words in file with only one letter difference. For example: if input is: "way" output should be [say, may, day, war, waw, wax, was...]
My code is finding words only with exact word which inputed. Like if input is "start", output is [restart, startup, started...] How can I change it to get output like above example? Here is a code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void fileRead(vector<string>& v, ifstream& fin) {
string line;
while (getline(fin, line)) {
v.push_back(line);
}
}
void search(vector<string>& v, string word) {
for (int i = 0; i < v.size(); i++) {
int index = v[i].find(word);
if (index != -1)
cout << v[i] << endl;
}
}
int main() {
vector<string> wordVector;
ifstream fin("text.txt");
if (!fin) {
cout << "text.txt can't opened" << endl;
return 0;
}
fileRead(wordVector, fin);
fin.close();
cout << "reading text.txt." << endl;
while (true) {
cout << "Input a word: >>";
string word;
getline(cin, word);
if (word == "exit")
break;
search(wordVector, word);
}
cout << "Terminated" << endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我写了此功能
find_all_alternate
找到给定单词的所有可能替代方案。我从此处列出了单词列表
编译和运行
输出
I wrote this function
find_all_alternate
to find all possible alternate of a given word.I've taken the list of words from here
Compilation and running
Output
您的程序显示了此结果,因为
std :: String
查找功能只会查找基因的确切匹配。您想要的是计算2个字符串的距离,然后显示1个距离为1的距离。
对于计算距离,通常使用众所周知的 levensthein 算法。
我在答案中显示了一个实现 and 在这里。
这样,您的解决方案可以修改为以为
,如果要将其转换为更现代的C ++,也可以使用:
如果有限制只有相等长度的单词,则可以编写一个简单的比较函数你自己。
看起来像:
Your program shows this result, because the
std::string
find function will only look for an exact match of a substring.What you want is to calculate the distance of 2 strings and then show those with a distance of 1.
For calculating the distance, generally the well known Levensthein algorithm is used.
I showed an implementation already in my answers here and here.
With that, your solution can be modified to
And if you want to convert this to more modern C++, you can also use:
And if there is the restriction that only words of equal length shall be shown, then you can write a simple comparison function by yourself.
This would look like: