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

📄 wjgl.cpp

📁 模拟操作系统管理内存方法
💻 CPP
字号:
// WJGL.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "string"
#include "stdlib.h"
#include "windows.h"
const int max=256;
using namespace std;
struct fcb                 
{
	string name;
	string type;
	fcb    *index;
	fcb    *prior;                  
	fcb    *next;
	int    memery_number;
};
fcb *head_fcb;
int memery[16][16];
int fat[max];
//------------------------------------------------------------------------------------------------
void init_memery()
{
	int m;
	for(int i=0;i<16;i++)
	{
		for(int j=0;j<16;j++)
		{
			m=rand()%2;
			memery[i][j]=m;
			cout<<memery[i][j]<<" ";
		}
		cout<<endl;
	}
}
//------------------------------------------------------------------------------------------------
void init_fat()
{
	for(int i=0;i<max;i++)
		fat[i]=-1;
}
//------------------------------------------------------------------------------------------------
int find_memery()
{
	for(int i=0;i<16;i++)
	{
		for(int j=0;j<16;j++)
			if(memery[i][j]==0)
				return (i*16+j);
cout<<"xiaosheng shi shabi"<<endl;
	}
	return -1;

}
//------------------------------------------------------------------------------------------------
fcb *find_fcb(fcb *head,string name,string type)
{
	fcb *rear_find_fcb;
	rear_find_fcb=head;
	while(rear_find_fcb->next!=NULL)
	{
		if(rear_find_fcb->type==type)
		{
			if(rear_find_fcb->name==name)
				return rear_find_fcb;
		}
		rear_find_fcb=rear_find_fcb->next;
	}
    if(rear_find_fcb->type==type)
	{
		if(rear_find_fcb->name==name)
			return rear_find_fcb;
	}
	rear_find_fcb=head;
	while(rear_find_fcb->prior!=NULL)
	{
		if(rear_find_fcb->type==type)
		{
			
			if(rear_find_fcb->name==name)
				return rear_find_fcb;
		}
		rear_find_fcb=rear_find_fcb->prior;
	}
	if(rear_find_fcb->type==type)
	{
		if(rear_find_fcb->name==name)
			return rear_find_fcb;
	}
	return NULL;
}
//------------------------------------------------------------------------------------------------
void show(fcb *head_show)
{
	fcb *rear_show;
	fcb *temp_show;
	rear_show=head_show;
	string chack[256];
	int top=-1;
/*	if(rear_show->index==NULL)
		cout<<"c:";
//		cout<<"c:\";
	else
	{	
		
		temp_show=find_fcb(rear_show,"..","content");
		rear_show=temp_show->index;
		cout<<rear_show->name;
		show(rear_show);
		cout<<rear_show->name<<"/";
	}
*/
	temp_show=find_fcb(rear_show,"..","content");
	while(temp_show->index->name!=".")
	{
		rear_show=temp_show->index;
		top++;
		chack[top]=rear_show->name;
		temp_show=find_fcb(rear_show,"..","content");		
	}
	cout<<"c:\\";
	for(int i=top;i>=0;i--)
	{
		cout<<chack[i]<<"\\";
	}

	
}
//------------------------------------------------------------------------------------------------
fcb *init_fcb(string name,string type,fcb *prior)
{
	fcb *fcb_init_fcb;
	int m=find_memery();
	fcb_init_fcb=new fcb;
	fcb_init_fcb->name=name;
	fcb_init_fcb->type=type;
    fcb_init_fcb->index=NULL;
	fcb_init_fcb->prior=prior;
	fcb_init_fcb->next=NULL;
	fcb_init_fcb->memery_number=m;
	return fcb_init_fcb;
}
//------------------------------------------------------------------------------------------------
bool chack_fcb(string name,string type)                        //  查找当前目录是否含有同名且同类的fcb
{
	fcb *rear_chack_fcb;
	rear_chack_fcb=head_fcb;
	while(rear_chack_fcb->next!=NULL)
	{
		if(rear_chack_fcb->name==name&&rear_chack_fcb->type==type)
			return true;
		rear_chack_fcb=rear_chack_fcb->next;
	}
	if(rear_chack_fcb->name==name&&rear_chack_fcb->type==type)
			return true;
	return false;
}
//------------------------------------------------------------------------------------------------
void MD()                                                   //创建一个目录 
{                          
	fcb *fcb_md;
	bool flag_md;
	string name;
	cin>>name;
	fcb *rear_md;
    rear_md=head_fcb;
	flag_md=chack_fcb(name,"content");
	if(flag_md==true)
		cout<<"创建的目录已经存在"<<endl;
	else
	{
		while(rear_md->next!=NULL)
		{
			rear_md=rear_md->next;
		}
		fcb_md=init_fcb(name,"content",rear_md);
		rear_md->next=fcb_md;
		fcb *index1,*index2;
		index1=init_fcb(".","content",NULL);
		index2=init_fcb("..","content",index1);
		fcb_md->index=index1;                      //创建新目录时,给它加两个一般目录
		index2->index=fcb_md;
		index1->index=index1;
		index1->next=index2;
	}
}
//------------------------------------------------------------------------------------------------
void CD()
{
	string name_cd;
	cin>>name_cd;
	getchar();
	fcb *rear_cd,*rear_cd1;
	rear_cd=head_fcb;
	bool flag_cd;
	flag_cd=chack_fcb(name_cd,"content");
	if(flag_cd==false)
		cout<<"目录不存在!"<<endl;
	else                      // 目录存在时                                      
	{
		rear_cd=find_fcb(head_fcb,name_cd,"content");
		rear_cd1=rear_cd->index;
		head_fcb=find_fcb(rear_cd1,".","content");
	}
}
//------------------------------------------------------------------------------------------------
void RD()
{
	string name_rd;
	cin>>name_rd;
	getchar();
	fcb *rear_rd,*temp_rd;
	rear_rd=head_fcb;
	if(chack_fcb(name_rd,"content")==true&&name_rd!=".."&&name_rd!=".")
	{
		temp_rd=find_fcb(head_fcb,name_rd,"content");
		rear_rd=temp_rd->index;
		if(rear_rd->next->next==NULL)
		{
			cout<<"是否真的要删除(y/n):";
			string flag;
			cin>>flag;
			getchar();
			if(flag=="y"||flag=="Y")
			{
				if(temp_rd->next!=NULL&&temp_rd->prior!=NULL)
				{
					temp_rd->prior->next=temp_rd->next;
					temp_rd->next->prior=temp_rd->prior;
				}
				else if(temp_rd->next==NULL)
					temp_rd->prior->next=NULL;
				else if(temp_rd->prior==NULL)
					temp_rd->next->prior=NULL;
				delete rear_rd->next;
				delete rear_rd;
				delete temp_rd;
			}
		}
		else
		{
			cout<<"不能删除,此目录非空!"<<endl;
		}
	}
	else if(name_rd!=".."&&name_rd!=".") 
		cout<<name_rd<<":不存在!"<<endl;
	else
		cout<<name_rd<<":不能删除!"<<endl;
}
//------------------------------------------------------------------------------------------------
void DIR()
{
	fcb *rear_dir;
	rear_dir=head_fcb;
	int content_number=0;
	int file_number=0;
	while(rear_dir->next!=NULL)
	{
		if(rear_dir->type=="content")
		{
			cout<<"\t<dir>\t"<<"content\t\t"<<rear_dir->name<<endl;
			content_number++;
		}
		else
		{
				cout<<"     \t"<<"  file  \t\t"<<rear_dir->name<<endl;
		    file_number++;
		}
		rear_dir=rear_dir->next;		
	}
	if(rear_dir->type=="content")
	{
		cout<<"\t<dir>\t"<<"content\t\t"<<rear_dir->name<<endl;
		content_number++;
	}
	else
	{
		cout<<"     \t"<<"  file  \t\t"<<rear_dir->name<<endl;
		file_number++;
	}
	cout<<"\t\t\t\t\t此目录下共有"<<content_number<<"个目录"<<endl;
	cout<<"\t\t\t\t\t共有文件:"<<file_number<<"个"<<endl;
}
//------------------------------------------------------------------------------------------------
void MK()
{
	fcb *rear_mk,*temp_mk;
	string name_mk;
	rear_mk=head_fcb;
	cin>>name_mk;
	getchar();
	if(chack_fcb(name_mk,"file")==true)
		cout<<"文件重名,不能建立!"<<endl<<endl;
	else
	{
		while(rear_mk->next!=NULL)
			rear_mk=rear_mk->next;
		temp_mk=init_fcb(name_mk,"file",rear_mk);
		rear_mk->next=temp_mk;
	}
}
//------------------------------------------------------------------------------------------------
void DEL()
{
	string name_del;
	cin>>name_del;
	fcb *rear_del;
	getchar();
	if(chack_fcb(name_del,"file")==false)
		cout<<"该目录下没有此文件"<<endl<<endl;
	else
	{
		rear_del=find_fcb(head_fcb,name_del,"file");
		if(rear_del->prior!=NULL&&rear_del->next!=NULL)
		{
			rear_del->prior->next=rear_del->next;
			rear_del->next->prior=rear_del->prior;
		}
		else if(rear_del->next==NULL)
			rear_del->prior->next=NULL;
		else
			rear_del->next->prior=NULL;
		delete rear_del;
	}
}
//------------------------------------------------------------------------------------------------
void control()
{
	void help();
	show(head_fcb);
	string demmand;
	cin>>demmand;
	getchar();
	while(demmand!="exit")
	{
		/*switch(demmand)
		{
		case "cd":
		case "CD":   CD();break;
		case "md":
		case "MD":   MD();break;
		case "dir":
		case "DIR":  DIR();break;
		case "mk":
		case "MK":   MK();break;
		case "rd":   
		case "RD":   RD();break;
		default:
			cout<<"系统没有"<<"'"<<demmand<<"'"<<"命令"<<endl<<endl;
		}
		*/
		if(demmand=="cd"||demmand=="CD")
			CD();
		else if(demmand=="md"||demmand=="MD")
			MD();
		else if(demmand=="dir"||demmand=="DIR")
			DIR();
		else if(demmand=="mk"||demmand=="MK")
			MK();
		else if(demmand=="rd"||demmand=="RD")
			RD();
		else if(demmand=="del"||demmand=="DEL")
			DEL();
        else if(demmand=="help"||demmand=="HELP")
			help();
		else
			cout<<"系统没有"<<"'"<<demmand<<"'"<<"命令"<<endl<<endl;
		show(head_fcb);
		cin>>demmand;
		getchar();
	}
	

}
//================================================================================================
void help()
{
	cout<<"all command:"<<endl;
	cout<<"\tmd\tcreating the contents."<<endl;
	cout<<"\tcd\tchanging the contents."<<endl;
	cout<<"\trd\tdelete the null contents."<<endl;
	cout<<"\tdir\tdisplaying the contents and the files."<<endl;
	cout<<"\tmk\tcreating a file."<<endl;
	cout<<"\tdel\tdelete the file."<<endl;
	cout<<"\thelp\thelp you know the command."<<endl;
}
//------------------------------------------------------------------------------------------------
void main()
{
	system("color 6a"); 
	init_memery();
	init_fat();                                        //  初始化fat,memery;
	fcb *fcb_rear,*fcb_rear1;
	fcb_rear=init_fcb(".","content",NULL);            //  初始化根目录
	fcb_rear1=init_fcb("..","content",fcb_rear);         //  初始化根目录当前目录fcb
    fcb_rear->next=fcb_rear1;
	fcb_rear->index=fcb_rear;
	fcb_rear1->index=fcb_rear;
	head_fcb=fcb_rear;
	help();
	control();
}

⌨️ 快捷键说明

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