C ++ Letter Occurences

时间:2018-04-13 19:45:36

标签: c++

  

编写一个C ++程序,该程序从文本文件中读取输入并计算从输入中读取的字符数。如果读取的字符是字母('a' - 'z'),则计算输入中该字母出现的次数[使用数组](大写和小写都应计为相同的字母)。输出输入文本中每个字母的百分比,以及输入中非字母字符的百分比。

是的,这是一个家庭作业问题,而且我已经掌握了大部分内容,但由于某些原因,它并没有像我希望的那样增加。

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

using namespace std;

int main()
{
    // make array with size of 26
    // make array with all letter of alphabet
    const int size = 26;
    int narray[size];
    char larray[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };

    // all variables
    int count = 0;
    char character;
    int length = 0;
    int amount = 0;
    int sum = 0;
    double percent = 0;


    // open file user inputs
    ifstream input;
    string file;
    cout << "Please enter the file to be read" << endl;
    cin >> file;
    input.open(file);

    if (input.fail())
    {
        cout << "Can't open file successfully." << endl;
            return 1;
    }

    // count amount of characters and spaces in while loop
    while (!input.eof())  //loop until the file ends
    {
        getline(input, file);   // read every character
        int c = file.length();  // count length
        length += c;
    }

    // make every variable in array equal to 0
    for (count = 0; count < size; count++)
    {
        narray[count] = amount;
    }


    // make for loop to read every character
    for (int i = 0; i < length; i++)
    {
        input.get(character);  // read characters

        if (character <= 'A' && character >= 'z')
        {
            narray[tolower(character)-'a']++; // find out which variable of the array it is and add 1 to the amount
            sum++;
        }
    }


    // make for loop to print out array percentages
    for (int j = 0; j < size; j++)
    {
        percent = (narray[j] / length) * 100;
        cout << larray[j] << " " << percent << "%" << endl;
    }

    int non = (((length - sum) / length) * 100);
    cout << "Non letter characters " << non << "%" << endl;

    input.close();

    return 0;
}

1 个答案:

答案 0 :(得分:1)

你的代码比它需要的要复杂得多,但更糟糕的是它有几个错误。

  • 您正在使用2个单独的循环来完成1循环可以执行的工作。

  • 在执行读取操作之前,您正在调用input.eof()。在尝试读取之前,流的状态不会更新,因此在第一次读取之前调用eof()是未定义的行为。

  • 在您通过流读取一次EOF以计算其字符后,您不会将流回到开头,这样您就可以再次阅读这些字符。

  • 您没有计算第一个循环中的换行符,但是您正在读取第二个循环中的换行符,因此第二个循环(可能)不会读取第一个循环计数的字符数

  • 您没有正确测试大写和小写字母,并且您没有考虑到在ASCII中,大写字母集和小写字母集之间有6个非字母字符。在计算字符时,您对narray[]数组的索引是完全错误的。

  • 您没有考虑文件可能完全为空,或者只包含换行符并且没有非换行符。如果出现上述任何一种情况,您的length变量将为0,并且在除以0时计算百分比时会出现错误。

话虽如此,请尝试更像这样的事情:

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

using namespace std;

int main()
{
    // make array with size of 26
    const int size = 26;
    int narray[size] = {}; // make every variable in array equal to 0

    // all variables
    char character;
    int count = 0;
    int sum_letters = 0;
    int sum_non = 0;
    double percent;
    string file, line;

    // prompt user for filename
    cout << "Please enter the file to be read" << endl;
    getline(cin, file);

    // open file
    ifstream input(file);
    if (!input.is_open())
    {
        cout << "Can't open file." << endl;
        return 1;
    }

    //loop until the file ends
    while (getline(input, line))
    {
        count += line.size(); // count every character

        for (int j = 0; j < line.size(); ++j)
        {
            character = line[j];

            // find out which variable of the array it is and add 1 to the amount
            if (character >= 'A' && character <= 'Z')
            {
                narray[character-'A']++;
                ++sum_letters;
            }
            else if (character >= 'a' && character <= 'z')
            {
                narray[character-'a']++;
                ++sum_letters;
            }
            else
                ++sum_non;
        }
    }

    input.close();

    if (count != 0)
    {
        // make for loop to print out array percentages
        for (int j = 0; j < size; ++j)
        {
            percent = (double(narray[j]) / count) * 100.0;
            cout << ('a'+j) << " " << percent << "%" << endl;
        }

        percent = (double(sum_non) / count) * 100.0;
        cout << "Non letter characters " << percent << "%" << endl;
    }
    else
    {
        cout << "File has no characters" << endl;
    }

    return 0;
}

如果要在非字母字符的百分比中包含换行符,请改用:

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

using namespace std;

int main()
{
    // make array with size of 26
    const int size = 26;
    int narray[size] = {}; // make every variable in array equal to 0

    // all variables
    char character;
    int count = 0;
    int sum_letters = 0;
    int sum_non = 0;
    double percent;
    string file, line;

    // prompt user for filename
    cout << "Please enter the file to be read" << endl;
    getline(cin, file);

    // open file
    ifstream input(file);
    if (!input.is_open())
    {
        cout << "Can't open file." << endl;
        return 1;
    }

    //loop until the file ends
    while (input.get(character))
    {
        ++count; // count every character

        // find out which variable of the array it is and add 1 to the amount
        if (character >= 'A' && character <= 'Z')
        {
            narray[character-'A']++;
            ++sum_letters;
        }
        else if (character >= 'a' && character <= 'z')
        {
            narray[character-'a']++;
            ++sum_letters;
        }
        else
            ++sum_non;
    }

    input.close();

    if (count != 0)
    {
        // make for loop to print out array percentages
        for (int j = 0; j < size; ++j)
        {
            percent = (double(narray[j]) / count) * 100.0;
            cout << ('a'+j) << " " << percent << "%" << endl;
        }

        percent = (double(sum_non) / count) * 100.0;
        cout << "Non letter characters " << percent << "%" << endl;
    }
    else
    {
        cout << "File is empty" << endl;
    }

    return 0;
}