计算机操作系统 电梯算法,操作系统实验报告四模拟电梯调度算法,对磁盘进行移臂和旋转调度...

#include #define null 0

#define len sizeof(struct cidaohao)

struct cidaohao

{

struct cidaohao *pre;

int num;

struct cidaohao *next;

};

FCFS(array)

int array[50];

{int i,j,sum=0;

printf("nFCFS : ");

for(i=1;array[i]!=-1;i++)

{

printf(" %d ",array[i]);

}

i=0;

for(i=0,j=1;array[j]!=-1;i++,j++)

{

if(array[i]>array[j]) sum+=(array[i]-array[j]);

else sum+=(array[j]-array[i]);

}

return(sum);

}

SSTF(head,now)

struct cidaohao *head;

int now;

{struct cidaohao *p,*lp,*rp;

int sum=0,front,behind;

p=head;

printf("nSSTF :");

while(p->num!=now) p=p->next;/* */

lp=p->pre;

rp=p->next;

do

{

if(p->next!=null&&p->pre!=null)

{front=p->num-lp->num;

behind=rp->num-p->num;

if(front>=behind)

{

sum+=behind;

p=rp;

printf(" %d ",p->num);

rp=p->next;

}

else

{

sum+=front;

p=lp;

printf(" %d ",p->num);

lp=p->pre;

}

}

else

{

if(p->next==null)

{ while(lp->num!=0)

{

sum+=p->num-lp->num;

p=lp;

printf(" %d ",p->num);

lp=p->pre;

}

return(sum);

}

if(p->pre==null)

{

while(rp->num!=0)

{

sum+=rp->num-p->num;

p=rp;

printf(" %d ",p->num);

rp=p->next;

}

return(sum);

}

}

}while(p->next!=null||p->pre!=null);

}

SCAN(head,n,m)

struct cidaohao *head;

int n,m;

{struct cidaohao *p,*pp;

int sum=0;

printf("nSCAN : ");

p=head;

while(p->num!=m) p=p->next;/* */

pp=p;

if(n{while(pp->next!=null)

{

sum+=pp->next->num-pp->num;

pp=pp->next;

printf(" %d ",pp->num);

}

sum+=pp->num-p->pre->num;

pp=p->pre;

if(pp->num==0) return(sum);

else

{while(pp->pre!=null)

{

printf(" %d ",pp->num);

sum+=pp->num-pp->pre->num;

pp=pp->pre;

}

printf(" %d ",pp->num);

return(sum);

}

}

else

{

while(pp->pre!=null)

{

sum+=pp->num-pp->pre->num;

pp=pp->pre;

printf(" %d ",pp->num);

}

sum+=p->next->num-pp->num;

pp=p->next;

if(pp->num==0) return(sum);

else

{while(pp->next!=null)

{

printf(" %d ",pp->num);

sum+=pp->next->num-pp->num;

pp=pp->next;

}

printf(" %d ",pp->num);

return(sum);

}

}

}

main()

{

FILE *fp;

char pt;

char str[10];

int cidao[100],i,j=1,count1=0,count2=0,count3=0,last,space=0;

struct cidaohao *p1,*p2,*new,*head;/* */

struct cidaohao *p,*lp,*rp;

for(i=0;i<50;i++) cidao[i]=-1;

i=0;

fp=fopen("cipan.txt","r+");/* */

if(fp==NULL)

{

printf("Cann't open this file ");

exit(0);

}

printf("nPlease input cidaohao now : ");

scanf("%d",&cidao[0]);

while((pt=fgetc(fp))!=EOF)/* */

{

if(pt>='0'&&pt<='9')

{

str[i]=pt;i++;

space=0;

}

else

{

if(pt==' '||pt=='n')

{if(space==1) break;

else

{str[i]='0';

cidao[j]=atoi(str);

if(pt=='n') break;

else

{

space=1;

j++;

i=0;

}

}

}

}

}/* */

if(pt==EOF) {str[i]='0';cidao[j]=atoi(str);}

fclose(fp);

i=0;

count1=FCFS(cidao);/* */

printf("nThe Total : %d n",count1);

p1=p2=head=(struct cidaohao* )malloc(len);/* */

p1->pre=null;

p1->num=cidao[0];

p1->next=null;

i=1;

while(cidao[i]!=-1)

{ if(cidao[i]num)/* */

{

p1=(struct cidaohao *)malloc(len);

p1->next=head;

p1->pre=null;

p1->num=cidao[i];

head->pre=p1;

head=p1;

}

else

{

while(p1->next!=null&&p1->num<=cidao[i])/* */

{ p2=p1;

p1=p1->next;

}

if (p1->num>cidao[i])/* */

{

new=(struct cidaohao*)malloc(len);

new->num=cidao[i];

new->next=p1;

new->pre=p2;

p1->pre=new;

p2->next=new;

}

else

{/* */

new=(struct cidaohao*)malloc(len);

new->num=cidao[i];

new->next=null;

new->pre=p1;

p1->next=new;

}

p1=head;/* */

}

i++;/* */

}

count2=SSTF(head,cidao[0]);

printf("nThe Total : %d ",count2);

printf("nnPleast input last cipanhao : ");

scanf("%d",&last);

count3=SCAN(head,last,cidao[0]);

printf("nThe Total : %d n",count3);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值