类函数不会永久更改成员变量
Class function does not change member variables permanently
我正在尝试实现FCFS先进先出(FIFO),也称为先到先得(FCFS)C++算法。
#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 << "ProcesstBurst TimetArrival TimetWaiting TimetTurnaround Time" << endl;
cout << "-------t----------t------------t------------t---------------" << endl;
for(Process i: v){
i.compute();
cout << i.id << "ttt" << i.bt << "ttt" << i.at << "tttt" << i.wt << "tttt" << 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();
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 将包含C样式数组的对象初始化为成员变量(C++)
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 在循环中按顺序遍历成员变量
- c++类声明时,相同的例程,不同的成员变量类型
- 如何从另一个文件继承私有成员变量和公共函数
- 在C++类中,是否必须初始化作为数组的成员变量
- 如何从子成员函数修改父公共成员变量
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 从私有成员变量的成员方法返回unique_ptr
- 在派生类中使用基类的私有成员变量的最佳方法
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 打印所有继承的类成员变量和方法
- 如何在复杂继承中访问静态成员变量
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 成员变量与函数概念检查