我正在编写一个入门级脚本来模拟疾病传播。我正在尝试编写它,以便可以运行多个试验并每次输出结果。该脚本使用一个Person类和一个People类,后者由一个人员向量组成。每个试验返回相同的结果(当单独测试时,它们从相同的输入返回不同的结果)。我认为每次试验都需要擦拭人群对象,但是我不确定如何。
我认为我需要一个析构函数,但不确定语法。在线资源对于我的技能水平来说太高级了,或者当我尝试复制其语法时给了我错误消息。
class Person {
private: // Person constructor
int status; bool infected;
public:
Person(){
status = 0; infected = false;
};
string status_string(){ // Outputs status of each person with a symbol as a string
};
void update_per(){ // Updates status of each person if they are sic
};
void infect(int n){ // Infects person if they are susceptible (not recovered or vaccinated)
};
void vaccinate(){ // Changes status of person to being vaccinated
};
bool is_stable(){ // Determines if person has recovered from infection
};
int get_status() { // Returns status of person
};
};
class Population { //Create population class
private: //declare private variable npeople
int npeople;
vector<Person> population; //create a vector of Persons named population with size npeople
public:
Population(int n){
srand(time(NULL));
npeople = n;
population = vector<Person>(n);
};
~Population() //DESTRUCTOR
{
delete[] population;
};
void random_infection(int days){ //method to randomly infect one person
};
int count_infected() { //method to count the number of people infected
};
void update_pop(int ncontacts, float contagion, int days) { // Updates the status of each person in population, also simulates spread of disease through contact
};
void print_status(){ // Output status of each person in population
};
void vacc_pop(float prob){ // Vaccinates a set number of people in the population
};
};
int main() {
ofstream popsizeresults;
int size; // Asks user for size of population
int numtrials; // Asks user for number of trials
for (int jjjj=1; jjjj<=numtrials; jjjj++){
int maxsick = 0;
int day = 0;
Population population(size); // Create population
population.vacc_pop(0.5); // Vaccinate population
population.random_infection(5); // Infect one random person in population
int step = 1;
for ( ; ; step++){
// Output status of each person in population
cout<<"In step "<<step<< " #sick: "<<population.count_infected()<<" : ";
population.print_status();
cout<<endl;
// If no people are sick, then the disease has run its course
if(population.count_infected() == 0)
break;
// Update the status of each person and simulate spread of disease
population.update_pop(size*0.25,0.9,5);
if (population.count_infected() > maxsick){
maxsick = population.count_infected();
day = step;
}
}
popsizeresults.open("disease10.txt", ios::app);
popsizeresults << jjjj << "," << step << "," << maxsick << "," << day << "\n";
popsizeresults.close();
//population.~Population(); //call destructor
return 0;
}
}
在我添加析构函数(不起作用)之前,疾病10.txt的输出对每个试验产生不同的结果,但对每个试验产生相同的结果。一次测试一个试验(对于相同的输入)时,它将产生不同的结果(这是目标)。我不确定析构函数是否真的是答案,我对C ++很陌生。无论哪种方式,我都不确定如何为每个试验复制不同的结果。
答案 0 :(得分:-1)
现在我明白了你的追求,这是人口重置的一个非常精简的演示。
请参阅注释中带编号的注释,以改进样式等。
#include <vector>
#include <iostream>
// a very basic Person for exposition
class Person
{
public:
Person() { };
};
// Population cut down to the bare minimum
class Population
{
private:
// note 1: no need to store npeople. a vector has a size(). Why store the same thing twice
// note 2: never use `using namespace std;` at global scope. Spell out std:: explicitly
std::vector<Person> population;
public:
// note 3: number of people can never be negative, so why give ourselves the choice? make it unsiged
Population(unsigned n)
// note 4: use list initialisation in constructors
: population(n)
{
};
// note 5: no need for destructors. google "rule of none", "rule of 5", "rule of 3". Prefer rule of none
// answer:
// a specific function to reset the population
void reset(unsigned n)
{
// destroy old people
population.clear();
// make new people
population.resize(n);
}
// note 6: allows us to print a representation of a population for exposition
friend std::ostream& operator<<(std::ostream& os, Population const& pop)
{
for (Person const& p : pop.population)
{
os << '.';
}
os << "(" << pop.population.size() << " people)";
return os;
}
};
int main()
{
Population population = Population(10);
std::cout << population << '\n';
// note 6: reset the population
population.reset(5);
std::cout << population << '\n';
}
预期输出:
..........(10 people)
.....(5 people)