我需要帮助查单词和数词
I need help doing a word search and count
好的,所以我正在做一项家庭作业,我假设阅读一个包含100个选定单词的文本文件,然后阅读另一个"文本"文件,并比较这100个选定的单词中有多少出现在"文本"文件上,最长和最短的单词是什么,以及这些选定的单词在文本文件中的时间。我需要完全的帮助,这就是我目前所得到的。顺便说一句,我还没有输入选定的文件,所以这就是为什么那些ifs语句是不完整的
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int fileNumber;
string Words [100];
string wordCounts [100];
string Frequencies [100];
string shortest = "abcdefghijklmnopqrstuvwxyz";
string longest = "";
int totalWordCount = 0;
ifstream inData;
int I=0;
inData.open("SelectWords.txt");
while ( !inData.eof () ) {
inData >> Words [I];
}
int irow;
while ( irow=0, irow<=100, irow++) {
cout<<Words[I]<<endl;
}
cout<<"Choose a file you want to open"<<endl;
cout<<"1= A Modest Proposal"<<endl;
cout<<"2=Apology" <<endl;
cout<<"3=The Call of the Wild"<<endl;
cout<<"4=The Adventures of Tom Sawyer"<<endl;
cin>> fileNumber;
while ((fileNumber<1) || (fileNumber>4))
{
cout<<"Error please try again"<<endl;
cout<<"Choose a file you want to open"<<endl;
cout<<"1 - A Modest Proposal"<<endl;
cout<<"2 - Apology" <<endl;
cout<<"3 - The Call of the Wild"<<endl;
cout<<"4 - The Adventures of Tom Sawyer"<<endl;
cin>> fileNumber;
}
if (fileNumber==1)
{
}
if (fileNumber==2)
{
}
if (fileNumber==3)
{
}
if (fileNumber==4)
{
}
system ("pause");
return 0;
}
我会这样做:
std::string filename;
switch (filenumber)
{
case 1:
filename = "modest_proposal.txt";
break;
case 2:
filename = "apology.txt";
break;
//...
}
std::ifstream target_file(filename.c_str());
if (!target_file)
{
std::cerr << "Error opening file " << filename << endl;
return EXIT_FAILURE;
}
std::string word;
while (target_file >> word)
{
// Search word list
}
请注意,我如何仅使用switch
来确定文件名。无论文件名如何,算法的其余部分都是相同的。
编辑1:建议
- 使用
std::map<word, count>
而不是两个单独的数组 - 为菜单创建一个文本变量,并将该变量打印两次而不是复制代码
相关文章:
- 文本文件中的单词链表
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何根据单词在文本中出现的概率输出单词
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 需要帮助调试我的代码,以逆转字符串中的单词
- 需要帮助理解一个循环的单词
- C++帮助:使用数组和循环在文本中查找单词
- 电话单词生成器帮助C++
- 需要帮助C++计算器输入单词
- 我需要帮助查单词和数词
- 将每个单词的第一个字符大写-帮助查找我的错误