gpt4 book ai didi

c++ - 我是否建立了一种正确的组织字符的方法以便我可以计算它们?

转载 作者:行者123 更新时间:2023-11-30 04:47:44 25 4
gpt4 key购买 nike

该代码应该能够计算字符总数,然后计算它们在文本文件中出现的次数。我尝试构建一个结构,该结构使一个数组同时是一个整数和一个字符数组,这样我就可以在与我的数组相同的位置进行计数。但现在我被困住了。我在网上看了很多,但找不到我需要帮助的东西。有人有什么建议吗?同样在代码中,如果您看到任何应该更改的内容,我将感谢您提供的提示!我是 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);
}

struct 应该计算 '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. :(\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';
}

关于c++ - 我是否建立了一种正确的组织字符的方法以便我可以计算它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56121810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com