#include<iostream> #include<string> using namespace std; struct pcb { string name; int run_time; int level; int state = 0; struct pcb * next; }k1,k2,k3,k4,k5; int main() { cout << "please input the level , run_time, name of process 1" << endl; cin >> k1.level >> k1.run_time>>k1.name; cout << "please input the level , run_time, name of process 2" << endl; cin >> k2.level >> k2.run_time>>k2.name; cout << "please input the level , run_time ,name of process 3" << endl; cin >> k3.level >> k3.run_time>>k3.name; cout << "please input the level , run_time ,name of process 4" << endl; cin >> k4.level >> k4.run_time>>k4.name; cout << "please input the level , run_time ,name of process 5" << endl; cin >> k5.level >> k5.run_time>>k5.name; pcb a[5] = { k1, k2, k3, k4, k5 }; static int sxh = 5; while (sxh >= 1) { for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 5; j++) { if (a[i].level <= a[j].level) { pcb k; k = a[i]; a[i] = a[j]; a[j] = k; } } } for (int i = 0; i <= sxh - 1; i++) { cout << " " << a[i].name; } cout << endl; cout << a[0].name<<" "; cout << endl; a[0].level--; a[0].run_time--; if (a[0].run_time == 0) { sxh--; a[0].level = 0; a[0].state = 1; } } system("pause"); return 0; }
这个程序重点是for循环的使用和static静态变量的合理使用,以及当需要删除数组里面的元素(程序用了覆盖输出的方法)。