c语言课程设计文件搜索.docx
《c语言课程设计文件搜索.docx》由会员分享,可在线阅读,更多相关《c语言课程设计文件搜索.docx(31页珍藏版)》请在冰点文库上搜索。
c语言课程设计文件搜索
这是我大二时上c语言课程的课程设计。
程序基本功能:
1添加文件
2删除文件
3全部文件列表
4修改文件摘要
5按关键词查找文件
程序注释完全,可以按照步骤提示一步一步运行。
当然由于水平有限程序有不少bug,不过基本功能实现了。
希望能帮到需要的童鞋。
两外程序可以直接运行的。
如果赋值程序到VC++6.0环境下,中文及注释变成了问号。
可以发消息联系我,发给亲们源文件。
//Search.cpp:
Definestheentrypointfortheconsoleapplication.
//通信081
//最后链表写入后没有添加释放内存语句
//list时最后文件总是输出两遍
/*添加文件时注意添加到@之前已修正*/
//2010/08/03最后修改到Delet函数
//2010/08/05list函数有bug已修正
//2010/08/05修改到SearchKeys()
//08-06连续添加文件时第一个以后的.abs文件中写入不了内容解决
//小的bug有待纠正
//2010/08/05基本修改完成没有美化界面
//过期文件没有删除的提醒***********************************
//搜索关键词时同一文件中重复出现一个关键词输出也重复
//2010/08/07重复修改文件摘要时有错
#include"stdafx.h"
#include
#include
#include
#include
#include
#defineZ"创建时间:
"
#defineMAX20
structNameOfText{
charname[MAX];
structNameOfText*next;
};//程序开始读入文件名
structtm*stm;
voidWucan();
structNameOfText*AddFile(structNameOfText*);//加入文件
structNameOfText*DelFile(structNameOfText*);//删除文件
structNameOfText*ListFile(structNameOfText*);//文件列表
structNameOfText*ReviseFile(structNameOfText*);//修改文件
structNameOfText*SearchKeys(structNameOfText*);//搜索关键词
voidTimeDelay();
voidEXIT();
voidEXIT1();
voidmenu();
intFirstCheck();
structNameOfText*DuChu(structNameOfText*);
structNameOfText*XieRu(structNameOfText*);
voidRelease(structNameOfText*);
structNameOfText*head=NULL;
charstr[15],str1[15],str2[15];//str装文件名,str1装加了txt后缀的文件名,str2装加了abs后缀的文件名
charsstr1[5][15];//sstr1用来存放参数传递的关键词
intMonth,Day;
charmonth[3],day[3];
voidmain()
{
intm;//m为循环变量
chartemp[3];
time_tnow;
now=time(NULL);
stm=localtime(&now);
/**********************把int转换为string********************/
Month=stm->tm_mon+1;
Day=stm->tm_mday;
itoa(Month,month,10);
itoa(Day,day,10);
if(Month<10)
{
strcpy(temp,month);
month[0]='0';
month[1]='\0';
strcat(month,temp);
}
if(Day<10)
{
strcpy(temp,day);
day[0]='0';
day[1]='\0';
strcat(day,temp);
}
/*********************把int转换为string********************/
intnum,L=0;//num为菜单选项标号l控制是否退出系统
printf("\n\n\n");
printf("**********************\n");
printf("**\n");
printf("*欢迎使用科技查询!
*\n");
printf("**\n");
printf("**********************\n\n\n\n");
printf("Usage:
\n");
printf("程序具有以下功能:
\n\n");
printf("1添加文献\n");
printf("2删除文献\n");
printf("3全文献列表\n");
printf("4修改文献摘要\n");
printf("5按关键词查找\n\n");
printf("运行时请按照提示操作!
\n\n");
printf("********************************************************************************\n");
printf("请按Enter进入程序菜单列表\n");
getchar();
system("cls");
FirstCheck();/*检查PageBase是否存在*/
head=DuChu(head);/*读出文件名*/
for(m=0;;m++)
{
menu();
printf("\n请选择菜单序号:
");
scanf("%d",&num);
while(getchar()!
='\n');
switch(num)
{
case1:
head=AddFile(head);break;
case2:
head=DelFile(head);break;
case3:
head=ListFile(head);break;
case4:
head=ReviseFile(head);break;
case5:
head=SearchKeys(head);break;
case6:
EXIT();
default:
printf("Wronginput!
\nThenumbertochooserangefrom1to6!
\n");getchar();
}
system("cls");
//printf("正在回到主菜单...\n\n");
//TimeDelay();
//system("cls");
}
EXIT();
}
/*******************************************************************************************/
intFirstCheck()
{
FILE*fp;
if((fp=fopen("PageBase.txt","r"))==NULL)
{
printf("您是第一次使用科技查询系统!
下面将为您创建文件名库PageBase.\n\n请稍后...");
TimeDelay();
fp=fopen("PageBase.txt","w");
fputs("@",fp);
fclose(fp);
printf("\n\n文件名库PageBase创建成功!
请按Enter键退出,重新启动程序!
\n");
getchar();
EXIT1();
return1;
}
else
return0;
}
/*******************************************************************************************/
structNameOfText*DuChu(structNameOfText*head)
{
FILE*fp;
chars[20];
structNameOfText*tempnode;
structNameOfText*temp;
head=temp=(structNameOfText*)malloc(sizeof(structNameOfText));
head->next=NULL;
fp=fopen("PageBase.txt","r");
fgets(s,20,fp);
strcpy((*temp).name,s);
for(;(!
feof(fp));)
{
fgets(s,20,fp);//读取文件名
tempnode=(structNameOfText*)malloc(sizeof(structNameOfText));
temp->next=tempnode;
strcpy((*tempnode).name,s);
temp=tempnode;
(*temp).next=NULL;
}
fclose(fp);
returnhead;
}
/*******************************************************************************************/
structNameOfText*XieRu(structNameOfText*head)
{
FILE*fp;
chars[20];
structNameOfText*temp=head;
fp=fopen("PageBase.txt","w");
while(temp!
=NULL)
{
strcpy(s,(*temp).name);
fputs(s,fp);
temp=temp->next;
}
fclose(fp);
returnhead;
}
/******************************************************************************************/
voidRelease(structNameOfText*head)
{
structNameOfText*temp;
while(head->next!
=NULL)
{
temp=head;
head=head->next;
free(temp);
}
free(head);
}
/******************************************************************************************/
structNameOfText*AddFile(structNameOfText*head)
{
FILE*fp1,*fp2,*fp3,*fp;//fp1指向txt文件,fp2指向abs文件,fp3指向PageBase.txt
intc=0,L=0,b,i,a;//计算txt文件中前50个单词
charch,s[20];//ch用来向txt文件中写内容
structNameOfText*temp;
structNameOfText*temp1;
structNameOfText*temp2;
structNameOfText*temp3;
do{
b=0;
c=0;
temp=head;
temp1=head;
for(i=0;i<20;i++)
s[i]=0;
printf("请输入待添加的文件名:
\t\t例如:
wang\n");
gets(str);
strcpy(str1,str);
strcpy(str2,str);
strcat(str1,".txt");//在文件名后加.txt,赋给str1
strcat(str2,".abs");//在文件名后加.abs,赋给str2
/***************************用链表判断将要创建的文件是否存在*************************/
while(temp!
=NULL)
{
strcpy(s,(*temp).name);
s[strlen(s)-1]=0;
a=strcmp(s,str);
if(a==0)
{
b=1;
break;
}
else
temp=temp->next;
}
if(b==1)
{
printf("\n该文件已存在!
\n\n\n");
gotoloop;
}
/***********************************判断结束**************************************/
if((fp1=fopen(str1,"w"))==NULL)//创建txt文件
{
printf("创建文件失败!
\n");
printf("如果给您带来不便请谅解!
\n");
returnhead;
}
fclose(fp1);
if((fp1=fopen(str1,"a+"))==NULL)//打开txt文件,准备写入内容
{
printf("创建文件失败!
\n");
printf("如果给您带来不便请谅解!
\n");
returnhead;
}
printf("创建文件成功!
\n\n");
printf("请输入文件内容(以@结尾)!
\n");
for(;(ch=getchar())!
='@';)//向txt中写入内容
fputc(ch,fp1);
fprintf(fp1,"%s%d-%s-%s",Z,stm->tm_year+1900,month,day);
fputc('\n',fp1);
fclose(fp1);
fp1=fopen(str1,"r");
if((fp2=fopen(str2,"a+"))==NULL)//打开abs文件,生成摘要
{
printf("保存文件失败!
\n");
printf("如果给您带来不便请谅解!
\n");
remove(str1);
returnhead;
}
for(;!
feof(fp1)&&c<=50;)
{
ch=fgetc(fp1);
fputc(ch,fp2);
if(ch==''||ch=='\n')
c++;
}
fputc('\n',fp2);
fprintf(fp2,"%s%d-%s-%s",Z,stm->tm_year+1900,month,day);
fputc('\n',fp2);
fclose(fp2);
fclose(fp1);
/***************保存文件名到列表**************/
temp2=(structNameOfText*)malloc(sizeof(structNameOfText));
str[strlen(str)]='\n';
strcpy((*temp2).name,str);
if((*temp1).name[0]=='@')//文件库中还没有文件
{
head=temp2;
temp2->next=temp1;
}
else
{
while(temp1->next->next!
=NULL)
temp1=temp1->next;
temp2->next=temp1->next;
temp1->next=temp2;
temp3=head;
}
/***************保存文件名到列表**************/
system("cls");
printf("\n\n\n\n\n\n\n");
printf("保存成功!
\n\n");
loop:
printf("继续添加文件or退出?
\n\n\n选择0退出其他数字键继续!
\n");
scanf("%d",&L);
while(getchar()!
='\n');
if(L==0)
{
printf("已退出添加文件子菜单!
\n");
printf("正在回到主菜单...");
TimeDelay();
returnhead;
}
system("cls");
}while(L);
returnhead;
}
//**********************************************************************************
structNameOfText*DelFile(structNameOfText*head)
{
intL,a,a1=0,a2=0,b=0,i;
charch,s[20];
FILE*fp;
structNameOfText*temp;
structNameOfText*temp1;
do{
temp=head;
for(i=0;i<20;i++)
s[i]=0;
printf("请输入待删除文件名:
\n");
gets(str);
strcpy(str1,str);
strcpy(str2,str);
strcat(str1,".txt");//在文件名后加.txt,赋给str1
strcat(str2,".abs");//在文件名后加.abs,赋给str2
/********************从链表中查找待删除的文件(下)******************************/
while(temp!
=NULL)
{
strcpy(s,(*temp).name);
if(s[0]=='@')
{
printf("文件库中没有文件!
请先添加文件!
\n\n");
TimeDelay();
returnhead;
}
s[strlen(s)-1]=0;
a1=strcmp(s,str);
if(s[0]!
='@'&&a1==0)//找到待删除的文件名
{
b=1;
break;
}
else
temp=temp->next;
}
/********************从链表中查找待删除的文件(上)******************************/
if(b==0)
{
printf("不存在该文件!
\n\n\n");
gotoloop;
}
printf("确定删除文献?
请选择y删除,其他键返回\n");
ch=getchar();
if(ch=='Y'||ch=='y')
{
if((fp=fopen(str1,"r"))==NULL)
{
printf("\n\n该文件已被删除!
\n\n");
gotoloop;
}
else
{
fclose(fp);
remove(str1);
remove(str2);
}
/***************************从链表中删除文件(下)******************************/
strcpy(s,(*head).name);
s[strlen(s)-1]=0;
a=strcmp(s,str);
if(a==0)//要删除的文件名在表首
{
temp1=head;
head=head->next;
free(temp1);
}
temp=head;
temp1=head->next;
while(temp1!
=NULL)
{
strcpy(s,(*temp1).name);
s[strlen(s)-1]=0;
a=strcmp(s,str);
if(a==0)/*在链表中找到待删除的文件*/
{
temp->next=temp1->next;
free(temp1);
break;
}
else
{
temp1=temp1->next;
temp=temp->next;
}
}
/*************************从链表中删除文件(上)*********************************/
printf("文献已删除!
\n\n\n");
}
loop:
printf("继续删除文件or退出?
\n\n\n选择0退出其他数字键继续!
\n");
scanf("%d",&L);
while(getchar()!
='\n');
if(L==0)
{
printf("正在回到主菜单...\n");
TimeDelay();
returnhead;
}
system("cls");
}while(L);
returnhead;
}
//***********************************************************************************
structNameOfText*ListFile(structNameOfText*head)
{
system("cls");
inti,j=0,k;
char