我是在构建正确的组织角色的方式,以便我可以计数它们

Am I building a correct way of organizing characters so I can count them?

本文关键字:我可以 方式 角色 构建      更新时间:2023-10-16

该代码应该能够计算总数,然后每次出现在文本文件中时计数。我尝试构建一个制造一个数组的结构,该结构是一个整数和char阵列,以便我可以将计数与数组的位置相同。但是现在我被困了。我在网上看了很多,但找不到我需要帮助的东西。有人得到了一些建议吗?另外,在代码中,如果您看到任何应该更改的内容,我感谢提示!我在C 上很新,所以请轻松对我。

结构,多个阵列,搜索Internet的答案

#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'的次数,应能够转换为一种情况,但是我可以在计数一个或另一个情况下这样做。我的测试仪文件都在所有小写字母中,所以这将是一个很好的起点。

更大的提示,使用 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. :(nn";
        return EXIT_FAILURE;
    }
    std::ifstream is{ file_name };
    if (!is.is_open()) {
        std::cerr << "Couldn't open "" << file_name << "" for reading. :(nn";
        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 << "nn";
}  // 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. :(nn";
    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. :(nn";
    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) << "nn";
    for (auto const &count : counts)
        std::cout << "'" << count.first << "': " << count.second << 'n';
}
相关文章: