c ++文件阅读器程序

时间:2018-02-13 17:54:59

标签: c++

感谢任何帮助。我正在编写一个程序来计算给定纯文本文件中存在的不同类型的字符。它假设计算排除所有空格,计算所有字母,从那些将计算元音的字母,计算所有数字,并计算其他类型的字符。文本文件显示:“作业5很有趣!”完全包含所有间距和准确。

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

using namespace std;

int main()
{
    ifstream inputFile;
    ofstream outputFile;
    string userInput;
    int countLength, countSpace, totalVowels, totalNumbers, totalOther;
    double alphaPercent, vowelPercent, numPercent, otherPercent;
    int totalLetters[26] = {0};
    char getChar;

    totalVowels = 0;
    totalNumbers = 0;
    totalOther = 0;

    cout << "Enter the file name: " << endl;
    getline(cin, userInput);

    if (userInput == "as.5.txt")
    {
        inputFile.open("as.5.txt");

        if (inputFile.is_open())
        {
            while (getline(inputFile,userInput))
            {
                countLength+= userInput.length();
                if (userInput == " ")
                {
                    countSpace ++;
                    countLength = countLength - countSpace;

                    for (unsigned n = 0; n < userInput.length(); ++n)
                    {
                        getChar = char (userInput[n]); //reads the letters in the file

                        if (getChar == 'a'|| getChar == 'e'|| getChar == 'i' || getChar == 'o'|| getChar == 'u' || getChar == 'A' || getChar == 'E' || getChar == 'I' || getChar == 'O' || getChar == 'U')
                        {
                            totalVowels++;
                        }
                        else if (getChar == 1 || getChar == 2 || getChar == 3 || getChar == 4 || getChar == 5 || getChar == 6 || getChar == 7 || getChar == 8 || getChar == 9)
                        {
                            totalNumbers++;
                        }
                        else
                        {
                            totalOther++;
                        }
                    }
                }
                alphaPercent = ((countLength - totalNumbers - totalOther) * 100)/countLength;
                vowelPercent = (totalVowels * 100)/countLength;
                numPercent = (totalNumbers * 100)/countLength;
                otherPercent = (totalOther * 100)/countLength;

                cout << "% of alphabets = " << alphaPercent << "%" << endl;
                cout << "% of vowels = " << vowelPercent << "%" << endl;
                cout << "% of numbers = " << numPercent << "%" << endl;
                cout << "% of the rest = " << otherPercent << "%" << endl;
                cout << "Total number of characters = " << countLength << endl;

                inputFile.close();
                return 0;
            }
        }
    }
    else
    {
        cout << "Type in an available file." << endl;
    }

    return 0;
}

没有语法错误,但输出如下:

Enter the file name:
as.5.txt
% of alphabets = 100%
% of vowels = 0%
% of numbers = 0%
% of the rest = 0%
Total number of characters = 28

什么时候看起来像这样:

Enter the file name:
as.5.txt
% of alphabets = 92%
% of vowels = 32%
% of numbers = 4%
% of the rest = 4%
Total number of characters = 25

2 个答案:

答案 0 :(得分:2)

代码看起来像

if (userInput == " ")
{
    // ...
    for (unsigned n = 0; n < userInput.length(); ++n)
    {
        // count the various types
    }
}

只有当所有输入行等于" "时,才会运行计数循环。 因此,不会计算元音,数字......,

答案 1 :(得分:2)

有一种更简单的方法来计算给定std::string中的元音(你有#include <string>,所以我认为在你的作业中使用它是可以接受的)。基本上沿着std::isdigitstd::isalpha的行,您可以编写自己的辅助函数来检查标准元音的输入字符。

#include <algorithm>   ///< std::count_if
#include <cctype>      ///< std::tolower, std::isdigit, std::isalpha
#include <set>         ///< std::set

bool isvowel(unsigned char ch)
{
    static std::set<unsigned char> const vowels{ 'a','e','i','o','u' };
    // normalize to lowercase before checking for match
    return vowels.find(std::tolower(ch)) != vowels.end();
}

然后,您可以通过在给定字符串上使用辅助函数的STL算法std::count_if来“计算”给定字符串中的所有元音。

std::string text; ///< the string to check for vowels
int vowels = std::count_if(text.begin(), text.end(), [](auto ch){ return isvowel(ch); });

提示#1:您可以使用上面的代码行,只需将其最后一部分更改为return std::isdigit(ch);并获取“免费”数字计数。

提示#2:您可以使用上面的代码行,只需将其最后一部分更改为return std::isalpha(ch);,即可“免费”获得字母字符数。

相关问题