/*
先来先服务FCFS要求:
1>用结构体定义程序:程序号,优先数(到达时间),CPU运算所需时间2>输入进程序列
3>按优先数高低排序输出
*/ #include<stdio.h>#include<string.h>#define N 100typedef struct Time{ int reachTime; //优先数(程序到达时间) int runningTime; //CPU运算时间}Time;typedef struct Information
{ long ID; //程序号 Time pay;}INF;int n;
void readpay(INF p[]);void DeSortbytheFirst(INF p[]);void main() //主函数{ INF p[N]; struct Time; int sum=0; int i; long number; printf("请输入程序个数:(<100个)"); scanf("%d",&n); readpay(p); for(i=0;i<n;i++) { sum=sum+p[i].pay.reachTime+p[i].pay.runningTime; } printf("\n\n\t\t#####总周转时间为:%d毫秒#####\n\t\n",sum);DeSortbytheFirst(p);
}void readpay(INF p[]) //1.输入程序资料
{ int i; for(i=0;i<n;i++) { printf("\n\t请输入第%d个程序号:\n",i+1); scanf("\t\t%ld",&p[i].ID);printf("请输入第%d个程序的到达时间:\n",i+1);
scanf("\t\t%d",&p[i].pay.reachTime);printf("请输入第%d个程序的CPU运算时间:\n",i+1);
scanf("\t\t%d",&p[i].pay.runningTime); printf("\t\n%10ld %d\t%d\t\n \t周转时间:%d\n",p[i].ID,p[i].pay.reachTime,p[i].pay.runningTime,p[i].pay.reachTime+p[i].pay.runningTime); }}void DeSortbytheFirst(INF p[]) //2.按优先级高到低排序输出
{ int i,j,k,last; int All; int a1,a2; INF temp;printf("\t####################按优先级高低排序输出##################\n\n");
printf("\n\t程序号\t到达时间\t周转时间\n");for(i=0;i<n;i++)
{ k=i; for(j=i+1;j<=n;j++) { a1=p[i].pay.reachTime; a2=p[j].pay.reachTime; if(a1>a2) { temp=p[j]; p[j]=p[i]; p[i]=temp; } }for(k=1;k<n+1;k++)
{ All=p[k].pay.reachTime+p[k].pay.runningTime; last=k; } printf("%10ld\t%d\t\t%d\t\n",p[last].ID,p[last].pay.reachTime,All); } }