Class 函数不会永久改变成员变量
Class function does not change member variables permanently
我正在尝试在 C++ 中实现 FCFS先进先出 (FIFO),也称为先来先服务 (FCFS) 算法。
#include<bits/stdc++.h>
using namespace std;
class Process{
public:
static int count;
static int cycle_count;
int id;
int at;
int wt;
int tat;
int bt;
Process(){
id = count++;
}
void compute(){
if (cycle_count < at){
cycle_count = at;
}
cycle_count += bt;
tat = cycle_count;
wt = tat - bt;
}
};
float average_wt(int n, vector<Process> v){
float avg = 0;
for (Process i: v){
avg += i.wt;
}
avg /= n;
return avg;
}
float average_tat(int n, vector<Process> v){
float avg = 0;
for (int i = 0; i < n; ++i){
avg += v[i].tat;
}
avg /= n;
return avg;
}
void print(int n, vector<Process> v){
cout << "Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time" << endl;
cout << "-------\t----------\t------------\t------------\t---------------" << endl;
for(Process i: v){
i.compute();
cout << i.id << "\t\t\t" << i.bt << "\t\t\t" << i.at << "\t\t\t\t" << i.wt << "\t\t\t\t" << i.tat << endl;
}
cout << "Average Waiting Time: " << average_wt(n, v) << endl;
cout << "Average Turnaround Time: " << average_tat(n, v) << endl;
cout << endl;
}
bool sort_on_at(Process a, Process b){
return a.at < b.at;
}
int Process::count = 0;
int Process::cycle_count = 0;
int main(int argc, char const *argv[]) {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> process(n);
for(int i = 0; i < n; ++i){
cout << "Process " << i << ":" << endl;
cout << "\tArrival Time: ";
cin >> process[i].at;
cout << "\tBurst Time: ";
cin >> process[i].bt;
}
sort(process.begin(), process.end(), sort_on_at);
print(n, process);
return 0;
}
问题在于此代码正确打印了各个进程的等待和周转时间,但给出的平均等待和周转时间为 0
。
预期输出:
Enter the number of processes: 3
Process 0:
Arrival Time: 0
Burst Time: 24
Process 1:
Arrival Time: 0
Burst Time: 3
Process 2:
Arrival Time: 0
Burst Time: 3
Process Burst Time Arrival Time Waiting Time Turnaround Time
------- ---------- ------------ ------------ ---------------
0 24 0 0 24
1 3 0 24 27
2 3 0 27 30
Average Waiting Time: 17
Average Turnaround Time: 27
实际输出:
Enter the number of processes: 3
Process 0:
Arrival Time: 0
Burst Time: 24
Process 1:
Arrival Time: 0
Burst Time: 3
Process 2:
Arrival Time: 0
Burst Time: 3
Process Burst Time Arrival Time Waiting Time Turnaround Time
------- ---------- ------------ ------------ ---------------
0 24 0 0 24
1 3 0 24 27
2 3 0 27 30
Average Waiting Time: 0
Average Turnaround Time: 0
我确实尝试了一些调试,发现 compute()
函数确实更改了值,(因为它为各个进程打印了正确的值)但是由于某种原因 wt
和 tat
average_tat()
和 average_wt()
.
中所有进程的值为 0
如果我能说得更清楚,请告诉我。
wt
是在 compute
方法中计算出来的,但这对 Process
:
的 copy 进行操作
for(Process i: v){ // a copy is made
i.compute();
您需要使用值引用(在本例中为&i
)处理向量中存储的原始Process
,然后wt
将被保存。
for(Process& i: v){
i.compute();
我正在尝试在 C++ 中实现 FCFS先进先出 (FIFO),也称为先来先服务 (FCFS) 算法。
#include<bits/stdc++.h>
using namespace std;
class Process{
public:
static int count;
static int cycle_count;
int id;
int at;
int wt;
int tat;
int bt;
Process(){
id = count++;
}
void compute(){
if (cycle_count < at){
cycle_count = at;
}
cycle_count += bt;
tat = cycle_count;
wt = tat - bt;
}
};
float average_wt(int n, vector<Process> v){
float avg = 0;
for (Process i: v){
avg += i.wt;
}
avg /= n;
return avg;
}
float average_tat(int n, vector<Process> v){
float avg = 0;
for (int i = 0; i < n; ++i){
avg += v[i].tat;
}
avg /= n;
return avg;
}
void print(int n, vector<Process> v){
cout << "Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time" << endl;
cout << "-------\t----------\t------------\t------------\t---------------" << endl;
for(Process i: v){
i.compute();
cout << i.id << "\t\t\t" << i.bt << "\t\t\t" << i.at << "\t\t\t\t" << i.wt << "\t\t\t\t" << i.tat << endl;
}
cout << "Average Waiting Time: " << average_wt(n, v) << endl;
cout << "Average Turnaround Time: " << average_tat(n, v) << endl;
cout << endl;
}
bool sort_on_at(Process a, Process b){
return a.at < b.at;
}
int Process::count = 0;
int Process::cycle_count = 0;
int main(int argc, char const *argv[]) {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> process(n);
for(int i = 0; i < n; ++i){
cout << "Process " << i << ":" << endl;
cout << "\tArrival Time: ";
cin >> process[i].at;
cout << "\tBurst Time: ";
cin >> process[i].bt;
}
sort(process.begin(), process.end(), sort_on_at);
print(n, process);
return 0;
}
问题在于此代码正确打印了各个进程的等待和周转时间,但给出的平均等待和周转时间为 0
。
预期输出:
Enter the number of processes: 3
Process 0:
Arrival Time: 0
Burst Time: 24
Process 1:
Arrival Time: 0
Burst Time: 3
Process 2:
Arrival Time: 0
Burst Time: 3
Process Burst Time Arrival Time Waiting Time Turnaround Time
------- ---------- ------------ ------------ ---------------
0 24 0 0 24
1 3 0 24 27
2 3 0 27 30
Average Waiting Time: 17
Average Turnaround Time: 27
实际输出:
Enter the number of processes: 3
Process 0:
Arrival Time: 0
Burst Time: 24
Process 1:
Arrival Time: 0
Burst Time: 3
Process 2:
Arrival Time: 0
Burst Time: 3
Process Burst Time Arrival Time Waiting Time Turnaround Time
------- ---------- ------------ ------------ ---------------
0 24 0 0 24
1 3 0 24 27
2 3 0 27 30
Average Waiting Time: 0
Average Turnaround Time: 0
我确实尝试了一些调试,发现 compute()
函数确实更改了值,(因为它为各个进程打印了正确的值)但是由于某种原因 wt
和 tat
average_tat()
和 average_wt()
.
0
如果我能说得更清楚,请告诉我。
wt
是在 compute
方法中计算出来的,但这对 Process
:
for(Process i: v){ // a copy is made
i.compute();
您需要使用值引用(在本例中为&i
)处理向量中存储的原始Process
,然后wt
将被保存。
for(Process& i: v){
i.compute();