C++ C++;从文件中计算字符数组中的特定计数器
我试图计算文件中特定字符的数量。我遇到的问题是,输出是一个巨大的数字,与文件中每个字母的数量不匹配 RainOrShine.txt RRCSSSCSCRCSSSSCCRCRCSS SSSCCSSSCCSSSCCSSCRCCSSSSS sssscscscscscscscssssscsC++ C++;从文件中计算字符数组中的特定计数器,c++,arrays,C++,Arrays,我试图计算文件中特定字符的数量。我遇到的问题是,输出是一个巨大的数字,与文件中每个字母的数量不匹配 RainOrShine.txt RRCSSSCSCRCSSSSCCRCRCSS SSSCCSSSCCSSSCCSSCRCCSSSSS sssscscscscscscscssssscs #include <iostream> #include <string> #include <fstream> #include <cstring> using na
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
string filelocation = "C:/Users/erizj/OneDrive/Documents/RainOrShine.txt";
ifstream textfile;
textfile.open(filelocation.c_str());
char weather[3][30];
int countR,countC,countS = 0;
if (!textfile)
{
cout << "Error opening the file.";
return 0;
}
else
{ // Read weather data in from file
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 30; col++)
{
textfile >> weather[row][col];
if(weather[row][col]=='R'){
countR++;
}
if(weather[row][col]=='C'){
countC++;
}
if(weather[row][col]=='S'){
countS++;
}
}
}
}
cout<<"Rainy Days during 3-Month Period: "<<countR<<endl;
cout<<"Cloudy Days during 3-Month Period: "<<countC<<endl;
cout<<"Sunny Days during 3-Month Period: "<<countS<<endl;
//cout<<"Rainy Days in June: "<<
textfile.close();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
string filelocation=“C:/Users/erizj/OneDrive/Documents/RainOrShine.txt”;
ifstream文本文件;
open(filelocation.c_str());
煤焦天气[3][30];
int countR,countC,countS=0;
如果(!textfile)
{
天气[行][列];
如果(天气[行][列]='R'){
countR++;
}
如果(天气[行][列]='C'){
c++;
}
如果(天气[行][col]='S'){
计数++;
}
}
}
}
库特
初始化计数
,但不初始化countR
和countC
初始化计数
,但保持计数r
和计数c
未初始化。您的程序假定数据中有固定数量的行和列。这可能是个问题
我建议采用更灵活的方法,不对数据的组织方式或数据量进行假设
让我们为数据库定义一个结构。数据可以按顺序存储,但我们需要动态的数据结构:std::vector
现在,在进行任何分析之前读取数据:
std::vector<char> database;
std::string text_line; // Easier to read in a lot of data
while (getline(data_file, text_line))
{
std::string::size_type index;
const std::string::size_type length = text_line.length();
for (index = 0; index < length; ++index)
{
const char c = text_line[index];
if ((c == 'R') || (c == 'S') || (c == 'C'))
{
database.push_back(c);
}
}
}
std::向量数据库;
std::字符串文本_行;//更容易读取大量数据
while(getline(数据文件、文本行))
{
std::string::size\u类型索引;
常量std::string::size_type length=text_line.length();
对于(索引=0;索引<长度;++索引)
{
const char c=文本行[索引];
如果((c='R')| |(c='S')| |(c='c'))
{
数据库。推回(c);
}
}
}
由于数据被读入数据库,因此您可以对其进行分析:
unsigned int duration = 0;
unsigned int rain_quantity = 0;
unsigned int sunny_quantity = 0;
unsigned int cloudy_quantity = 0;
// Set up the duration for the first 30 days
duration = 30;
if (database.size() < 30)
{
duration = database.size();
}
for (unsigned int index = 0; index < duration; ++index)
{
const char c = database[index];
if (c == 'C')
{
++cloudy_quantity;
}
else
{
if (c == 'R')
{
++rain_quantity;
}
else
{
++sunny_quantity;
}
}
}
unsigned int duration=0;
无符号整数数量=0;
无符号整数数量=0;
无符号整数和整数数量=0;
//设置前30天的持续时间
持续时间=30;
if(database.size()<30)
{
duration=database.size();
}
for(无符号整数索引=0;索引
您可以执行其他分析,而无需从文件中读取数据 您的程序假设数据中有固定数量的行和列。这可能是个问题
我建议采用更灵活的方法,不对数据的组织方式或数据量进行假设
让我们为数据库定义一个结构。数据可以按顺序存储,但我们需要动态的数据结构:std::vector
现在,在进行任何分析之前读取数据:
std::vector<char> database;
std::string text_line; // Easier to read in a lot of data
while (getline(data_file, text_line))
{
std::string::size_type index;
const std::string::size_type length = text_line.length();
for (index = 0; index < length; ++index)
{
const char c = text_line[index];
if ((c == 'R') || (c == 'S') || (c == 'C'))
{
database.push_back(c);
}
}
}
std::向量数据库;
std::字符串文本_行;//更容易读取大量数据
while(getline(数据文件、文本行))
{
std::string::size\u类型索引;
常量std::string::size_type length=text_line.length();
对于(索引=0;索引<长度;++索引)
{
const char c=文本行[索引];
如果((c='R')| |(c='S')| |(c='c'))
{
数据库。推回(c);
}
}
}
由于数据被读入数据库,因此您可以对其进行分析:
unsigned int duration = 0;
unsigned int rain_quantity = 0;
unsigned int sunny_quantity = 0;
unsigned int cloudy_quantity = 0;
// Set up the duration for the first 30 days
duration = 30;
if (database.size() < 30)
{
duration = database.size();
}
for (unsigned int index = 0; index < duration; ++index)
{
const char c = database[index];
if (c == 'C')
{
++cloudy_quantity;
}
else
{
if (c == 'R')
{
++rain_quantity;
}
else
{
++sunny_quantity;
}
}
}
unsigned int duration=0;
无符号整数数量=0;
无符号整数数量=0;
无符号整数和整数数量=0;
//设置前30天的持续时间
持续时间=30;
if(database.size()<30)
{
duration=database.size();
}
for(无符号整数索引=0;索引
您可以执行其他分析,而无需从文件中读取数据 您需要分别初始化所有整数变量。
int countR=0,countS=0,countT=0代码>您需要分别初始化所有整数变量。
int countR=0,countS=0,countT=0代码>除非这是某种学校练习,否则它要短得多,也不容易出错。除此之外:你只需要一个字符,而不是九十个,因为你除了查看你读到的最后一个值之外,从不使用数组做任何事。我相信std::map
是过火了。人们可以将数据存储在std::vector
中,因为几天是连续的。除非这是某种学校练习,否则它会更短,更不容易出错。除此之外,你只需要一个char
,而不是九十个,因为除了查看上次读取的值之外,你从不使用数组做任何事情。我认为std::map
是过分了。可以将数据存储在std::vector
中,因为天数是连续的。除了ASHelpers keen find之外,如果一个月有31天,您当前的代码将有额外的问题。否则,将文本文件加载到缓冲区并使用std::count表示“R”、“C”和“S”可能会更容易。如果您的文件包含3个月后的多个月,请根据CR&LF构建字符串索引(我会亲自拆分字符串,然后在每个字符串上使用std::count)。除了AsHelpers keen find之外,如果一个月有31天,您当前的代码将有其他问题。否则,将文本文件加载到缓冲区并使用std::count表示“R”、“C”和“S”可能会更容易。如果您的文件包含3个月后的多个月,则基于CR&LF构建字符串索引(我将拆分字符串)