c++ - 找不到我的程序无法正常执行的解决方案
问题描述
编程新手,我几乎尝试了所有方法来让我的程序运行,但无法弄清楚。这是我的 Into to Programming 课程,所以我将发布它的说明以及上下文目的。
我知道我的程序运行正常,直到第 62 行的右括号。我遇到的主要问题是在那之后,我调用函数sharedLetters。对于上下文,我尝试了多种方法来尝试修复函数定义,所以在这一点上它有点混乱,但一开始是错误的,所以就是这样。至于代码的其余部分,我不完全确定它的编写是否正确,因为我无法超越被调用的函数。
作业说明
对于这项作业,您将编写一个程序,该程序构成了填字游戏生成器的基础。为了创建一个填字游戏,您需要识别一个或多个单词共有的字母。该程序通过识别几个单词共有的字母并将它们存储在中间数据结构中来帮助您生成填字游戏。
使用中间数据结构是简化编码的一种方法。您创建一个数据结构来组织您的输入,以便更容易创建最终输出。您的 CrosswordGenerator 程序将打开一个包含单词列表的文件。要为填字游戏生成做准备,您的代码应实现以下伪代码:
- 打开一个名为 wordlist.txt 的文件
- 确定文件中的字数 (N)。
- 创建一个名为 wordArray[] 的大小的数组;
- 将文件中的所有单词读入一个名为 wordArray[N] 的数组中。
- 按长度降序对单词数组进行排序
- 从数组的开头(最长的单词)开始检查单词
- 查找数组中可以与当前单词相交的所有其他单词 如果两个单词共享一个字母,则它们可以相交。
编写一个名为
String sharedLetters(String S1, String S2)
.实现
sharedLetters(S1,S2)
将返回一个字符串,其中包含两个参数字符串 S1 和 S2 共享的字母。如果 S1 和 S2 之间没有共享字母,sharedLetters()
则应返回空字符串""
。sharedLetters(S1,S2)
应该忽略大小写;大写和小写字母被认为是相等的。创建一个二维字符串数组,
wordsIntersect[N][N]
其中每个维度 N 是 的大小wordArray
。迭代
wordArray
比较不同的单词。不要将一个词与它本身进行比较。如果与into的结果wordArray[i]
共享任何字母。应忽略非字母字符。wordArray[j]
sharedLetters(wordArray[i],wordArray[j])
wordsIntersect[i][j]
大写和小写字母应该是等价的。您的程序不应将它们视为不同的标记。
共享信件的顺序不是这个程序的考虑因素。
将数组的内容写入
wordsIntersect
逗号分隔值 (CSV) 文本文件,该文件以行优先格式命名wordsIntersect.csv
:row,col,value。在下面的示例中,有一个条目wordsIntersect[12][33]
包含字符串“tci”。还会有一行wordsIntersect.csv
包含12,33,tci
- 字母周围没有引号,并且该行在字母“i”之后结束。鉴于wordsIntersect[12][78]
will contains""
在文件中将有一行包含:12,78,
在最后一个逗号之后有一个行尾。注意
sharedLetters() 输出的示例。
SharedLetters(transaction,dictum)
返回"tci"
SharedLetters(Transaction,kudzu)
返回""
if
"transaction"
is stored inwordArray[12]
and"dictum"
is stored inwordArray[33]
thenwordsIntersect[12][33]
将包含"tci"
.如果
"kudzu"
存储在wordArray[78]
thenwordsIntersect[12][78]
将包含""
.
我的实际代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::string;
//calling function that will record shared letters between words, to be called later
string sharedLetters(string S1[], string S2[]);
int main ()
{
//variables for indexes used in arrays
int i, compareIndex, count = 0;
//declaring array and its total size based on total words in file
const int N = 644;
string wordArray[N];
//declaring a 2-D array wordsIntersect, setting size of both dimensions to 'N'
string wordsIntersect[644][644];
//declaring file
ifstream file;
//opening file
file.open("wordlist.txt");
cout << "Reading data from the file." << endl;
while (count < N)
{
file >> wordArray[count];
count++;
}
//closing the file
cout << "Closing the wordlist.txt file now." << endl;
file.close();
//sort the array of strings in descending order using SelectionSort
//going through array of strings, wordArray[]
for (int i = 0; i < N; i++)
{
//creating a nested for loop to compare indexes
for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
{
//compare the words at both indexes to determine which one is longer
if (wordArray[i].length() < wordArray[compareIndex].length())
{
//if true, swap the elements using selectionSort
string longerWord = wordArray[compareIndex];
//swap indexes i and compareIndex
wordArray[compareIndex] = wordArray[i];
wordArray[i] = longerWord;
}
}
}
//calling sharedLetters function
for(i = 0; i < N; i++)
{
for(compareIndex = 0; compareIndex < N; compareIndex++)
{
string sharedLetters(wordArray[i], wordArray[compareIndex]);
}
}
string word = string sharedLetters(wordArray[i], wordArray[compareIndex]);
cout << word << endl;
//compare every word in wordArray to every other word within the array, using the sharedLetters funtion
for (int i = 0; i < N; i++)
{
for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
{
//saving a string result within the main function that will equal
//the return value of the called function in the previous step
wordsIntersect[i][compareIndex] = sharedLetters(wordArray[i], wordArray[compareIndex]);
}
}
for (int i = 0; i < N; i++)
{
for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
{
cout << wordArray[i] << "," << wordArray[compareIndex] << " " << wordsIntersect[i][compareIndex] << endl;
}
}
return 0;
}
//setting up 2D array wordsIntersect
for (int i = 0; i < N; i++)
{
for (int compareIndex = 0; compareIndex < N; compareIndex++)
{
wordsIntersect[i][compareIndex];
}
}
//writing the data in 2-D array into file wordsIntersect.csv
cout << "Now opening the file wordsIntersect.csv." << endl;
ofstream outputFile;
outputFile.open("wordsIntersect.csv");
cout << "Inputting sorted array wordsIntersect within file, now." << endl;
outputFile << wordsIntersect[i][compareIndex];
//closing file
outputFile.close();
cout << "Data has successfully been entered into the file. The file is now closed." << endl;
return 0;
}
//function that will identify common letters between each word
string sharedLetters(string S1[], string S2[])
{
//declaring string for sharedLetters
string sharedletters = "";
//declaring a string for words with common letters
string commonLetter = "";
//nested for loop to compare letters in S1 and S2
for (int i = 0; i < S1.length(); i++)
{
for (int compareIndex = 0; compareIndex < S2.length(); compareIndex++)
{
//comparing letters in strings
if(S1[i] == S2[compareIndex])
{
//assigning the letter at index i whenever common is found
commonLetter = S1[i];
//adding the value of commonLetter to sharedletters
sharedletters = commonLetter;
break;
}
}
}
//return string of sharedletters
return sharedletters;
}
解决方案
推荐阅读
- python - 试图从用户输入中获取最接近已知键的值 | numpy.core_exception
- javascript - 在 Vue 中复制对象并更改嵌套属性
- r - 如何在堆栈溢出中共享数据帧
- sql - Oracle SQL - 总和下一个 X 行数
- pine-script - 计算出的 Heikin Ashi 值与界面不同
- docker - 与现有 docker 容器交互运行
- css - 使用 Chrome 将网页打印为 PDF 时处理可变字体
- javascript - 如何使用 JQuery 检测网站是否接受 IFrame 嵌入?
- mongodb-query - 在 mongodb 中,集合有没有办法加密几个关键列并解密剩余的列?
- smartgwt - SmartGWT 中的 HTMLPane 是否有任何替代方案,其中 setContentsType 为 PAGE,内部使用 iframe?