C++ 将值传递给 fStream

C++ Pass Values To fStream

本文关键字:fStream 值传 C++      更新时间:2023-10-16

我正在尝试将值从计算函数传递到文件函数。但是"到达"和"突发"值没有从"计算"函数中正确读取。它仅返回通过计算输入的最后一个值。

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;
};

这会让你大吃一惊,但你的代码是正确的。

arrivalburst 是类变量,从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_burstsall_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);

而且,现在您的代码应该可以正确访问您之前输入的值。

PS:最好在C++中阅读更多有关类和类变量的信息。