📄 a.c
字号:
#include<malloc.h>
#include<stdio.h>
#include<string.h>//主目录
struct linkf
{char username[16];
struct links *next;
};
struct linkr//文件-记录类型
{char name[16];
int sex;
struct linkr *next;
};
struct links//用户文件目录
{char filename[16];
struct linkr *headlink;//串联文件连接指针
int length;
struct links *next;//表目链表指针
};
struct linkt //打开文件表
{char filename[16];
int flag; //修改标志 0-没有修改 1-修改过
int length; //记录数
struct linkr *headlink; //串联文件头指针
struct linkt *next; //表目链表指针
};
struct linkf mfd[5];
struct links *ufd,*p2,*q2;
struct linkr *p,*q;
struct linkt *afd,*p3,*q3;
char X[16],s,username[16];
int r,i,w,k,n,m,j,t,flag;
void print1()
{int i;
printf("\n MFD:\n");
printf("----------------\n");
for(i=0;i<n;i++)
{printf("| %s",mfd[i].username);
printf("\n----------------\n");
}
}
void print2()
{int i;
for(i=0;i<n;i++)
if (flag==i && ufd!=NULL)
{printf("\n MFD__[%s]\n",mfd[i].username);
p2=mfd[i].next;
printf("+_UFD-Filename_+_ Length _+\n");
while(p2!=NULL)
{printf("|%10s | %4d |\n",p2->filename,p2->length);
p2=p2->next;
printf("+--------------+--------------+\n");
}
}
if(ufd==NULL) printf("\nUFD_[%s] is NULL\n",username);
if(afd!=NULL)
{p3=afd;
printf("\n\nAFD_[%s]\n",username);
printf("+-----Filename-----+----length---+\n");
while(p3!=NULL)
{printf("|%12s | %d |\n",
p3->filename,p3->length);
p3=p3->next;
printf("+------------------|-------------+\n");
}
}
else printf("\n\nAFD_[%s] is NULL\n",username);
}
void print3()
{p3=afd;
printf("Input printing-filename:"); scanf("%s",X);
if(p3!=NULL)
{while((strcmp(p3->filename,X)!=0)&&(p3->next!=NULL))
p3=p3->next;
if(strcmp(p3->filename,X)!=0)
printf("The file hasn't opened!\n");
else
if(p3->headlink==NULL) printf("The file is NULL!\n");
else {p=p3->headlink; i=1;
printf("| num | name | sex |\n");
printf("---------------------------------------\n");
while (p!=NULL)
{printf("|%5d |%10s | %d |\n",i,p->name,p->sex);
i++; p=p->next;
printf("---------------------------------------\n");
}
}
} else printf("The file hasn't opened!\n");
}
void pcreate()
{ printf("Input create filename:");scanf("%s",X);
q2=ufd;
if (q2!=NULL)
{while((strcmp(q2->filename,X)!=0)&&(q2->next!=NULL))
q2=q2->next;
if(strcmp(q2->filename,X)==0)
{printf("File has already been created!\n");
return;
}
}
p2=(struct links *)malloc(sizeof(struct links));
if(ufd==NULL) ufd=p2; else q2->next=p2;
strcpy(p2->filename,X);
p2->length=0;
p2->next=NULL;
p2->headlink=NULL;
}
void popen()
{ p2=ufd;
printf("Input Open filename:");scanf("%s",X);
if(p2!=NULL)
{while((strcmp(p2->filename,X)!=0)&&(p2->next!=NULL))
p2=p2->next;
if(strcmp(p2->filename,X)==0)
{q3=afd;
if(afd!=NULL)
{while((strcmp(q3->filename,X)!=0)&&(q3->next!=NULL))
q3=q3->next;
if(strcmp(q3->filename,X)==0)
{printf("The file \"%s\" has been opened.\n",X);
return;
}
}
p3=(struct linkt *)malloc(sizeof(struct linkt));
if(afd==NULL) afd=p3; else q3->next=p3;
strcpy(p3->filename,X);
p3->length=p2->length;
p3->headlink=p2->headlink;
p3->flag=0;
p3->next=NULL;
printf("The file \"%s\"has been opened.\n",X);
return;
}
}
printf("Can't find \"%s\" file in \"%s\" USER.\n",X,username);
}
void pclose()
{
int n=0;p3=q3=afd;
if(afd==NULL)
{printf("You have not open files!");return;}
else
printf("Input Open filename:");scanf("%s",X);
while((strcmp(p3->filename,X)!=0)&&(p3->next!=NULL))
{
n++;q3=p3;p3=p3->next;}
if(strcmp(p3->filename,X)!=0)
{
printf("The file hasn't be found!\n");
return;
}
else
{ if(n==0) afd=afd->next;
else q3->next=p3->next;}
if(p3->flag==0)
{free (p3);
printf("The file ""%s" " is closed!\n",X);
return;
}
else
{
p2=ufd;
if(p2!=NULL)
{while((strcmp(p2->filename,X)!=0)&&(p2->next!=NULL))
p2=p2->next;
if(strcmp(p2->filename,X)==0)
{
p2->length=p3->length;
p2->headlink=p3->headlink;
}
}
q3=p3->next;
free (p3);
printf("The file ""%s" " is closed!\n",X);
return;
}
}
void pdelete()
{ int n=m=0;
printf("Input the filename:");
scanf("%s",X);
q3=afd;
while(q3!=NULL && strcmp(q3->filename,X)!=0)
{n++;p3=q3;q3=q3->next;}
if(q3!=NULL)
{
if(n==0)
afd=afd->next;
else
p3->next=q3->next;
free(q3);//free(p3);
}
{
q2=ufd;
while(q2!=NULL && strcmp(q2->filename,X)!=0)
{m++;p2=q2;q2=q2->next ;}
if(q2==NULL)
printf("The file %s is wrong!",X);
else
{
if(m==0)
{
ufd=ufd->next;
mfd[flag].next=ufd;
}
else
p2->next=q2->next;
free(q2);//free(p2);
printf("The file ""%s" " is deleted!\n",X);
}
}
}
close()
{while (afd!=NULL)
{p3=afd; afd=afd->next;
p2=ufd;
while(strcmp(p2->filename,p3->filename)!=0
&&p2->next!=NULL) p2=p2->next;
if(strcmp(p2->filename,p3->filename)==0)
{p2->length=p3->length; p2->headlink=p3->headlink;
}
free(p3);
}
}
pread()
{ int ri,i=1;
printf("Input the filename:");
scanf("%s",X);
q3=afd;
while(q3!=NULL && strcmp(q3->filename,X)!=0)
q3=q3->next;
if(q3==NULL) {printf("file have not found!");return;}
else
{printf("input record :\n");scanf("%d",&ri);
if(ri>0)
{p=q3->headlink;
while(p!=NULL&&ri>i)
{p=p->next;i++;}
if(p==NULL) {printf("The record number ERROR!");return;}
else
printf("--------------------------\n");
if(p->sex ==0)
printf("Student-num : %d\nStudent-name: %s\nStudent-sex : %s\n",i,p->name,"men");
else
printf("Student-num : %d\nStudent-name: %s\nStudent-sex : %s\n",i,p->name,"women");
printf("--------------------------\n");
return;
}
printf("The record number ERROR!");
}
}
pwrite()
{ int sex;
printf("Input the filename:");
scanf("%s",X);
q3=afd;
while(q3!=NULL && strcmp(q3->filename,X)!=0)
q3=q3->next;
if(q3==NULL) {printf("file have not found/open!");return;}
else
{
p=(struct linkr *)malloc(sizeof(struct linkr));
printf("input name:\n");scanf("%s",username);strcpy(p->name,username);
printf("input sex (0/1):\n");scanf("%d",&sex);p->sex=sex;
if(q3->headlink==NULL)
{
p->next=q3->headlink;
q3->headlink=p;
}
else
{
q=q3->headlink;
while(q->next!=NULL)
q=q->next;
p->next=q->next;
q->next=p;
}
q3->flag=1;
q3->length++;
return;
}
}
main()
{ afd=NULL; ufd=NULL;
for (i=0;i<5;i++)
{strcpy(mfd[i].username," ");
mfd[i].next=NULL;
}
printf("User number N(N<=5)=");scanf("%d",&n);
for(i=0;i<n;i++)
{w=0;
printf("User MFD username X[%d]=",i+1);scanf("%s",X);
for (j=0;j<n;j++)
if (strcmp(mfd[j].username,X)==0) w=1;
if(w==0) strcpy(mfd[i].username,X);
else
{ printf("\nThe Username has been Created!\n");i--;}
}
do
{ print1();
do
{ printf("Input user name you want to operate:");
scanf("%s",username);
for(i=0;(i<n&&(strcmp(mfd[i].username,username)!=0));i++);
if(i>=n) {printf("Not found Username!\n");}
} while(i>=n);
t=0; flag=i; ufd=mfd[i].next;
do
{ printf("\n------------------------------------\n");
printf(" 1--Creat 2--Open 3--Delete\n");
printf(" 4--cLose 5--Read 6--Write\n");
printf(" 7--disPlay 8--fileplay 9--Quit\n");
printf("------------------------------------\n");
printf(" Select[ ]\b\b");
scanf("%s",&s);
switch(s)
{case '1':pcreate();break;
case '2':popen();break;
case '3':pdelete();break;
case '4':pclose();break;
case '5':pread();break;
case '6':pwrite();break;
case '7':print2();break;
case '8':print3();break;
case '9':t=1;close();
}
mfd[i].next=ufd;
} while(!t);
printf("\n---------------------------------------\n");
printf("| 1--Edit other user , 2--Quit |\n" );
printf("---------------------------------------\n");
printf(" Select[ ]\b\b");
scanf("%d",&m);
if(m==2) r=1;
else r=0;
} while(!r);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -