我们被要求打开一个包含句子的文本文件,并查看文件中的所有字母和空格,并计算每个 ascii 字符的数量。
当我在主函数中拥有所有信息时,它工作正常。我只是不知道如何在主要内容中成功调用它们。输出应如下所示:
15 words
6 a
3 d
6 e
3 g
3 h
3 i
15 l
6 o
6 r
3 s
3 t
3 w
当一切都在主函数中时,我实现了这个输出,所以现在只需要通过我的函数定义来获得它。
我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//prototypes
//int openFile();
void readFile(ifstream &in);
void closeFile(ifstream &in);
//definitions
int openFile()
{
ifstream in;
in.open("word_data.txt");
}
void readFile(ifstream &in)
{
//store the frequency of the letters
int letters[128];
//declare variables
char let;
int wordCount = 0;
//for loop to initialize all counts to zero
for (int i = 0; i < 128; i++)
{
letters[i] = 0;
}
//get letters until we reach end of file
//whitespace = wordCount++;
let = in.get();
while (let != EOF)
{
if (let == ' ')
wordCount++;
//change to lowercase
let = tolower(let);
letters[let]++;
let = in.get();
}
//output
//num words
cout << wordCount + 1 << " words" << endl;
//count how many of each letter there are & print to screen in alphabetical order
for (char let = 'a'; let <= 'z'; let++)
{
if (letters[let] != 0)
{
cout << letters[let] << " "<< let <<endl;
}
}
}
void closeFile(ifstream &in)
{
in.close();
}
int main()
{
openFile();
readFile(in);
closeFile(in);
return 0;
}
问题在于您的openFile()
功能。它只创建一个本地 ifstream
,它不打开ifstream
其他功能可以访问的。
试试这个:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
//prototypes
void openFile(ifstream &in);
void readFile(ifstream &in);
void closeFile(ifstream &in);
//definitions
void openFile(ifstream &in)
{
in.open("word_data.txt");
}
void readFile(ifstream &in)
{
//store the frequency of the letters
int letters[128] = {};
//declare variables
char ch;
int wordCount = 0;
//get letters until we reach end of file
while (in.get(ch))
{
if (ch == ' ')
wordCount++;
//change to lowercase
ch = static_cast<char>(tolower(static_cast<unsigned char>(ch)));
letters[ch]++;
}
//output
//num words
cout << wordCount + 1 << " words" << endl;
//count how many of each letter there are & print to screen in alphabetical order
for (ch = 'a'; ch <= 'z'; ch++)
{
if (letters[ch] != 0)
{
cout << letters[ch] << " " << ch <<endl;
}
}
}
void closeFile(ifstream &in)
{
in.close();
}
int main()
{
ifstream in;
openFile(in);
readFile(in);
closeFile(in);
return 0;
}
话虽如此,您可能会考虑使用 astd::map
来跟踪您的频率,而不是使用int[]
数组。并使用一次operator>>
阅读整个单词:
#include <map>
...
void readFile(ifstream &in)
{
//store the frequency of the letters
map<char, int> letters;
//declare variables
string word;
int wordCount = 0;
//get letters until we reach end of file
while (in >> word)
{
++wordCount;
//for(size_t idx = 0; idx < word.size(); ++idx)
//{
// char ch = word[idx];
for(char ch : word)
{
//change to lowercase
ch = static_cast<char>(tolower(static_cast<unsigned char>(ch)));
if (ch >= 'a' && ch <= 'z')
letters[ch]++;
}
}
//output
//num words
cout << wordCount << " words" << endl;
//count how many of each letter there are & print to screen in alphabetical order
//for (map<char, int>::iterator iter = letters.begin(); iter != letters.end(); ++iter)
//{
// cout << iter->second << " " << iter->first << endl;
//}
for (auto &elem : letters)
{
cout << elem.second << " " << elem.first << endl;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句