C++ 如何从长度为9的文本文件中获取单词?
我想知道你如何通过一个文本文件,得到该文本文件中长度为9的所有单词,然后选择其中任何一个单词并随机排列这些字母C++ 如何从长度为9的文本文件中获取单词?,c++,c++11,C++,C++11,我想知道你如何通过一个文本文件,得到该文本文件中长度为9的所有单词,然后选择其中任何一个单词并随机排列这些字母 book = readWordsFile("misc/word.txt"); std::vector<std::string> text; for(int i = 0; i<book.size();i++){ if(book[i] == 9) { return book[i]; } } 假设您有一个文件words.txt,它是一个很大的单词长文件,用逗号分隔,例如
book = readWordsFile("misc/word.txt");
std::vector<std::string> text;
for(int i = 0; i<book.size();i++){
if(book[i] == 9) {
return book[i];
}
}
假设您有一个文件words.txt,它是一个很大的单词长文件,用逗号分隔,例如一个流行的MOOC课程:
你可以这样做:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> // random_shuffle
#include <cstdlib> // rand()
using namespace std;
int main() {
// init rand function
srand(time(NULL));
string dictionaryPath = "words.txt";
ifstream ifs(dictionaryPath);
vector<string> allNineLetterWords;
char c;
string word;
// read words.txt and save 9 letter words into list
while (ifs.get(c))
{
// seperate words by ,
if (c == ',') {
// store 9 letter words
if (word.size() == 9) {
allNineLetterWords.push_back(word);
}
word = "";
} else {
word += c;
}
}
ifs.close();
// randomize
int randomIndex = rand() % allNineLetterWords.size();
string originalWord = allNineLetterWords.at(randomIndex);
string scrambledWord;
vector<int> indices = {0, 1, 2, 3, 4, 5, 6, 7, 8};
std::random_shuffle(indices.begin(), indices.end());
for ( int i = 0 ; i < 9; i++) {
int randomIndex = indices.at(i);
scrambledWord += originalWord.at(randomIndex);
}
// print original and scrambled word
cout << originalWord << ", " << scrambledWord << endl;
return 0;
}
vector<string> findWords(string dictionaryPath, int length) {
ifstream ifs(dictionaryPath);
vector<string> allLengthNWords;
char c;
string word;
// read words.txt and save 9 letter words into list
while (ifs.get(c))
{
// seperate words by ,
if (c == ',') {
// store 9 letter words
if (word.size() == length) {
allLengthNWords.push_back(word);
}
word = "";
} else {
word += c;
}
}
ifs.close();
return allLengthNWords;
}
bool twoWordsExistInWord(string firstWord, string secondWord, string largeWord) {
// check iff all letters of first and second word are in larger word
// create a dictionary of all letters in largeWord
std::map<char, int> lettersMap;
for (int i = 0; i < largeWord.size(); i++)
{
if (!lettersMap.count(largeWord.at(i))) {
lettersMap.insert({largeWord.at(i), 1});
} else {
lettersMap[largeWord.at(i)]++;
}
}
for (int i = 0; i < firstWord.size(); i++) {
if (!lettersMap.count(firstWord[i]))
return false;
lettersMap[firstWord.at(i)]--;
}
for (int i = 0; i < secondWord.size(); i++) {
if (!lettersMap.count(secondWord[i]))
return false;
lettersMap[secondWord.at(i)]--;
}
for (int i = 0; i < lettersMap.size(); i++) {
if (lettersMap[i])
return false;
}
return true;
}
int main() {
// init rand function
srand(time(NULL));
string dictionaryPath = "words.txt";
vector<string> allNineLetterWords = findWords(dictionaryPath, 9);
vector<string> allFourLetterWords = findWords(dictionaryPath, 4);
vector<string> allFiveLetterWords = findWords(dictionaryPath, 5);
for (int k = 0; k < allNineLetterWords.size(); k++) {
for (int i = 0; i < allFourLetterWords.size(); i++) {
for (int j = 0; j < allFiveLetterWords.size(); j++) {
if (twoWordsExistInWord(allFourLetterWords[i], allFiveLetterWords[j], allNineLetterWords[k])) {
std::cout << allNineLetterWords[k] << ", " << allFourLetterWords[i] << allFiveLetterWords[j] << std::endl;
}
}
}
}
return 0;
}
输出:
促动器
解开拉链,piungzinp
动员,imilsezob
编辑:
如果你的目标是制作一个类似于《倒计时》、《字母和数字》等节目的单词拼图/条件,那么如果你只选择9个字母的单词,比如说4个字母和5个字母的单词的组合,那就更有趣了
大概是这样的:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> // random_shuffle
#include <cstdlib> // rand()
using namespace std;
int main() {
// init rand function
srand(time(NULL));
string dictionaryPath = "words.txt";
ifstream ifs(dictionaryPath);
vector<string> allNineLetterWords;
char c;
string word;
// read words.txt and save 9 letter words into list
while (ifs.get(c))
{
// seperate words by ,
if (c == ',') {
// store 9 letter words
if (word.size() == 9) {
allNineLetterWords.push_back(word);
}
word = "";
} else {
word += c;
}
}
ifs.close();
// randomize
int randomIndex = rand() % allNineLetterWords.size();
string originalWord = allNineLetterWords.at(randomIndex);
string scrambledWord;
vector<int> indices = {0, 1, 2, 3, 4, 5, 6, 7, 8};
std::random_shuffle(indices.begin(), indices.end());
for ( int i = 0 ; i < 9; i++) {
int randomIndex = indices.at(i);
scrambledWord += originalWord.at(randomIndex);
}
// print original and scrambled word
cout << originalWord << ", " << scrambledWord << endl;
return 0;
}
vector<string> findWords(string dictionaryPath, int length) {
ifstream ifs(dictionaryPath);
vector<string> allLengthNWords;
char c;
string word;
// read words.txt and save 9 letter words into list
while (ifs.get(c))
{
// seperate words by ,
if (c == ',') {
// store 9 letter words
if (word.size() == length) {
allLengthNWords.push_back(word);
}
word = "";
} else {
word += c;
}
}
ifs.close();
return allLengthNWords;
}
bool twoWordsExistInWord(string firstWord, string secondWord, string largeWord) {
// check iff all letters of first and second word are in larger word
// create a dictionary of all letters in largeWord
std::map<char, int> lettersMap;
for (int i = 0; i < largeWord.size(); i++)
{
if (!lettersMap.count(largeWord.at(i))) {
lettersMap.insert({largeWord.at(i), 1});
} else {
lettersMap[largeWord.at(i)]++;
}
}
for (int i = 0; i < firstWord.size(); i++) {
if (!lettersMap.count(firstWord[i]))
return false;
lettersMap[firstWord.at(i)]--;
}
for (int i = 0; i < secondWord.size(); i++) {
if (!lettersMap.count(secondWord[i]))
return false;
lettersMap[secondWord.at(i)]--;
}
for (int i = 0; i < lettersMap.size(); i++) {
if (lettersMap[i])
return false;
}
return true;
}
int main() {
// init rand function
srand(time(NULL));
string dictionaryPath = "words.txt";
vector<string> allNineLetterWords = findWords(dictionaryPath, 9);
vector<string> allFourLetterWords = findWords(dictionaryPath, 4);
vector<string> allFiveLetterWords = findWords(dictionaryPath, 5);
for (int k = 0; k < allNineLetterWords.size(); k++) {
for (int i = 0; i < allFourLetterWords.size(); i++) {
for (int j = 0; j < allFiveLetterWords.size(); j++) {
if (twoWordsExistInWord(allFourLetterWords[i], allFiveLetterWords[j], allNineLetterWords[k])) {
std::cout << allNineLetterWords[k] << ", " << allFourLetterWords[i] << allFiveLetterWords[j] << std::endl;
}
}
}
}
return 0;
}
将产生:
入迷了,阿克内特伦德
女人
充气器
驱魔
哈哈,玩得开心 @EduardoRaygoza:-嘿,我添加了另一部分,只是为了好玩,如果你想看看的话!当然可以非常感谢。