我正在尝试将值从calculate函数传递给文件函数。但是,“计算”功能无法正确读取“到达”和“爆发”值。它只返回通过calculate输入的最后一个值。
float RR::calculate()
{
cout << "Enter the number of processes: ";
cin >> num_pr;
vector<RR*> all_processes;
for (int i=0; i<num_pr; i++)
{
cout << "What is the arrival time for process " << i << ": ";
cin >> arrival_in;
cout << "What is the burst time for process " << i << ": ";
cin >> burst_in;
}
...
file (num_pr, arrival_in, burst_in, quantum, avg);
}
void RR::file(int processes, float arrival, float burst, float quantum, float avg)
{
fstream newFile;
newFile.open ("results.txt",ios::in | ios::out | ios::app);
for (int i=0; i<processes; i++)
{
newFile << "Arrival time for process " << i << ": " << arrival << endl;
newFile << "Burst time for process " << i << ": " << burst << endl;
}
}
这是我的班级定义:
class RR
{
public:
RR();
RR(float burst_set, float arrival_set);
int num_pr, pos;
float quantum, avg, burst_sum, time, burst_time, sleep_time, arrival_sum, total_avg, burst_in, arrival_in, calculate(), get_arrival(), get_burst(), get_avg(), get_initial_burst();
void set_avg(float avg_set);
void set_burst(float burst_time_set);
void write_file(int processes, float arrival, float burst, float quantum, float avg);
private:
float initial_burst, arrival_time, avg_time;
};
答案 0 :(得分:1)
这会让您感到惊讶,但您的代码是正确的。
arrival
和burst
是类变量,calculated
只会更新:它们永远不会存储,如下所示:
float RR::calculate()
{
cout << "Enter the number of processes: ";
cin >> num_pr;
vector<RR*> all_processes;
//your loop is correct, but it is only updating the values in `arrival_in` and `burst_in`
//once this function finishes executing, those variables will be set at the
//last value that was assigned to them.
for (int i=0; i<num_pr; i++)
{
cout << "What is the arrival time for process " << i << ": ";
cin >> arrival_in;
cout << "What is the burst time for process " << i << ": ";
cin >> burst_in;
}
}
因此,当你在这里使用它们时:
file (num_pr, arrival_in, burst_in, quantum, avg);
由于这些变量仍然包含上次运行时的值,因此结果并不令人惊讶。
这意味着您的实现仅访问这些变量在其中的最后一个值而不是所有变量(根据需要)。存储它们的一种方法是使用类vectors
来存储所有值。然后,稍后在file()
中,您可以查阅这些向量并写出正确的值。
示例(请注意,这只是一个示例 - 因为您对C ++的理解存在其他问题,所以不应该使用它):
float RR::calculate()
{
cout << "Enter the number of processes: ";
cin >> num_pr;
vector<RR*> all_processes;
//your loop is correct, but it is only updating the values in `arrival_in` and `burst_in`
//once this function finishes executing, those variables will be set at the
//last value that was assigned to them.
for (int i=0; i<num_pr; i++)
{
cout << "What is the arrival time for process " << i << ": ";
cin >> arrival_in;
all_arrivals.push_back(arrival_in);
cout << "What is the burst time for process " << i << ": ";
cin >> burst_in;
all_bursts.push_back(burst_in);
}
}
此处定义了all_bursts
和all_arrivals
:
class RR
{
public:
RR();
RR(float burst_set, float arrival_set);
int num_pr, pos;
float quantum, avg, burst_sum, time, burst_time, sleep_time, arrival_sum, total_avg, burst_in, arrival_in, calculate(), get_arrival(), get_burst(), get_avg(), get_initial_burst();
void set_avg(float avg_set);
void set_burst(float burst_time_set);
void write_file(int processes, float arrival, float burst, float quantum, float avg);
private:
float initial_burst, arrival_time, avg_time;
std::vector<float> all_arrivals;//this should be private
std::vector<float> all_bursts;//this should be private
};
现在,如果您这样做,您可以使用这些值(不将它们作为函数参数传递,因为默认情况下类函数可以在任何地方访问这些变量):
void RR::file(int processes, float quantum, float avg)
{
fstream newFile;
newFile.open ("results.txt",ios::in | ios::out | ios::app);
//since the number of `all_arrivals` and `all_bursts` are the same: i.e. `processes`, you can use `processes` here:
//alternatively, you can use `all_arrivals.size()` or `all_bursts.size()` in lieu of `processes`
for (int i=0; i<processes; i++)
{
newFile << "Arrival time for process " << i << ": " << all_arrivals[i] << endl;
newFile << "Burst time for process " << i << ": " << all_bursts[i] << endl;
}
}
现在,您可以像这样调用函数:
file (num_pr, quantum, avg);
现在,您的代码应该正确访问您之前输入的值。
P.S。:在C ++中阅读更多关于类和类变量的信息是个好主意。