如何制作一个对象并使用循环将其放入数组中,所以如果我想添加数据,我所要做的就是制作一个新的字符串



如何制作对象并将其放入数组?我想用参数化构造函数制作一个DailyStats数组。我想使用循环来添加到对象数组中。

#ifndef DAILYSTATS_H
#define DAILYSTATS_H
#include <iostream>
#include <vector>
using namespace std;
class DailyStats
{
public:
DailyStats();
DailyStats(string content);
void parse(const string& line);
double mean();
string getDate();
void setDate(string newDate); 
private:
string date;
double temperatureValues[];
};
#endif
#include <iostream>
#include "DailyStats.hpp"
#include <vector>
#include <cmath>
using namespace std;

DailyStats::DailyStats()
{
date = "";
count = 0;
temperatureValues[0];
}
DailyStats::DailyStats(string content)
{
parse(content);
temperatureValues[24];
count = 0;
}
void DailyStats::parse(const string& line)
{
string random = "";
string random1 = "";
char del = ' ';
int count2 = 0;
for(int i = 0; i <= (int)line.size(); i++)
{
if(count < 10)
{
random1 += line[i];
setDate(random1);
count++;
}
if(line[i] != del && count == 10)
{
random += line[i];
}
else if (line[i] == del && count == 10)
{
temperatureValues[count2] = stod(random);
random = "";
count2++;
}
}
}
double DailyStats::mean()
{
count = 1;
double num = 0;
while(count <= 24)
{
num += temperatureValues[count];
if(count == 24)
{
num = num/24;
}
count++;
}
return ceil(num * 100.0) / 100.0;
}
string DailyStats::getDate()
{
return date;
}
void DailyStats::setDate(string newDate)
{
date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;
int main()
{
string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
DailyStats info[4];
info[1] = { {str0} };
cout << info[1].mean() << endl;

return 0;
}

正如你在我的代码中看到的,我有不同的字符串,我想把它们放在我的对象中,用作参数化构造函数的变量。所以,当我完成这项工作时,我可以调用我的mean()函数,这样它就可以获得不同数据的不同方式。

我在没有数组的情况下尝试了它,一切都很好,但我不知道如何让数组工作。

我看到的主要问题是temperatureValues[]数组在编译时未绑定。这是行不通的。由于您直到运行时才知道数组的大小,因此请使用std::vector(事实上,您的代码中已经有了#include <vector>(,例如:

#ifndef DAILYSTATS_H
#define DAILYSTATS_H
#include <vector>
#include <string>
class DailyStats
{
public:
DailyStats() = default;
DailyStats(const std::string& content);
void parse(const std::string& line);
double mean() const;
std::string getDate() const;
void setDate(const std::string& newDate); 
private:
std::string date;
std::vector<double> temperatureValues;
};
#endif
#include "DailyStats.hpp"
#include <cmath>
#include <sstream>

DailyStats::DailyStats(const string& content)
{
parse(content);
}
void DailyStats::parse(const string& line)
{
std::istringstream iss(line);
double value;
iss >> date;
temperatureValues.clear();
while (iss >> value)
temperatureValues.push_back(value);
}
double DailyStats::mean() const
{
double num = 0.0;
if (!temperatureValues.empty())
{
for(size_t count = 0; count < temperatureValues.size(); ++count)
{
num += temperatureValues[count];
}
num = std::ceil((num / temperatureValues.size()) * 100.0) / 100.0;
}
return num;
}
std::string DailyStats::getDate() const
{
return date;
}
void DailyStats::setDate(const std::string& newDate)
{
date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;
int main()
{
string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
DailyStats info[4];
info[0].parse(str0);
info[1].parse(str1);
info[2].parse(str2);
info[3].parse(str3);
cout << info[0].mean() << endl;
cout << info[1].mean() << endl;
cout << info[2].mean() << endl;
cout << info[3].mean() << endl;
return 0;
}

最新更新