问题描述
这是一个非常具有挑战性的问题,因为我不确定如何正确地问它。
我编写了一个程序,每次加载时都会读取相同的文本数据。文本数据是字典中的单词列表,并且该程序解决了字谜类型的谜题(例如Jumble,Boggle,Scrabble等)。此文本数据永不变。目前,我用于读取文本文件,该文本文件必须与所生成的.exe位于同一文件夹中。这假定用户不会只是进入并擦除,编辑或破坏文本文件或确实在用户可能执行的范围内的某些事情。不仅如此,锁定文件并读取文件的操作非常缓慢。
程序的大部分工作是将.txt文件转换为抽象数据类型(ADT),该数据将单词分类为“签名”,然后构建一组具有相同签名的单词。这些集存储在一个结构(这里称为map)中,该结构是key:value类型数据结构,其中key是签名。无论如何,该信息与该问题无关,仅表示在加载时我需要在内存中构建ADT。我正在寻找一种比文本文件更复杂的方法。
由于我是编程新手,所以可能会有更好的方法。我只是不知道怎么问这个问题,因为我不知道那里有什么。
我了解数据库,但是同样,这似乎依赖于一个外部文件。我看过一些文章,它们谈论将数据存储在.h文件中,但他们始终希望构建一个字符数组(char [i]),这需要将这些数据转换为我的ADT,并且再次浪费时间。程序正在加载。 (为什么还要将其转换为char数组以将其读回ADT?)
/*
* Project: myJumble
* Created by CS106 C++ Assignment Wizard 0.1
*
* Name: Brad Beall
* Section: Life
* This code will solve the jumble puzzles in the newspaper.
*/
#include <fstream>
#include <iostream>
#include "simpio.h"
#include "map.h"
#include "set.h"
#include "genlib.h"
//This function swaps two characters.
void Swap(char &ch1,char &ch2)
{
char tmp = ch1;
ch1 = ch2;
ch2 = tmp;
}
//This function sorts all the chars in a word in alphabetical order
string SortWord(string inWord)
{
inWord = ConvertToLowerCase(inWord);
//these two for loops will sort the string alphabetically
// - idea is starting from the front,find the 'smallest' character in the string.
// (where a is 'smaller' than b)
// then move that smallest character to the front of the string
// Now move to the next character and again look for the smallest character.
// Example: for "peach",first move the 'a' to the front to form "apech",then move 'c' to form "acpeh"...
for (int i = 0; i < inWord.length(); i++) {
int minIndex = i;
for (int j = i+1; j < inWord.length(); j++)
{
if (inWord[j] < inWord[minIndex])
{
// looking for the 'smallest' character
minIndex = j;
}
}
Swap(inWord[i],inWord[minIndex]);
}
return inWord;
}
void BuildDictionary(Map<Set<string> > &kDict,ifstream &in)
{
string nextWord = "";
while(true)
{
//read in the next word from the dictionary
in >> nextWord;
if (in.fail()) break;
//sort letters alphabetically using SortWord,use that as the key
// and then add that key:value pair to the set.
kDict[SortWord(nextWord)].add(nextWord);
}
}
//this function prints a set
void PrintSet(Set<string> &inputSet)
{
Set<string>::Iterator it = inputSet.iterator();
while (it.hasNext())
{
cout << it.next() << endl;
}
}
int main ()
{
////debug the function: string SortWord(string inWord)
//cout << "Enter a word to sort" << endl;
//string tempString = GetLine();
//tempString = SortWord(tempString);
//cout << tempString;
//building the dictionary may take some time.
cout << "Loading the dictionary. This may take some time." << endl;
//read in the text file with all dictionary words
ifstream in;
in.open("enable1.txt");
//call the member function that will create our data structure
//this will be a MAP:
// - key: the alphabetized letters from a word,or the word's "signature"
// - value: a Vector of words with the matching signature
Map<Set<string> > keyedDictionary;
BuildDictionary(keyedDictionary,in);
while(true)
{
//prompt user for a word to solve
cout << "Enter a jumbled word to solve." << endl;
cout << "Type '0' to exit." << endl << endl;
string solveWord = GetLine();
if(solveWord == "0"){
break;
}
//sort the word into a signature key
solveWord = SortWord(solveWord);
//call the PrintSet(Set) member function to print the set of solutions for this signature key
PrintSet(keyedDictionary[solveWord]);
}
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)