我是否在建立一种正确的组织字符的方式,以便可以对它们进行计数?

时间:2019-05-14 01:24:13

标签: c++

该代码应该能够对字符总数进行计数,然后在每次出现在文本文件中时对它们进行计数。我尝试构建一个结构,该结构同时使一个数组成为整数和char数组,以便可以将计数与数组放在同一位置。但是现在我被困住了。我在网上看了很多东西,但是找不到我需要帮助的东西。有人得到建议吗?另外,在代码中,如果您看到任何应更改的内容,我也感谢您的提示!我在c ++上比较新,所以请对我轻松一点。

结构,多个数组,在互联网上寻找答案

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

struct letters {
    int count;
    char letter;
};

constexpr int SIZE = 10000;

std::string fileName, word;
int count = 0, charCount = 0;
int Alphabet[26];
letters chars[];

void getFileName(std::string& fileName);
int countWords(int& count, std::string& fileName, std::string word);
int countChar(int& charCount, std::string& fileName, letters chars[]);
void sortChars(letters chars[SIZE], int SIZE);

int main()
{
    getFileName(fileName);
    countWords(count, fileName, word);
    countChar(charCount, fileName, chars);
    sortChars(chars, SIZE);
    return 0;
}

void getFileName(std::string& fileName)
{
    std::cout << "Please enter the name of the file followed by the type (ex: text.txt) : " << std::endl;
    std::getline(std::cin, fileName);
}

int countWords(int& count, std::string& fileName, std::string word)
{
    count = 0;
    std::ifstream infile(fileName);
    while (infile >> word) {
        count++;
    }
    std::cout << count << std::endl;
    return count;
}

int countChar(int& charCount, std::string& fileName, letters chars[])
{
    std::ifstream infile(fileName);
    while (infile >> chars->letter) {
        count++;
    }

    std::cout << charCount;
    return charCount;
}

void sortChars(letters chars[SIZE], int SIZE)
{
    int i = 0;
    std::ifstream infile(fileName);
    while (infile >> chars[i].letter) {
        for (int i = 0; i <= chars->count; i++) {
            if (infile == chars[i].letter) {
                chars[i].count++;
            }
        }
    }
}

void printCount()
{
    std::cout << count << std::endl;
    std::cout << charCount << std::endl;
    std::ifstream infile(fileName);
}

该结构应该计算'A''a'的次数,应该能够转换为一种情况,但是我可以在计算出一种或另一种之后进行此操作。我的测试仪文件全部小写,因此这是一个不错的起点。

1 个答案:

答案 0 :(得分:1)

更大的提示,请使用std::unordered_map来计算字符:

#include <cstdlib>        // EXIT_FAILURE
#include <cctype>         // std::isupper(), std::tolower()
#include <string>         // std::string<>, std::getline()
#include <unordered_map>  // std::unordered_map<>
#include <iostream>       // std::ifstream
#include <fstream>        // std::cout, std::cerr

int main()
{
    std::string file_name;
    if (!std::getline(std::cin, file_name)) {
        std::cerr << "Input error. :(\n\n";
        return EXIT_FAILURE;
    }

    std::ifstream is{ file_name };
    if (!is.is_open()) {
        std::cerr << "Couldn't open \"" << file_name << "\" for reading. :(\n\n";
        return EXIT_FAILURE;
    }

    std::size_t num_words = 0;
    std::unordered_map<char, std::size_t> char_counts;
    for (std::string word; is >> word; ++num_words) {
        for (auto ch : word) {
            if (std::isupper(ch))
                ch = std::tolower(ch);
            ++char_counts[ch];
        }
    }

    for (auto const &count : char_counts)
        std::cout << "'" << count.first << "': " << count.second << '\n';   
    std::cout << "Number of words: " << num_words << "\n\n";
}  // when control reaches the end of main() without encountering a return-statement
   // it has the same effect as return 0;

如果您坚持要将几行代码拆分为多个函数,请执行以下操作:

#include <cstdlib>        // EXIT_FAILURE
#include <cctype>         // std::isupper(), std::tolower()
#include <string>         // std::string<>, std::getline()
#include <unordered_map>  // std::unordered_map<>
#include <iostream>       // std::ifstream
#include <fstream>        // std::cout, std::cerr

std::string get_file_name()
{
    std::cout << "Filename: ";
    std::string file_name;
    if (!std::getline(std::cin, file_name))
        std::cerr << "Input error. :(\n\n";
    return file_name;
}

std::ifstream open_file(std::string file_name)
{
    std::ifstream file{ file_name };
    if (!file.is_open())
        std::cerr << "Couldn't open \"" << file_name << "\" for reading. :(\n\n";
    return file;
}

std::size_t get_file_stats(std::istream &is, std::unordered_map<char, std::size_t> &char_counts)
{
    std::size_t num_words = 0;
    for (std::string word; is >> word; ++num_words) {
        for (auto ch : word) {
            if (std::isupper(ch))
                ch = std::tolower(ch);
            ++char_counts[ch];
        }
    }
    return num_words;
}

int main()
{
    std::string file_name{ get_file_name() };
    if (!file_name.length())
        return EXIT_FAILURE;

    std::ifstream is{ open_file(file_name) };
    if (!is.is_open())
        return EXIT_FAILURE;

    std::unordered_map<char, std::size_t> counts;
    std::cout << "Number of words: " << get_file_stats(is, counts) << "\n\n";

    for (auto const &count : counts)
        std::cout << "'" << count.first << "': " << count.second << '\n';
}