movie.c
int movie_count=0;
Film *head;
int main(){
void CreateLink();
int menu();
CreateLink();
menu();
}
int menu(){
void ShowAllMovies();
void AddMovie();
void SearchMovie();
void ModifyMovie();
void DeleteMovie();
char choice;
printf("\n\n");
printf("\t\t------------------------------------------------\n");
printf("\t\t| 欢迎使用影片信息管理系统 |\n");
printf("\t\t------------------------------------------------\n");
printf("\t\t| 1-录入影片信息 |\n");
printf("\t\t| 2-查询影片信息 |\n");
printf("\t\t| 3-修改影片信息 |\n");
printf("\t\t| 4-删除影片信息 |\n");
printf("\t\t| 5-显示所有影片 |\n");
printf("\t\t| 6- 影片总数 |\n");
printf("\t\t| 7- 退出程序 |\n");
printf("\t\t------------------------------------------------\n");
printf("请选择功能1~7: ");
choice=getchar();
switch (choice){
case '1':
AddMovie();
break;
case '2':
SearchMovie();
printf("\n\n》》按任意键回到菜单》》\n");
getch();
system("cls");
menu();
break;
case '3':
ModifyMovie();
printf("\n\n》》按任意键回到菜单》》\n");
getch();
system("cls");
menu();
break;
case '4':
DeleteMovie();
printf("\n\n》》按任意键回到菜单》》\n");
getch();
system("cls");
menu();
break;
case '5':
ShowAllMovies();
break;
case '6':
printf("\t\t 本地记录共%d部影片\n》》按任意键回到菜单》》\n",movie_count);
getchar();
getch();
system("cls");
menu();
break;
case '7':
printf("\t\t退出成功\n");
getchar();
return 0;
default:
system("cls");
menu();
}
return 0;
}
void CreateLink(){
Film *pnew,*q;
FILE *movies_info = fopen(".\\persistence\\movies.txt","r");
if(movies_info==NULL){
printf("读取本地文件失败!");
exit(0);
}
head = (Film*)malloc(sizeof(Film));
head->next=NULL;
q=head;
fscanf(movies_info,"%d\n",&movie_count);
for(int i=0;iif(i==0){
pnew = (Film*)malloc(sizeof(Film));
fscanf(movies_info,"%4d",&pnew->id);
fgets(pnew->name,21,movies_info);
fgets(pnew->director,21,movies_info);
fgets(pnew->actor,21,movies_info);
fgets(pnew->date,21,movies_info);
fscanf(movies_info,"%f\n",&pnew->score);
head->next=pnew;
q=pnew;
}
else
{
pnew = (Film*)malloc(sizeof(Film));
fscanf(movies_info,"%4d",&pnew->id);
fgets(pnew->name,21,movies_info);
fgets(pnew->director,21,movies_info);
fgets(pnew->actor,21,movies_info);
fgets(pnew->date,21,movies_info);
fscanf(movies_info,"%f\n",&pnew->score);
q->next=pnew;
q=pnew;
}
}
q->next=NULL;
fclose(movies_info);
}
void AddMovie(){
Film *pnew,*q;
FILE *movies_info = fopen(".\\persistence\\movies.txt","w");
if(movies_info==NULL){
printf(" 读取本地文件失败!");
exit(0);
}
pnew = (Film*)malloc(sizeof(Film));
printf("\n\n\t请输入编号:");
scanf("%d",&pnew->id);
printf("\n\n\t请输入影片名:");
getchar();
gets(pnew->name);
printf("\n\n\t请输入导演名:");
gets(pnew->director);
printf("\n\n\t请输入主演名:");
gets(pnew->actor);
printf("\n\n\t请输入上映日期:");
gets(pnew->date);
printf("\n\n\t请输入评分:");
scanf("%f",&pnew->score);
q=head;
movie_count++;
while(q->next!=NULL)q=q->next;
q->next=pnew;
q=pnew;
q->next=NULL;
fprintf(movies_info,"%d\n",movie_count);
q=head->next;
while(q!=NULL){
fprintf(movies_info,"%04d%-20s%-20s%-20s%-20s%.1f\n",q->id,q->name,q->director,q->actor,q->date,q->score);
q=q->next;
}
fclose(movies_info);
getchar();
printf("\n\n 》》按任意键回到菜单》》\n");
getch();
system("cls");
menu();
}
void SearchMovie(){
printf("请输入要查询影片的编号: ");
int search_num;
scanf("%d",&search_num);
Film *p=head;
while(p&&(p->id!=search_num)){//要注意逻辑运算符的短路现象
p=p->next;
}
if(p!=NULL){
printf("=========搜索成功!=========\n\n 编号:%-04d\n 名称:%s\n 导演:%s\n 主演:%s\n 上映日期:%s\n 评分:%.1f\n",p->id,p->name,p->director,p->actor,p->date,p->score);
getchar();
}
else{
printf("\t没有找到编号为%04d的影片信息,请确认输入的编号正确!\n",search_num);
getchar();
}
}
void ModifyMovie(){
printf("请输入要修改影片的编号: ");
int search_num;
scanf("%d",&search_num);
char choice;
printf("确定要修改编号为%04d的影片信息吗? (Y/N) ",search_num);
getchar();
choice = getchar();
if(choice!='Y'&&choice!='y')return;
Film *p=head;
while(p&&(p->id!=search_num)){
p=p->next;
}
if(p!=NULL){
printf("=========搜索成功!=========\n\n");
printf("\n\n\t将编号(%04d)更改为:",p->id);
scanf("%d",&p->id);
printf("\n\n\t将影片名(%s)更改为:",p->name);
getchar();
gets(p->name);
printf("\n\n\t将导演名(%s)更改为:",p->director);
gets(p->director);
printf("\n\n\t将主演名(%s)更改为:",p->actor);
gets(p->actor);
printf("\n\n\t将上映日期(%s)更改为:",p->date);
gets(p->date);
printf("\n\n\t将评分(%.1f)更改为:",p->score);
scanf("%f",&p->score);
FILE *movies_info = fopen(".\\persistence\\movies.txt","w");
if(movies_info==NULL){
printf(" 写入本地文件失败!");
exit(0);
}
fprintf(movies_info,"%d\n",movie_count);
p=head->next;
while(p!=NULL){
fprintf(movies_info,"%04d%-20s%-20s%-20s%-20s%.1f\n",p->id,p->name,p->director,p->actor,p->date,p->score);
p=p->next;
}
fclose(movies_info);
printf("=========成功修改了编号为%04d的影片信息!=========");
}
else{
printf("\t没有找到编号为%04d的影片信息,请确认输入的编号正确!\n",search_num);
getchar();
}
}
void DeleteMovie(){
printf("请输入要删除影片的编号: ");
int search_num;
scanf("%d",&search_num);
char choice;
printf("确定要删除编号为%04d的影片信息吗? (Y/N) ",search_num);
getchar();
choice = getchar();
if(choice!='Y'&&choice!='y')return;
Film *p=head,*q;
while(p->next&&p->next->id!=search_num){
p=p->next;
}
if(p->next){
q=p->next;
p->next=p->next->next;
free(q);
printf("删除成功!\n");
movie_count--;
FILE *movies_info = fopen(".\\persistence\\movies.txt","w");
if(movies_info==NULL){
printf(" 写入本地文件失败!");
exit(0);
}
fprintf(movies_info,"%d\n",movie_count);
p=head->next;
while(p!=NULL){
fprintf(movies_info,"%04d%-20s%-20s%-20s%-20s%.1f\n",p->id,p->name,p->director,p->actor,p->date,p->score);
p=p->next;
}
fclose(movies_info);
}
else{
printf("\t没有找到编号为%04d的影片信息,请确认输入的编号正确!\n",search_num);
getchar();
}
}
void ShowAllMovies(){
if(movie_count==0){
printf("\t本地没有储存的影片信息!请添加影片信息!\n");
getchar();
printf("\n\n 》》按任意键回到菜单》》\n");
getch();
system("cls");
menu();
}
else{
Film *p;
p=head->next;
printf("编号 名称 导演 主演 上映日期 评分\n");
while(p!=NULL){
printf("%04d %-20s %-20s %-20s %-20s%-.1f\n",p->id,p->name,p->director,p->actor,p->date,p->score);
p=p->next;
}
getchar();
printf("\n 》》按任意键回到菜单》》\n");
getch();
system("cls");
menu();
}
}
movie.h
#ifndef __MOVIE_H__
#define __MOVIE_H__
typedef struct Movie
{
int id;
char name[30];
char director[30];
char actor[30];
char date[30];
float score;
struct Movie *next;
}Film;
#endif