1

計算関数からファイル関数に値を渡そうとしています。しかし、「到着」と「バースト」の値が「計算」機能から適切に読み取られていません。計算によって入力された最後の値のみを返します。

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

1 に答える 1

1

これはあなたを驚かせるでしょうが、あなたのコードは正しいです。

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

これらの変数には、前回実行したときの値がまだ含まれているため、結果は驚くべきものではありません。

これが意味することは、実装は、それらの変数が最後に持っていた値にのみアクセスし、すべての変数にアクセスするわけではないということです(必要に応じて)。それらを格納する 1 つの方法は、クラスを使用してすべての値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);

これで、コードは以前に入力した値に正しくアクセスできるはずです。

PS: C++ のクラスとクラス変数についてもう少し読んでおくとよいでしょう。

于 2013-11-04T02:57:10.970 に答える