我目前正在尝试用 C++ 编写一个单词计数程序,但在解析字符串并将单词彼此分开时遇到了困难。除此之外,每次重复单词时,我都很难让唯一单词的单词计数增加。我的 findWord() 和 DistinctWords() 函数很可能是我所知道的问题。也许您会在其他功能中看到我看不到的东西,至于上述功能,我不知道它们中有什么问题。这些是我的导师提供的指导:
创建一个程序,该程序将计算和报告文本文件中不同的、不区分大小写的单词的出现次数。
该程序应该有一个循环:
1.提示用户输入文件名。如果用户仅按 Enter 键,则终止循环和程序。
2.验证具有输入名称的文件存在。如果文件不存在,则显示相应的消息并返回到步骤 1。
3.读取并显示文件的内容。
4.显示文件中不同单词的计数。
5.显示文件中每个不同单词的排序列表以及每个单词的出现次数。按字数降序排列列表,按单词升序排列。
我现在很困,我的作业要在午夜到期。帮助当然会不胜感激。感谢您的时间。这是我的代码,我还将在其后复制粘贴示例测试文本文件:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream> // Needed to use files
#include <vector>
#include <algorithm> // Needed for sort from standard libraries
using namespace std;
struct WordCount{
string word; // Word
int count; // Occurence #
void iCount(){ count++; }
WordCount(string s){ word = s; count = 1;}
};
// Function prototypes
string InputText(); // Get user file name and …Run Code Online (Sandbox Code Playgroud)