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

📄 dosfolder.cpp

📁 包含操作系统原理书籍中所提到的很多方法的实现函数
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		{
			tmpIndex2 = tmpIndex1;  //tmpIndex2 save the block index
			tmpIndex1 = DiskSpace[tmpIndex2]; //tmpIndex1 save the content of the index
		}

		if((index2 = FindEmpty()) != 100)
		{
			DiskSpace[index2] = DiskSpace[tmpIndex2];
            DiskSpace[tmpIndex2] = index2;
			FileDirectory[index].FileBlockNum++;
		}
		else
		{
			printf("Out of memory!");
			fprintf(f,"Out of memory!");
			exit(0);
		}		
	}
}

void CreateFile(char *TempFileName, int index)  //创建一个新文件
// num means the block num
{
	int i, j, Tempindex1, TempIndex2 = 0;
	if( FileNum == 19)
	{
		printf("can't not create more files!");
		fprintf(f,"can't not create more files!");
		return;
	}
    for(j = 0; j < FileNum; j++)   //判断文件是否存在
	{
		if( !strcmp(TempFileName, FileDirectory[j].FileName))
			break;
	}
    if( j != FileNum)
	{                    
		printf("The file has existed.");
		printf("\nPress any key to continue...");
		
		fprintf(f,"The file has existed.");
		fprintf(f,"\nPress any key to continue...");
		getch();
	}
	else
	{        
		strcpy( FileDirectory[FileNum].FileName, TempFileName);
		FileDirectory[FileNum].FileBlockNum = index;
		for( i = 0 ; i <= index; i++)
		{      
			Tempindex1 = FindEmpty();
			if (i == 0)
			{
				FileDirectory[FileNum].BeginBlock=Tempindex1;
				TempIndex2 = Tempindex1;
			}
			else // if the else range write
			{					     
				DiskSpace[TempIndex2]=Tempindex1;
				DiskSpace[Tempindex1] = -3;	// mean the place not null					  
				TempIndex2 = Tempindex1;
			}
		}
		
		DiskSpace[TempIndex2] = -1;        // -1 means the end of the file              
		FileNum++;
		//printf("filenum=%d",FileNum);
	}
}
  
void DisplayDrawing(void)  //显示位图
{
	int i ,j = 0;
	//clrscr();
	printf("\n\n\n");
	fprintf(f,"\n\n\n");
	
	for( j = 0; j <= 9; j++)
		for( i =0; i <= 9 ; i++)	 
		{
			printf("%4d%", DiskSpace[ i+j*10]);
			fprintf(f,"%4d%", DiskSpace[ i+j*10]);
			if (i == 9)
			{
				printf ("\n");
				fprintf (f,"\n");
			}
		}
	printf("\nPress any key to continue...");
	fprintf(f,"\nPress any key to continue...");
	getch();
}
void DealMenu(int index )
{   //分别处理菜单项
	char TempFileName[20];
	int index1, index2;
	switch(index)
	{
         case 1:
			  Dir();
			  break;

		  case 2:
			  printf("Please input the name of the filename:");
			  fprintf(f,"Please input the name of the filename:");
			  scanf("%s", TempFileName);
			  fprintf(f,"%s\n", TempFileName);
			  if((index2 = FindFile(TempFileName))!=-1)
			  {
				  printf("You can input between 0 and %d\n",FileDirectory[index2].FileBlockNum-1);
				  printf("Please inpute the index in the file you want to delete");

				  fprintf(f,"You can input between 0 and %d\n",FileDirectory[index2].FileBlockNum-1);
				  fprintf(f,"Please inpute the index in the file you want to delete");
				  scanf("%d", &index1);
				  fprintf(f,"%d\n", index1);
				  DeleteRecord(index1, index2);
			  }
			  break;

		  case 3:
			  printf("Please input the name of the filename:");
			  fprintf(f,"Please input the name of the filename:");
			  scanf("%s", TempFileName);
			  fprintf(f,"%s\n", TempFileName);
			  DeleteAll(TempFileName);
			  break;

		  case 4:
			  InitProgram();
			  FileNum = 0;
			  break;
		  
		  case 5:
			  printf("Please input the name of the filename:");
			  fprintf(f,"Please input the name of the filename:");
			  scanf("%s", TempFileName);
			  fprintf(f,"%s\n", TempFileName);
			  if((index2 = FindFile(TempFileName))!=-1)
			  {
				  printf("You can input between 0 and %d\n",FileDirectory[index2].FileBlockNum-1);
				  printf("0 indicates that it will insert before the fire record. \n");
				  printf("%d indicates that it will insert after the last record. \n", 
                                    FileDirectory[index2].FileBlockNum);
				  printf("Please input the index in the file you want to insert:");

				  fprintf(f,"You can input between 0 and %d\n",FileDirectory[index2].FileBlockNum-1);
				  fprintf(f,"0 indicates that it will insert before the fire record. \n");
				  fprintf(f,"%d indicates that it will insert after the last record. \n", 
                                    FileDirectory[index2].FileBlockNum);
				  fprintf(f,"Please input the index in the file you want to insert:");
				  
				  scanf("%d", &index1);
				  fprintf(f,"%d\n", index1);
				  InsertRecord(index1, index2);
			  }
			  break;

		  case 6:
			  printf("Please input the name of the filename:");
			  fprintf(f,"Please input the name of the filename:");
			  scanf("%s", TempFileName);
			  fprintf(f,"%s\n", TempFileName); 
			  printf("Please input the number of tht blocks in the file you want to insert:\n");
			  fprintf(f,"Please input the number of tht blocks in the file you want to insert:\n");
			  scanf("%d", &index1);
			  fprintf(f,"%d\n", index1);
			  CreateFile(TempFileName, index1);
			  break;

		  case 7:					
			  DisplayDrawing();
			  break;
	}
}

void main()
{
	int index;
	f=fopen("result.txt","w");

	InitProgram();
	do{
		index = Menu();//显示菜单
		if (index == 8)
			break;
		if(index >= 1 && index < 8 )
			DealMenu(index);//处理菜单
	}while(1);
	
	fclose(f);
}

/* 程序的一个示例演示结果: 

The following is the instructions you can operatae.

     1: dir
     2: delete filename a record_index(>O)
     3: delete a file
     4: delete total files
     5: insert filename a record_index(>O)
     6: create a filename record_num(>O)
     7: display the drawing of the diskplace
     8: exit

There are not any files
Please select(1-8):6
Please input the name of the filename:file1
Please input the number of tht blocks in the file you want to insert:
5
The following is the instructions you can operatae.

     1: dir
     2: delete filename a record_index(>O)
     3: delete a file
     4: delete total files
     5: insert filename a record_index(>O)
     6: create a filename record_num(>O)
     7: display the drawing of the diskplace
     8: exit

The following are the files you can operate:
file1

Please select(1-8):6
Please input the name of the filename:file2
Please input the number of tht blocks in the file you want to insert:
9
The following is the instructions you can operatae.

     1: dir
     2: delete filename a record_index(>O)
     3: delete a file
     4: delete total files
     5: insert filename a record_index(>O)
     6: create a filename record_num(>O)
     7: display the drawing of the diskplace
     8: exit

The following are the files you can operate:
file1file2

Please select(1-8):1

Directory:
FileName:file1  BeginBlock:0 BlockNum:5
FileName:file2  BeginBlock:5 BlockNum:9

Press any key to continue...The following is the instructions you can operatae.

     1: dir
     2: delete filename a record_index(>O)
     3: delete a file
     4: delete total files
     5: insert filename a record_index(>O)
     6: create a filename record_num(>O)
     7: display the drawing of the diskplace
     8: exit

The following are the files you can operate:
file1file2

Please select(1-8):3
Please input the name of the filename:file1
The files have been deleted totatly.
Press any key to continue...The following is the instructions you can operatae.

     1: dir
     2: delete filename a record_index(>O)
     3: delete a file
     4: delete total files
     5: insert filename a record_index(>O)
     6: create a filename record_num(>O)
     7: display the drawing of the diskplace
     8: exit

The following are the files you can operate:
file2

Please select(1-8):1

Directory:
FileName:file2  BeginBlock:5 BlockNum:9

Press any key to continue...The following is the instructions you can operatae.

     1: dir
     2: delete filename a record_index(>O)
     3: delete a file
     4: delete total files
     5: insert filename a record_index(>O)
     6: create a filename record_num(>O)
     7: display the drawing of the diskplace
     8: exit

The following are the files you can operate:
file2

Please select(1-8):8




  */

⌨️ 快捷键说明

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