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

📄 shujiu6.cpp

📁 操作系统中的文件是通过文件目录项来管理的
💻 CPP
字号:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct head{int share;int len;struct record *q;};
struct record{	char d;struct record *next;};
struct dir2
{char *n2;struct dir2 *p1;struct head *p2;};
struct dir1
{char* n1;int sum;struct dir2 *q;};

void newaccount(struct dir1 DM[5])
{char name[10],c;
int i;
printf("you are registing a new user .\n");do
	{
		printf("please input your name:\n");
		scanf("%s",name);
		i=0;
		while(i<=4&&strcmp(DM[i].n1,name)!=0)
			i++;
		if(i<=4)
		{
			printf("this account has already been used!\n");
			continue;
		}
		i=0;
		while(strcmp(DM[i].n1,"")!=0)
			i++;
		if(i>4)
		{
			printf("there are already five users now!\n");
			return;
		}
		DM[i].n1=(char *)malloc(strlen(name)*sizeof(char));
		strcpy(DM[i].n1,name);
		DM[i].sum=0;
		DM[i].q=NULL;
		printf("register successfully!\ncontinue?(y/n)");
		getchar();
		scanf("%c",&c);
	}
	while(c=='Y'||c=='y');
	
	return;
}

void delaccount(struct dir1 DM[5])
{
	char name[10];
	int i;
	printf("you are deleting an account now\ninput user name:\n");
	scanf("%s",name);
	i=0;
	while(i<=4&&strcmp(DM[i].n1,name)!=0)
		i++;
	if(i>4)
	{
		printf("No this account!\n");
		return;
	}
	if(DM[i].sum!=0)
	{
		printf("this user still has files.can't delete it !\n");
		return;
	}
	strcpy(DM[i].n1,"");
	printf("delating successfully!\n");
	return;
}

void outputfile(struct dir1 DM[5])
{
	char name[10],c;
	struct dir2 *j;
	struct record *k;
	int i;
	do
	{
		printf("input user name:");
		scanf("%s",name);
		i=0;
		while(i<=4&&strcmp(DM[i].n1,name)!=0)
			i++;
		if(i>4)
		{
			printf("   No this account!\n");
			continue;
		}
		printf("user name:%s     file number:%d\n",DM[i].n1,DM[i].sum);
		j=DM[i].q;
		while(j!=NULL)
		{
			printf("   file name:%s\n",j->n2);
			k=j->p2->q;
			printf("     file address:%d   file length:%d\n",k,j->p2->len);
			while(k->next!=NULL)
			{
				printf("      rec:%c    address:%d\n",k->d,k);
				k=k->next;
			}
			j=j->p1;
		}
		printf("continue?(y/n)");
		getchar();
		c=getchar();
	}while(c=='Y'||c=='y');
	return;
}

struct head *searchfile(struct dir1 DM[5],char *name,char *fname)
{
	int i;
	struct dir2 *j;
	i=0;
	while(i<=4&&strcmp(DM[i].n1,name)!=0)
		i++;
	if(i>4)
	{	printf("No this account!");
	    return NULL;
	}
	j=DM[i].q;
	while(j!=NULL&&strcmp(j->n2,fname)!=0)
		j=j->p1;
	if(j==NULL)
		return NULL;
	return j->p2;
}

void search(struct dir1 DM[5])
{
	char name[10],fname[10];
	printf("input user name:");
	scanf("%s",name);
	printf("please input the file name:");
	scanf("%s",fname);
	if(searchfile(DM,name,fname)==NULL)
		printf("no such file!\n");
	else
		printf("the file exists.\n");
	return;
}


void creatfile(struct dir1 DM[5])
{
	char name[10],fname[10],b,c;
	struct dir2 *j;
	struct head *k;
	struct record *rec;
	int i;

	printf("you are creating a new file \n");
	do{
		printf("please input your name:\n");
		scanf("%s",name);
		i=0;
		while(i<=4&&strcmp(DM[i].n1,name)!=0)
			i++;
		if(i>4)
		{
			printf("No this account!\n");
			continue;
		}
		printf("please input file name:\n");
		scanf("%s",fname);
		j=DM[i].q;
		while(j!=NULL&&strcmp(j->n2,fname)!=0)
			j=j->p1;
		if(j!=NULL)
		{
			printf("this file has already existed!\n");
			continue;
		}
		j=(struct dir2 *)malloc(sizeof(struct dir2));
		j->p1=DM[i].q;
		j->n2=(char *)malloc(sizeof(char)*strlen(fname));
		strcpy(j->n2,fname);
		DM[i].q=j;
		DM[i].sum=DM[i].sum+1;
		c='1';
		if(c=='y'||c=='Y')
		{
			printf("input the one you want to share:\n");
			printf("account name:");
			scanf("%s",name);
			printf("file name:");
			scanf("%s",fname);
			k=searchfile(DM,name,fname);
			j->p2=k;
			k->share++;
		}
		else
		{
			k=(struct head *)malloc(sizeof(struct head));
			k->share=1;
			k->len=0;
			rec=(struct record *)malloc(sizeof(struct record));
			k->q=rec;
			printf("please input your file record(end whith #):\n");
			scanf("%c",&b);
			while(b!='#')
			{
				k->len++;
				rec->d=b;
				rec->next=(struct record *)malloc(sizeof(struct record));
				rec=rec->next;
				scanf("%c",&b);
			}
			rec->d=b;
			rec->next=NULL;
			j->p2=k;
		}
	}while(c=='Y'||c=='y');
	return;
}

void del_all_file(struct dir1 DM[5])
{
	struct dir2 *j,*l;
	struct record *rec,*p;
	struct head *k;
	int i;
	char name[10];

	printf("you are delating the user's all the files!\n");
	printf("please input user name:");
	scanf("%s",name);
	i=0;
	while(i<=4&&strcmp(DM[i].n1,name)!=0)
		i++;
	if(i>4)
	{
		printf("No this account!\n");
		return;
	}
	j=DM[i].q;
	while(j!=NULL)
	{
		k=j->p2;
		if(k->share>1)
		{
			k->share--;
			break;
		}
		rec=k->q;
		while(rec!=NULL)
		{
			p=rec;
			rec=rec->next;
			free(p);
		}
		free(k);
		l=j;
		j=j->p1;
		free(l);
	}
	DM[i].sum=0;
	DM[i].q=NULL;
	printf("delating successfully!\n");
	return;
}

void del_file(struct dir1 DM[5])
{
	char name[10],fname[10];
	struct dir2 *j,*l;
	struct head *k;
	struct record *rec,*p;
	int i;
	
	printf("you are delating a file now!\n");
	printf("please input your user name:\n");
	scanf("%s",name);
	i=0;
	while(i<=4&&strcmp(DM[i].n1,name)!=0)
		i++;
	if(i>4)
	{
		printf("No this account!\n");
		return;
	}
	printf("please input file name:\n");
	scanf("%s",fname);
	l=j=DM[i].q;
	while(j!=NULL&&strcmp(j->n2,fname)!=0)
	{l=j;	j=j->p1;}
	if(j==NULL)
	{	
		printf("No this file!\n");
		return;
	}
	if(l==DM[i].q)
	{
		DM[i].q=NULL;
		free(l);
		printf("delating successfully!\n");
		return;
	}
	DM[i].sum--;
	k=j->p2;
	if(k->share>1)
		k->share--;
	else
	{
		rec=k->q;
		while(rec!=NULL)
		{
			p=rec;
			rec=rec->next;
			free(p);
		}
		free(k);
	}
	l->p1=j->p1;
	free(j);
	printf("delating successfully!\n");
	return;
}


void main()
{
	static struct dir1 DM[5]={{"",0,NULL},{"",0,NULL},{"",0,NULL},{"",0,NULL},{"",0,NULL}};
	int i;
	do
	{
		printf("\n");
		printf("input your choice:\n");
		printf("      1.regist new account;\n");
		printf("      2.creat a new file;\n");
		printf("      3.search a file;\n");
		printf("      4.delate a file;\n");
		printf("      5.delate an account;\n");
		printf("      6.delate all files of you;\n");
		printf("      7.output all files of you;\n");
		printf("      8.quit.\n");
		scanf("%d",&i);
		switch(i)
		{
		case 1:newaccount(DM);break;
		case 2:creatfile(DM);break;
		case 3:search(DM);break;
		case 4:del_file(DM);break;
		case 5:delaccount(DM);break;
		case 6:del_all_file(DM);break;
		case 7:outputfile(DM);break;
		default:break;
		}
		printf("\n");
	}
	while(i!=8);
}


⌨️ 快捷键说明

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