计数信的出现在一个文件中 [英] Counting occurrences of letter in a file
问题描述
我想指望每个字母出现在文件的次数。当我运行code下面的计算Z两次。任何人都可以解释为什么?
该测试数据是:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
的#include<&iostream的GT; //必需的,如果你的程序没有任何I / O
#包括LT&;&了iomanip GT; //所需的输出格式
#包括LT&;&的fstream GT; //所需的文件I / O
#包括LT&;串GT; //必需的,如果你的程序使用C ++字符串
#包括LT&;&CMATH GT; //所需的复杂的数学函数
#包括LT&;&cctype GT; //所需的字母大小写转换使用命名空间std; //所需的ANSI C ++ 1998标准。诠释的main()
{
串回复;
串查找inputfilename;
ifstream的INPUTFILE;
CHAR字符;
INT letterCount [127] = {};COUT<< 输入文件名:
函数getline(CIN,查找inputfilename);//打开输入文件。
inputFile.open(inputFileName.c_str()); //需要.c_str()为C ++转换成字符串C风格字符串
//检查成功打开该文件。
如果(!inputFile.is_open())
{
COUT<< 无法打开输入文件。 << ENDL;
COUT<< preSS回车键继续......;
函数getline(CIN,回复)
出口(1);
}而(inputFile.peek()!= EOF)
{
INPUTFILE>>字符;
// TOUPPER(字符); letterCount [的static_cast< INT>(字符)] ++;
}对(INT迭代= 0;迭代&下; = 127;迭代++)
{
如果(letterCount [迭代]&0)
{
COUT<<的static_cast<焦炭>(迭代)<< << letterCount [迭代]<< ENDL;
}
}系统(暂停);
出口(0);
}
正如其他人所指出的那样,你有两个QS输入。有两个ZS的原因是,在最后的
INPUTFILE>>字符;
(可能是当有只是一个换行符留在流,因此不会EOF)没有任何转换,使从previous迭代全局的'字符'a'Z'。尝试检查inputFile.fail()事后看到这一点:
而(inputFile.peek()!= EOF)
{
INPUTFILE>>字符; 如果(!inputFile.fail())
{
letterCount [的static_cast< INT>(字符)] ++;
}
}
惯用的方式来写循环,这也解决了你的'Z'的问题,就是:
而(INPUTFILE>>字符)
{
letterCount [的static_cast< INT>(字符)] ++;
}
I'm trying to count the number of times each letter appears in a file. When I run the code below it counts "Z" twice. Can anyone explain why?
The test data is:
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
#include <iostream> //Required if your program does any I/O
#include <iomanip> //Required for output formatting
#include <fstream> //Required for file I/O
#include <string> //Required if your program uses C++ strings
#include <cmath> //Required for complex math functions
#include <cctype> //Required for letter case conversion
using namespace std; //Required for ANSI C++ 1998 standard.
int main ()
{
string reply;
string inputFileName;
ifstream inputFile;
char character;
int letterCount[127] = {};
cout << "Input file name: ";
getline(cin, inputFileName);
// Open the input file.
inputFile.open(inputFileName.c_str()); // Need .c_str() to convert a C++ string to a C-style string
// Check the file opened successfully.
if ( ! inputFile.is_open())
{
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
exit(1);
}
while ( inputFile.peek() != EOF )
{
inputFile >> character;
//toupper(character);
letterCount[static_cast<int>(character)]++;
}
for (int iteration = 0; iteration <= 127; iteration++)
{
if ( letterCount[iteration] > 0 )
{
cout << static_cast<char>(iteration) << " " << letterCount[iteration] << endl;
}
}
system("pause");
exit(0);
}
As others have pointed out, you have two Qs in the input. The reason you have two Zs is that the last
inputFile >> character;
(probably when there's just a newline character left in the stream, hence not EOF) fails to convert anything, leaving a 'Z' in the global 'character' from the previous iteration. Try inspecting inputFile.fail() afterwards to see this:
while (inputFile.peek() != EOF)
{
inputFile >> character;
if (!inputFile.fail())
{
letterCount[static_cast<int>(character)]++;
}
}
The idiomatic way to write the loop, and which also fixes your 'Z' problem, is:
while (inputFile >> character)
{
letterCount[static_cast<int>(character)]++;
}
这篇关于计数信的出现在一个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!