我是在构建正确的组织角色的方式,以便我可以计数它们
Am I building a correct way of organizing characters so I can count them?
该代码应该能够计算总数,然后每次出现在文本文件中时计数。我尝试构建一个制造一个数组的结构,该结构是一个整数和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';
}
相关文章:
- 为什么我可以将变量存储在不是其最小对齐方式的倍数的地址?
- 我可以使用哪种数据结构来处理这种方式
- 我可以以相同的方式使用 char* c 和字符串吗?
- 而不是那么多的 if 语句,我想要一个逻辑,我可以用一个语句或优化的方式来完成
- 复制文件的最佳方式是什么,以便我可以在复制过程中轻松取消复制?
- 如果 std::numeric_limits<float>::is_iec559 为真,这是否意味着我可以以明确定义的方式提取指数和尾数?
- 我想重置一个C++结构统计,我可以以某种方式使用 stat() 语法吗?
- 我可以以某种方式使我的矢量/数组动态更改装置数量吗?
- 我可以在中断中写入向量,然后以安全的方式仅在主线程内读取吗?
- 我是在构建正确的组织角色的方式,以便我可以计数它们
- 我可以使用什么方法或方式将用户输入的字符串转换为 C++ 中的整数
- 如何将 boost::beast 中的序列化数据转换为字符串,以便我可以以 FIFO 方式处理它?
- C++多重继承 - 相同的方法名称 - 我可以以某种方式删除其中一个吗?
- 拥有 std::map 的最佳方式,我可以在其中定义如果没有键时返回的内容
- 我可以以某种方式从GCC过滤错误消息行
- 我可以以某种方式将 HLSL 放入 .lib 中吗?
- 我可以以某种方式将 Maple 过程包含在C++代码中吗?
- 使用 Linux Eclipse,我可以以编程方式判断我正在调试器 (gdb) 中执行
- 我可以以这种方式返回迭代器吗?
- 我可以在不使用新关键字的情况下以某种方式实现这一点吗?C++