⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miao.cpp

📁 操作系统中的磁盘调度算法
💻 CPP
字号:
# include<stdio.h>
int i,j,k,number,dis,initial,b,*a;
void bubble_sort()
{ int swap;
  for(i=number-1;i>0;--i)
	  for(j=0;j<i;++j)
		  if(a[j]>a[j+1])
		  {
			  swap=a[j];
			  a[j]=a[j+1];
			  a[j+1]=swap;
		  }
}
void scan()
{
	printf("This is the solution of scan:\n");
	printf("The sequence of the disks is :\n");
    printf("ID\tdistance\n");
	while(i<number-1)
	{
		if(a[i]<initial)
		{
			i++;
			continue;
		}
		break ;
	}
		if(i==number-1)
		{
			while(i>=0)
			{
				dis=initial-a[i];
				printf("%d\t   %d\n",a[i],dis);
				initial=a[i];
				i--;
			}
		}
		else
		{
			k=i;
			while(i<number)
			{
				dis=a[i]-initial;
				printf("%d\t   %d\n",a[i],dis);
				initial=a[i];
				i++;
			}
			k--;
			while(k>0)
			{
				dis=initial-a[k];
				printf("%d\t   %d\n",a[k],dis);
				initial=a[k];
				k--;
			}
		}
}
void cscan()
{
	i=0;
	printf("This is the solution of cscan:\n");
	printf("The sequence of the disks is :\n");
    printf("ID\tdistance\n");
	while(i<number)
	{
		if(a[i]<initial)
		{
			i++;
			continue;
		}
		break;
	}
	if(i==number-1)
	{
		i=0;
		while(i<number-1)
		{
			dis=initial=a[i];
			printf("%d\t   %d\n",a[i],dis);
			initial=a[i];
			i++;
		}
	}
	else
	{
		k=i;
		while(i<number)
		{
			dis=a[i]-initial;
			printf("%d\t   %d\n",a[i],dis);
			initial=a[i];
			i++;
		}
		i=0;
		dis=initial-a[i];
		printf("%d\t   %d\n",a[i],dis);
		initial=a[i];
		i++;
		while(i<k)
		{
			dis=a[i]-initial;
			printf("%d\t   %d\n",a[i],dis);
			initial=a[i];
			i++;
		}
	}
}
void main()
{
	i=0;
	printf("Please input the initial disk:");
	scanf("%d",&b);
	initial=b;
	printf("Please input the disks(end with 0):\n");
	scanf("%d",&a[i]);
	i++;
	while(a[i-1]!=0)
	{
		scanf("%d",&a[i]);
		i++;
	}
	number=i-1;
	bubble_sort();
	i=0;
	printf("\nPlease input a number to choose the solution!\n");
	printf("1: choose scan!\n2: choose cscan!\n");
	scanf("%d",&b);
	switch(b)
	{
		case 1:scan(); break;
		case 2:cscan(); break;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -