首先我们要清楚在进程对象中需要拥有哪些信息:①进程标识符;②进程进入时间;③进程结束时间;
其次,在进程执行时,我们又需要哪些中间值来辅助进程进行:①服务剩余时间;②时间线;③优先级;
最后,在进程执行结束后,我们需要得到哪些信息:①进程开始时间;②进程结束时间;③进程周转时间;④进程带权周转时间;
在上述中我们可以发现创建进程类时,其中拥有一个很重要的对象,就是时间,因此我们也需要实现一个时间类,来为我们创建进程类做服务。
时间类
class Time
{
public:
Time(int hour = 0, int minute = 0)
:_hour(hour),
_minute(minute)
{}
~Time(){}
//输入重载
friend istream& operator>>(istream& _cin, Time& time);
//输出重载
friend ostream& operator<<(ostream& _cout, const Time time);
//计算差值,返回分钟
int operator-(const Time& time)
{
Time max = *this;
Time min = time;
int flag = 1;
if (max < min)
{
Time tmp = min;
min = max;
max = tmp;
flag = -1;
}
int hour = max._hour - min._hour;
int minute = max._minute - min._minute;
if (minute < 0)
{
--hour;
minute += 60;
}
return (minute + hour * 60)*flag;
}
Time& operator+(const int n)
{
int min = n + this->_minute;
this->_hour += min / 60;
this->_minute = min % 60;
return *this;
}
Time& operator+=(const int n)
{
*this = *this + n;
return *this;
}
//赋值运算符重载
Time& operator=(Time& time)
{
this->_hour = time._hour;
this->_minute = time._minute;
return *this;
}
//逻辑运算重载
bool operator>(const Time& time)
{
if (this->_hour > time._hour)
return true;
else
{
if (this->_hour == time._hour && this->_minute > time._minute)
return true;
else return false;
}
}
bool operator==(const Time& time)
{
return this->_hour == time._hour &&
this->_minute == time._minute;
}
bool operator!=(const Time& time)
{
return !(*this == time);
}
bool operator>=(const Time& time)
{
return *this > time || *this == time;
}
bool operator<(const Time& time)
{
return !(*this >= time);
}
bool operator<=(const Time& time)
{
return *this < time || *this == time;
}
private:
int _hour;
int _minute;
};
istream& operator>>(istream& _cin, Time& time)
{
cout << "时间:";
_cin >> time._hour;
_cin >> time._minute;
return _cin;
}
ostream& operator<<(ostream& _cout, const Time time)
{
cout << setiosflags(ios::right);
_cout << setw(2) << time._hour << ':' << setw(2) << time._minute;
return _cout;
}
进程类
//进程类
class Process
{
public:
Process()
{}
Process(Time entertime, size_t servicetime, Time begintime, Time finishtime,
size_t rtime = 0, double powertime = 0, int name = 0)
:_EnterTime(entertime),
_ServiceTime(servicetime),
_BeginTime(begintime),
_FinishTime(finishtime),
_rTime(rtime)
{
_PowerTime = powertime;
_rTime = rtime;
_name = name;
}
~Process()
{}
//输入重载
friend istream& operator>>(istream& _cin, Process& process);
//输出重载
friend ostream& operator<<(ostream& _cout, const Process process);
//引用重载
Process& operator&()
{
return *this;
}
//赋值重载
Process& operator=(Process& a)
{
this->_BeginTime = a._BeginTime;
this->_EnterTime = a._EnterTime;
this->_FinishTime = a._FinishTime;
this->_name = a._name;
this->_PowerTime = a._PowerTime;
this->_rTime = a._rTime;
this->_ServiceTime = a._ServiceTime;
this->_RemainSeviceTime = a._RemainSeviceTime;
this->_First = a.__First;
return *this;
}
static Time _LineTime; //时间线
int _name; //进程编号
Time _EnterTime; // 进入时间
size_t _ServiceTime; //服务时间
Time _BeginTime; //开始时间
Time _FinishTime; //完成时间
size_t _rTime; //周转时间
double _PowerTime; //带权周转时间
double _First; //优先级
int _RemainSeviceTime; //剩余时间
};
进程信息的输入
//输入
void InPut(vector<Process>& process)
{
cout << "添加几条进程:";
int n;
cin >> n;
process.resize(n);
for (auto& e : process)
{
cin >> e;
}
}
进程信息的输出
//输出
void OutPut(const vector<Process>& process)
{
cout << "进程编号 " << "进入时间 " << "开始时间 "
<< "服务时间 " << "完成时间 " << "周转时间 " << "带权周转时间" << endl;
for (auto e : process)
{
cout << e << endl;
}
}
版权声明:本文为weixin_44030298原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。