• 实验二 总结


    代码如下:

    #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("
    
    ");
            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("
    作业%s正在运行,其运行情况为:
    ",q->name);
            printf("开始运行时刻:%d
    ",q->starttime);
            printf("完成时刻:%d
    ",q->finishtime);
            printf("周转时间:%f
    ",q->cycletime);
            printf("带权周转时间:%f
    ",q->weightedtime);
            printf("响应比:%f
    ",q->response);
            getch();
        }
        else{                         //显示先来先服务,最短作业优先算法调度后作业的运行情况 
             printf("
    作业%s正在运行,其运行情况为:
    ",q->name);
             printf("开始运行时刻:%d
    ",q->starttime);
            printf("完成时刻:%d
    ",q->finishtime);
            printf("周转时间:%f
    ",q->cycletime);
            printf("带权周转时间:%f
    ",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("
    %s has been finished!
     press any key to continue...
    ",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("
    
    作业已全部完成!");
        printf("
    %d个作业的平均周转时间是:%f",n,t);
        printf("
    %d个作业的平均带权周转时间是%f:
    
    
    ",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("
     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("
     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("
    没有满足要求的进程,需等待");
                time++;
                if(time>100){
                    printf("
    时间过长");
                    getch();
                } 
                else{
                    running(p,m);
                }
            }
        }
        final();
    }
    void output(){
        int m;
        system("cls");
        printf("
    
    		**********************************		
    ");
        printf("				作业调度演示
    ");
        printf("
    
    
    			1.先来先服务算法."); 
        printf("
    			2.最短作业优先算法.");
        printf("
    			3.响应比高者优先算法.");
        printf("
    			0.退出程序.");
        printf("
    
    				选择所要操作:");
        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();
    }


    总结:目前这段代码还有点小问题,我会继续改进。这次的实验主要是调用函数和指针、栈的问题,之前数据结构学过的,不过。。我还给老师了。。所以这次的实验做起来很吃力,不过我会继续努力的

  • 相关阅读:
    centos7.4 系统安装指导
    win10下硬盘安装CentOS7
    CentOs7.X下配置FTP
    pyspider 安装使用过程的一些坑
    .Net Core 商城微服务项目系列(十三):搭建Log4net+ELK+Kafka日志框架
    .Net Core自动化部署系列(二):使用Jenkins打造镜像发布流水线
    Kubernetes 系列(六):Kubernetes部署Prometheus监控
    Kubernetes 系列(五):Prometheus监控框架简介
    .Net Core 商城微服务项目系列(十二):使用k8s部署商城服务
    Kubernetes 系列(四):使用Traefik访问.net core api
  • 原文地址:https://www.cnblogs.com/colorH/p/4491463.html
Copyright © 2020-2023  润新知