计算文本文件中的字母

时间:2009-12-13 13:36:54

标签: c++

可以告诉我这个代码有什么不对吗?它编译和一切都很好,但输出是固定的零一直下降。所以它不算数字。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "c:/test.txt";

int main () 
{
    string lineBuffer;
    ifstream inMyStream (FileName); //open my file stream


    if (inMyStream.is_open()) 
    {
       //create an array to hold the letter counts
       int upperCaseCount[26] = {0};
           int lowerCaseCount[26] = {0};

       //read the text file
       while (!inMyStream.eof() )
       {
           //get a line of text
           getline (inMyStream, lineBuffer);
           //read through each letter in the lineBuffer
           char oneLetter;
           for( int n=0; n < (int)lineBuffer.length(); ++n )
           {
             oneLetter = char( lineBuffer[n] ); //get a letter
                if (oneLetter >= 'A' && oneLetter <='Z') 
                { //decide if it is a capital letter
                     upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                         if (oneLetter >= 'a' && oneLetter <='z') 
                         { //decide if it is a lower letter
                               lowerCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                         }//end 
                }//end
           }
        }//end of while

        inMyStream.close(); //close the file stream

        //display the counts
        for (int i= 0; i < 26; i++)
            cout << char(i + 65) << "\t\t" << lowerCaseCount[i] << char(i + 95) << "\t\t" << lowerCaseCount[i] << endl;
}//end of if
        else cout << "File Error: Open Failed";

       return 0;
}

6 个答案:

答案 0 :(得分:7)

你已经得到了一些你知道的问题的帮助,现在可能还有一些你可能没有意识到的问题(还有):

   while (!inMyStream.eof() )
   {
       //get a line of text
       getline (inMyStream, lineBuffer);

你应该立刻学到的一点是,正如你所写的,这将无法正常工作。你通常想做的是:

while (getline(inMyStream, lineBuffer)) {
    // .. the rest of the processing.

但是,由于您一次只处理一个字符,而忽略除字母之外的所有字符,因此一次只能读取一个字符可能更简单:

int ch;
while (inMyStream >> ch)
// process the character

由于没有其他人提及它们,我还要指出,不是明确地测试'a'和'z'来查找小写字母,而'A'和'Z'来查找大写字母,你会发现最好使用islowerisupper,它们在<ctype.h>(以及其他几个地方)中提供:

#include <ctype.h>

while (inMyStream >> ch)
    if (isupper((unsigned char)ch))
        ++upperCount[(unsigned char)ch-'A'];
    else if (islower((unsigned char)ch))
        ++lowerCount[(unsigned char)ch-'a'];

答案 1 :(得分:6)

编辑:确实这里描述的问题不是唯一的问题,请参阅其他答案以获得更完整的解决方案。

upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                     if (oneLetter >= 'a' && oneLetter <='z') 
                                     { //decide if it is a lower letter
                           lowerCaseCount[int(oneLetter)- 65]++;

(至少)这两个65中的一个是错误的。我建议改为int('A')int('a')而不是......

注意:这可能不是解释您问题的原因。

答案 2 :(得分:4)

这里你的if语句范围错误。每个字母可以 大写或小写,但是if语句作用域的方式,如果字母已经是大写的,那么你只检查小写,这当然是荒谬的。< / p>

你想要更像的东西:

for(unsigned n = 0; n < lineBuffer.length(); ++n)
{
   oneLetter = char( lineBuffer[n] ); // get a letter
   if (oneLetter >= 'A' && oneLetter <='Z') {
     upperCaseCount[int(oneLetter)- 'A']++;
   }
   else if (oneLetter >= 'a' && oneLetter <='z') { 
     lowerCaseCount[int(oneLetter)- 'a']++;
   }
}

答案 3 :(得分:2)

您的if大写和小写字母拼写错误。如果oneLetter不是大写,你甚至不会看小写字母。这两个if应该处于同一级别。

这是我能看到的唯一错误。

我建议调试,如gf建议的那样,或者抛出一些打印语句来验证你对发生了什么(或不发生)的假设。

答案 4 :(得分:1)

此代码可能存在其他问题,但有一点突出的是,计算小写字母的if语句位于if语句中,用于计算大写语句。您的测试文件可能不包含任何大写字母,因此输出为实心零。

应该有两个单独的if语句,例如:

if (oneLetter >= 'A' && oneLetter <='Z') 
{ //decide if it is a capital letter
  upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array   
}//end

if (oneLetter >= 'a' && oneLetter <='z') 
{ //decide if it is a lower letter
  lowerCaseCount[int(oneLetter)- 65]++; //make the index match the count array
}//end 

答案 5 :(得分:0)

最后打印输出怎么样,小写字母计数打印两次?这解释了为什么它“一直向下”,因为原始代码 正确地计算大写字母不是吗?

相关问题