这是实践周的实践项目,通过使用文件操作和链表将数据进行存储和应用,代码部分只有重要代码部分,不包括登录检测代码。
0:运行图
1:首先使用单链表存储数据
//使用单链表,其中的功能有创建链表Initial_list() ; Data_input(SLtour stuhead) //输入节点相关信息
// print_list(SLtour stuhead) //对链表进行打印 print_list(SLtour stuhead) //对链表进行打印
// Insert_list(SLtour stuhead) //从表尾插入数据 seek_list(SLtour stuhead) //查询景点
// SLtour delete_list(SLtour stuhead) //删除节点
typedef struct Treval //旅游数据类型的定义
{
char province[20]; //省份
char city[20]; //城市
char scenic_spot[40];//景点
int tickets;//门票
int cost;//预计花销
char description[500]; //景点描述
struct Treval *Next;
}tour,*SLtour;
SLtour stuhead; //定义一个头指针
SLtour Initial_list() //动态分配大量节点
{
tour *p,*q;
int i,n,j=0;
stuhead=p=(tour*)malloc(sizeof(tour)); //头节点处不存储数据
printf("你想创建几个景点:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q=(tour*)malloc(sizeof(tour));//动态分配内存
if(q != NULL)
{
j++;
}
p->Next=q;
p=q;
}
p->Next=NULL;
printf("成功创建%d个景点\n",j);
return stuhead;
}
SLtour Data_input(SLtour stuhead) //输入节点相关信息
{
tour *p;
p=stuhead->Next;
while(p != NULL)
{
printf("请输入想创建景点的相关信息\n");
printf("所属省份:");
scanf("%s",p->province);
printf("所属城市:");
scanf("%s",p->city);
printf("景点名称:");
scanf("%s",p->scenic_spot);
printf("门票价:");
scanf("%d",&p->tickets);
printf("预计花销:");
scanf("%d",&p->cost);
printf("景点简介:");
scanf("%s",p->description);
printf("\n");
p=p->Next;
}
return stuhead;
}
int print_list(SLtour stuhead) //对链表进行打印
{
tour *p;
p=stuhead->Next;
printf(" \t\t\t景点信息如下\t\t\t \n");
while(p != NULL)
{
printf("-------------------------------------------------------------------------------\n");
printf("所属省份:");
printf("%s\t",p->province);
printf("所属城市:");
printf("%s\t",p->city);
printf("景点名称:");
printf("%s\t",p->scenic_spot);
printf("门票价:");
printf("%d\t",p->tickets);
printf("预计花销:");
printf("%d\t",p->cost);
printf("\n");
printf("景点简介:");
printf("%s\t",p->description);
printf("\n-------------------------------------------------------------------------------");
printf("\n");
p=p->Next;
}
return 0;
}
SLtour Insert_list(SLtour stuhead) //从表尾插入数据
{
tour *p,*q;
int i=1;
p=stuhead->Next;
while(p->Next != NULL)
{
i++;
p=p->Next;
}
printf("插入之前已有%d个景点\n",i);
if(p->Next==NULL)
{
q=(tour*)malloc(sizeof(tour)); //动态分配内存
if(q != NULL)
{
printf("成功插入一个景点\n");
printf("请输入你要插入的相关信息\n");
printf("所属省份:");
scanf("%s",q->province);
printf("所属城市:");
scanf("%s",q->city);
printf("景点名称:");
scanf("%s",q->scenic_spot);
printf("门票价:");
scanf("%d",&q->tickets);
printf("预计花销:");
scanf("%d",&q->cost);
printf("景点简介:");
scanf("%s",q->description);
printf("\n");
p->Next=q;
q->Next=NULL;
}
}
else
{
printf("内存不够,插入失败\n");
}
return stuhead;
}
int seek_list(SLtour stuhead) //查询景点
{
tour *p;
p=stuhead->Next;
printf("请输入你想查找的景点名字\n");
char name[40];
scanf("%s",name);
while(p != NULL)
{
if(strcmp(p->scenic_spot,name)==0)
{
printf("找到该景点:\n");
printf("所属省份:%s\t",p->province);
printf("所在城市:%s\t",p->city);
printf("景点名称:%s\t",name);
printf("门票价:%d\t",p->tickets);
printf("预计花销:%d\t",p->cost);
printf("\n");
printf("景点简介:%s",p->description);
printf("\n");
return 0;
}
p=p->Next;
}
if(p == NULL)
{
printf("未找到该景点\n");
}
return 0;
}
SLtour delete_list(SLtour stuhead) //删除节点
{
tour *p,*q;
char name[40];
printf("请输入你要删除的景点名称:\n");
scanf("%s",name);
p=stuhead->Next;
if(strcmp(p->scenic_spot,name)==0) //删除的结点在首元节点处
{
printf("找到该景点并进行删除\n");
stuhead->Next=p->Next;
return stuhead;
}
while((strcmp(p->scenic_spot,name)!=0) && (p != NULL))
{
q=p;
p=p->Next;
}
if((strcmp(p->scenic_spot,name)==0) && (p == NULL)) //删除结点在尾部
{
printf("找到该景点并进行删除\n");
q->Next=NULL;
return stuhead;
}
if(strcmp(p->scenic_spot,name)==0)
{
printf("找到该景点并进行删除\n");
q->Next=p->Next;
free(p); //释放删除景点储存空间
}
else
{
printf("未找到该景点\n");
}
return stuhead;
}
2:之后使用C语言中的文件处理函数,将数据存入.txt文件中
//
//
SLtour File_Initial_list(int n) //创建与文本文件需要的景点节点
{
tour *p,*q;
int i,j=0;
stuhead=p=(tour*)malloc(sizeof(tour)); //头节点处不存储数据
for(i=1;i<=n;i++)
{
q=(tour*)malloc(sizeof(tour));//动态分配内存
if(q != NULL)
{
j++;
}
p->Next=q;
p=q;
}
p->Next=NULL;
printf("成功创建%d个景点\n",j);
return stuhead;
}
int count_file_read() //计数文本文件中有多少景点
{
FILE *fp;
int n=0;
fp=fopen("E:\\旅游推荐系统\\旅游景点数据.txt","r+");
if(fp == NULL)
{
printf("打开文件失败\n");
return 0;
}
tour p;
while (fscanf(fp,"%s\t%s\t%s\t%d\t%d\t%s\n",p.province,p.city,p.scenic_spot,&p.tickets,&p.cost,p.description) != EOF)
{
n++;
}
fclose(fp);
return n;
}
SLtour Fil_read_list(SLtour stuhead) //将文本中的数据读入到链表中 //先进行链表的创建 File_Initial_list(i)
{
tour *p=stuhead->Next;
FILE *fp;
fp=fopen("E:\\旅游推荐系统\\旅游景点数据.txt","r+");
if(fp == NULL)
{
printf("打开文件失败\n");
return stuhead;
}
while (fscanf(fp,"%s\t%s\t%s\t%d\t%d\t%s\n",p->province,p->city,p->scenic_spot,&p->tickets,&p->cost,p->description) != EOF && p != NULL)
{
p=p->Next;
}
fclose(fp);
return stuhead;
}
//现在需要进行将链表和文本文件进行连接起来使用——该函数放在链表和文件操作之后,进行调用
//只有创建好该函数才能将所有的功能进行操作
SLtour List_And_File_cat(SLtour stuhead) //该函数要放在链表操作之前
{
SLtour head=stuhead;
int i=count_file_read(); //算好的链表所需空间
head=File_Initial_list(i); //动态分配大量节点
head=Fil_read_list(head); //将文本中的数据读入到链表中
stuhead=head;
return stuhead;
}