这次的实验,我觉得挺有难度的
代码如下:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #define getpch(type)(type*)malloc(sizeof(type)) #define NULL 0 struct jcb{ char name[10]; //work name int reachtime; int starttime; int needtime; int finishtime; float cycletime; //周转时间 float weightedtime; //带权周转时间 float response; //响应比 char state; //状态 struct jcb *next; //结构体指针 }*ready=NULL,*p,*q; typedef struct jcb JCB; int n,time=0; //how many? time first work; float T1=0,T2=0; void input(){ int i; printf("输入作业个数(2-24)(输入1为默认值5): "); scanf("%d",&n); if(n==1){ n=5; } for(i=0;i<n;i++){ p=getpch(JCB); printf("\n\n"); printf("输入作业名:"); scanf("%s",p->name); printf("输入到达时间:"); scanf("%d",&p->reachtime); printf("输入所需运行时间:"); scanf("%d",&p->needtime); p->state=‘W‘; p->next=NULL; if(ready==NULL) ready=q=p; else{ q->next=p; q=p; } } } void disp(JCB *q,int m){ //显示作业运行后的周转时间及带权周转时间等 if(m==3){ //显示高响应比算法调度作业后的运行情况 printf("\n作业%s正在运行,其运行情况为:\n",q->name); printf("开始运行时刻:%d\n",q->starttime); printf("完成时刻:%d\n",q->finishtime); printf("周转时间:%f\n",q->cycletime); printf("带权周转时间:%f\n",q->weightedtime); printf("响应比:%f\n",q->response); getch(); } else{ //显示先来先服务,最短作业优先算法调度后作业的运行情况 printf("\n作业%s正在运行,其运行情况为:\n",q->name); printf("开始运行时刻:%d\n",q->starttime); printf("完成时刻:%d\n",q->finishtime); printf("周转时间:%f\n",q->cycletime); printf("带权周转时间:%f\n",q->weightedtime); getch(); } } void running(JCB *p,int m){ //运行作业 if(p==ready){ //先将要运行的作业从队列中分离出来 ready=p->next; p->next=NULL; } else{ q=ready; while(q->next!=p){ q=q->next; } q->next=p->next; } p->starttime=time; p->state=‘R‘; p->finishtime=p->starttime+p->needtime; p->cycletime=(float)(p->finishtime-p->reachtime); p->weightedtime=(float)(p->cycletime/p->needtime); T1=T1+p->cycletime; T2=T2+p->weightedtime; disp(p,m); time=p->needtime+time; p->state=‘F‘; printf("\n%s has been finished!\n press any key to continue...\n",p->name); free(p); getch(); } void response(){ //计算队列中作业的高响应比 JCB *padv; padv=ready; do{ if(padv->state==‘W‘&&padv->reachtime<=time) padv->response=(float)(time-padv->reachtime+padv->needtime)/padv->needtime; padv=padv->next; }while(padv!=NULL); } void final(){ //最后打印作业的平均周转时间,平均带权周转时间 float s,t; t=T1/n; s=T2/n; getch(); printf("\n\n作业已全部完成!"); printf("\n%d个作业的平均周转时间是:%f",n,t); printf("\n%d个作业的平均带权周转时间是%f:\n\n\n",n,s); } void hrrn(int m){ //高响应比算法 JCB *min; int i,iden; system("cls"); input(); for(i=0;i<n;i++){ p=min=ready; iden=1; response(); do{ if(p->state==‘W‘&&p->reachtime<=time){ if(iden){ min=p; iden=0; } else if(p->response>min->response) { min=p; } p=p->next; } }while(p!=NULL); if(iden){ i--; time++; if(time>1000){ printf("\n runtime is too long .. error.."); getch(); } } else{ running(min,m); } } final(); } void sjf(int m){ //最短作业优先 JCB *min; int i,iden; system("cls"); input(); for(i=0;i<n;i++){ p=min=ready; iden=1; response(); do{ if(p->state==‘W‘&&p->reachtime<=time) if(iden){ min=p; iden=0; } else if(p->response>min->response){ min=p; } p=p->next; }while(p!=NULL); if(iden){ i--; time++; if(time>1000){ printf("\n runtime is too long .. error.."); getch(); } else{ running(min,m); } } } final(); } void fcfs(int m){ //先来先服务 int i,iden; system("cls"); input(); for(i=0;i<n;i++){ p=ready; iden=1; do{ if(p->state==‘W‘&&p->reachtime<=time) iden=0; if(iden) p=p->next; }while(p!=NULL&&iden); if(iden){ i--; printf("\n没有满足要求的进程,需等待"); time++; if(time>100){ printf("\n时间过长"); getch(); } else{ running(p,m); } } } final(); } void output(){ int m; system("cls"); printf("\n\n\t\t**********************************\t\t\n"); printf("\t\t\t\t作业调度演示\n"); printf("\n\n\n\t\t\t1.先来先服务算法."); printf("\n\t\t\t2.最短作业优先算法."); printf("\n\t\t\t3.响应比高者优先算法."); printf("\n\t\t\t0.退出程序."); printf("\n\n\t\t\t\t选择所要操作:"); scanf("%d",&m); switch(m){ case 1: fcfs(m); getch(); system("cls"); output(); break; case 2: sjf(m); getch(); system("cls"); output(); break; case 3: hrrn(m); getch(); system("cls"); output(); break; case 0: system("cls"); break; default: printf("选择错误,重新选择."); getch(); system("cls"); output(); } } main(){ output(); }
目前这段代码还有点小问题,我会继续改进。这次的实验主要是调用函数和指针、栈的问题,之前数据结构学过的,不过。。我还给老师了。。所以这次的实验做起来很吃力,不过我会继续努力的
时间: 2024-11-05 13:26:30