2013-11-04 173 views
1

我試圖將計算函數的值傳遞給文件函數。但是「到達」和「突發」值沒有從「計算」功能中正確讀取。它只返回通過計算輸入的最後一個值。C++將值傳遞給fStream

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

你可以發佈你的類定義嗎?從目前的情況來看,這有點難以分辨。 – jrd1

+1

我已將類定義添加到原始文章。 – optimus203

+0

我想你可能需要移動文件(num_pr,arrival_in,burst_in,quantum,avg);在上面的for循環裏面。 –

回答

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

由於這些變量仍包含上次運行時的值,因此您的結果並不令人驚訝。

這是什麼意思是你的實現只訪問這些變量在它們中的最後一個值,而不是所有的(根據需要)。存儲它們的一種方法是使用類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); 

而且,現在您的代碼應預先輸入正確訪問值你。

P.S .:在C++中閱讀更多關於類和類變量的知識將是一個好主意。

+0

非常好的解決方案,額外2載體。像魅力一樣工作。感謝您的時間和幫助! – optimus203

+0

@ optimus203:沒問題。很高興爲您服務。而且,謝謝你! :) – jrd1