我需要問用戶插入進程數量,進程ID和等待時間 然後我必須根據進程優先級進行排序,比較和打印 我是新的C,I無法弄清楚如何做到這一點 任何幫助預先感謝 由於*/從Struct中的排序,比較和顯示C
struct Process {
int Id;
int Prio;
int Time;
};
struct Process process[100];
void init() {
printf("Enter number of processes:\n ");
scanf("%d",&n);
while(n<3) {
printf("Number has to be grater than 2\n");
scanf("%d",&n);
}
for (int x=0; x<n; x++) {
printf("Process %d ID:\n ", x+1);
scanf("%d",&process[x].Id);
printf("Process %d priority:\n ", x+1);
scanf("%d",&process[x].Prio);
printf("Process %d time:\n ", x+1);
scanf("%d",&process[x].Time);
}
}
void priority() {
for (int x=0; x<n; x++) {
printf("%d",process[x].Id);
printf(" %d",process[x].Prio);
printf(" %d\n\n",process[x].Time);
}
}
void line(int dashes) {
for(int x=1;x<dashes;x++) {
printf("-");
}
}
void display() {
printf("\n");
printf(" PROCESS SCHEDULING\n");
line(90);
printf("\n");
printf("ID");
printf(" PRIORITY");
printf(" WAITING TIME");
line(90);
printf("\n\n");
}
int main() {
init();
display();
priority();
return 0;
}
如果我理解你的問題,你需要根據優先級對進程進行排序,只需應用任何排序算法並通過process.priority進行比較即可。你的麻煩究竟是什麼? Sintax? – IssamTP
這是一項任務嗎?你正在關注哪個教程?你有沒有試過編譯這段代碼(變量「n」在哪裏)?哪個編譯器?哪個操作系統是這個? – x29a
這是一個作業IssamTP。這個想法是我不知道要應用的算法/ sintax。 – escuk