#include "iostream"
#include <Windows.h>
#include <tchar.h>
using namespace std;

class Process {
public:
	string ProcessName; //I / 进程名字
	int Time; // 进程需要时间
	int leval; // 进程优先级
	int LeftTime;// 进程运行一段时间后还需要的时间
};

void Sort(Process pr[], int size);
void sort1(Process pr[], int size); //此排序后按优先级从大到小排列
//此排序后按需要的CPU时间从小到大排列
void FIFO(Process pr[], int num, int Timepice);//先来先服务算法
void TimeTurn(Process process[], int num, int Timepice); // 时间片轮转算法
void Priority(Process process[], int num, int Timepice);

void FIFO(Process process[], int num, int Timepice)
{// process[] 是输入的进程，num 是进程的数目，Timepice 是时间片大小
	while (true) {
		if (num == 0) {
			cout << " 所有进程都已经执行完毕!" << endl;
			exit(1);
		}
		if (process[0].LeftTime == 0) {
			cout << "进程" << process[0].ProcessName << "已经执行完毕!" << endl;
			for (int i = 0;i < num;i++)
				process[i] = process[i + 1];
			num--;
		}
		else if (process[num - 1].LeftTime == 0) {
			cout << "进程" << process[num - 1].ProcessName << "已经执行完毕!" << endl;
			num--;
		}
		else {
			cout << endl;//输出正在运行的进程…     
		}
		cout << endl;
		system(" pause");
		cout << endl;
	} // while
}


void TimeTurn(Process process[], int num, int Timepice) {
	while (true) {

		if (num == 0) {
			cout << "所有进程都已经执行完毕！" << endl;
			exit(1);
		}
		if (process[0].LeftTime == 0) {
			cout << "进程" << process[0].ProcessName << "已经执行完毕" << endl;
			for (int i = 0;i < num;i++)
				process[i] = process[i + 1];
			num--;
		}
		if (process[num - 1].LeftTime == 0) {
			cout << "进程" << process[num - 1].ProcessName << "已经执行完毕!" << endl;
			num--;
		}
		else if (process[0].LeftTime > 0) {
			cout << endl;
		}
		Process temp;
		temp = process[0];
		for (int j = 0;j < num;j++)
			process[j] = process[j + 1];
		process[num - 1] = temp;
	}// else
	cout << endl;
	system(" pause");
	cout << endl;
}

void Sort(Process pr[], int size) {

	for (int i = 1;i < size;i++) {
		Process temp;
		temp = pr[i];
		int j = i;
		while (j > 0 && temp.leval < pr[j - 1].leval) {
			pr[j] = pr[j - 1];
			j--;
		}
		pr[j] = temp;
	} for (int d = size - 1;d > size / 2;d--) {
		Process temp;
		temp = pr[d];
		pr[d] = pr[size - d - 1];
		pr[size - d - 1] = temp;
	}
}

void Priority(Process process[], int num, int Timepice) {
	while (true) {
		if (num == 0) {
			cout << "所有进程都已经执行完毕！" << endl;
			exit(1);
		}
		if (process[0].LeftTime == 0)
		{
			cout << "进程" << process[0].ProcessName << "已经执行完毕!" << endl;
			for (int m = 0;m < num;m++)
				process[m] = process[m + 1];//一个进程执行完毕后从数组中删除
			num--;//此时进程数目减少一个
		}
		if (num != 1 && process[num - 1].LeftTime == 0) {
			cout << "进程" << process[num - 1].ProcessName << "已经执行完毕!" << endl;
			num--;
		}

		if (process[0].LeftTime > 0) {

			cout << endl;//输出正在运行的进程..
		}
		Sort(process, num);
		cout << endl;
		system(" pause");
		cout << endl;
	}
}

void sort1(Process pr[], int size) {

	for (int i = 1;i < size;i++) {
		Process temp;
		temp = pr[i];
		int j = i;
		while (j > 0 && temp.Time < pr[j - 1].Time) {

			pr[j] = pr[i - 1];
			j--;
		}
		pr[j] = temp;
	}
}

int _tmain(int argc, _TCHAR* argv[]) {
	int a;
	cout << "选择调度算法：" << endl;
	cout << "1:FIFO2:时间片轮换3:优先级调度4:最短时间优先" << endl;
	cin >> a;
	const int Size = 30;
	Process  process[Size];int num;
	int TimePice;
	cout << "输入进程个数:" << endl;
	cin >> num;
	cout << "输入此进程时间片大小：" << endl;
	cin >> TimePice;
	for (int i = 0; i < num; i++) {
		string name;
		int CpuTime;
		int Leval;
		cout << "输入第" << i + 1 << "个进程的名字、CPU 时间和优先级 :" << endl;
		cin >> name;
		cin >> CpuTime >> Leval;
		process[i].ProcessName = name;
		process[i].Time = CpuTime;
		process[i].leval = Leval;
		cout << endl;
	}
	for (int k = 0;k < num;k++)
		process[k].LeftTime = process[k].Time;
	cout << "（说明：在本程序所列进程信息中，优先级一项是指进程运行后的优先级！）";
	cout << endl;  cout << endl;
	cout << "进程名字" << "占用 CPU时间" << "还需要占用时间“<<”优先级" << "状态" << endl;
	if (a == 1) FIFO(process, num, TimePice);
	else if (a == 2) TimeTurn(process, num, TimePice);
	else if (a == 3) {
		Sort(process, num);
		Priority(process, num, TimePice);
	}
	else {
		sort1(process, num);
		FIFO(process, num, TimePice);
	}
	return 0;
}