我是编程的新手,因为我开始学习计算机科学。 我的任务是使用C语言编写程序。 我正在努力,但是我最重要的功能(insert_a_new_movie)遇到了问题。 更具体地说,当我编译并插入信息时,它会在最后一次插入(年)时停止并返回状态= 0 ...
****任务说明**** 您被要求用C语言实现一个程序 将模拟电影管理信息系统的操作 视频俱乐部。 更具体地说,您的程序应提供以下功能:
- 插入新电影
- 按ID删除电影
- 按标题搜索电影
- 根据导演姓氏搜索电影
- 描述电影的信息将存储在文本文件(.txt)中
- 文本文件将在程序开始和录音集开始时读取
- 包含的内容将上传到电影动态数组。
- 您必须使用结构(使用typedef)。
//我必须使用的结构,可以在下面的代码中找到它们。 影片代码(id)应由程序自动计算并给出。
新插入将存储在表的末尾。
用户应该能够重复选择上述操作之一 直到他输入数字5为止,程序将结束。
在这种情况下,保存电影信息的文件应被注销 覆盖,以便在输入或删除记录时 保留用户所做的更改。 我现在要写的代码
#include <stdio.h
#include <stdlib.h
#include <string.h>
#define SIZE 100
typedef struct date
{
int day,month,year;
}date;
typedef struct director_info
{
char director_name[SIZE];
char director_surname[SIZE];
}director_info;
typedef struct movie
{
int id;
char title[SIZE];
director_info *director;
date *release_date;
}movie;
typedef struct movies
{
movie *array;
int capacity;
int size;
}movies;
int menu();
void insert_a_new_movie(movies *mo);
void save_movie_to_a_file(movies *mo,char *filename);
void load_movies_from_file(movies *mo,char *filename);
int main(int argc,char *argv[])
{
int choice,pos;
movies mo;
char filename[SIZE];
int counter=0;
choice=menu();
while(choice)
{
if(choice==1)
{
//1.insert a new movie.--->Check first time or load a file.
insert_a_new_movie(&mo);
}
else if(choice==2)
{
//2.delete a movie based on movies' id.
}
else if(choice==3)
{
//3.search a movie based on the title.
}
else if(choice==4)
{
//4.search a movie based on surname of the director.
}
else if(choice==5)
{
//5.exit --->While exiting ,program should be saved on a file.
printf("Before exiting make sure you will save the file.\n");
printf("FILENAME:");
scanf("%s",filename);
save_movie_to_a_file(&mo,filename);
printf("\nMOVIE(-S) SAVED!\n");
return 0;
}
choice=menu();
}
return 0;
}
int menu()
{
int choice;
printf("************* VIDEO CLUB ****************\n");
printf("1. INSERT A NEW MOVIE.\n");
printf("2. DELETE A MOVIE BY GIVING ID.\n");
printf("3. SEARCH A MOVIE BY GIVING THE TITLE.\n");
printf("4. SEARCH A MOVIE BY GIVING DIRECTOR'S SURNAME.\n");
printf("5. EXIT PROGRAM.\n");
scanf("%d",&choice);
return choice;
}
void insert_a_new_movie(movies *mo)
{
int counter=1;
movie m;
m.id=counter++;
printf("Please give information about the movie.\n");
printf("MOVIE'S ID : %d\n",m.id);
printf("TITLE: \n");
fflush(stdin);
scanf("%s",m.title);
printf("DIRECTOR'S SURNAME: \n");
fflush(stdin);
scanf("%s",m.director->director_surname);
printf("DIRECTOR'S NAME: \n");
fflush(stdin);
scanf("%s",m.director->director_name);
printf("RELEASE DAY: \n");
fflush(stdin);
scanf("%d",&m.release_date->day);
printf("RELEASE MONTH: \n");
fflush(stdin);
scanf("%d",&m.release_date->month);
printf("RELEASE YEAR: \n");
fflush(stdin);
scanf("%d",&m.release_date->year);
if(mo->size >= mo->capacity)
{
mo->capacity +=10;
mo->array=realloc(mo->array,sizeof(movie)* mo->capacity);
}
strcpy(mo->array[mo->size].title,m.title);
strcpy(mo->array[mo->size].director->director_surname,m.director->director_surname);
strcpy(mo->array[mo->size].director->director_name,m.director->director_name);
mo->array[mo->size].release_date->day=m.release_date->day;
mo->array[mo->size].release_date->month=m.release_date->month;
mo->array[mo->size].release_date->year=m.release_date->year;
mo->size++;
m.id-=counter++;
}
void save_movie_to_a_file(movies *mo,char *filename)
{
int i;
FILE *fp;
if((fp=fopen(filename,"w"))==NULL)
{
fprintf(stderr, "ERROR.\n");
return;
}
for(i=0;i<mo->size;i++)
{
fprintf(fp,"%s",mo->array[i].title);
fprintf(fp,"%s",mo->array[i].director->director_surname);
fprintf(fp,"%s",mo->array[i].director->director_name);
fprintf(fp,"%d",mo->array[i].release_date->day);
fprintf(fp,"%d",mo->array[i].release_date->month);
fprintf(fp,"%d",mo->array[i].release_date->year);
}
fclose(fp);
}
void load_movies_from_file(movies *mo,char *filename)
{
int i;
char line[100];
char *tok;
FILE *fp;
if ((fp=fopen(filename,"r"))==NULL)
{
fprintf(stderr,"FILE NOT FOUND.\n");
return;
}
while(!feof(fp))
{
fgets(line,100,fp);
tok=strtok(line,"\n\0");
if(mo->size >= mo->capacity)
{
mo->capacity +=10;
mo->array=realloc(mo->array,sizeof(movie)*mo->capacity);
}
if (tok != NULL)
{
strcpy(mo->array[mo->size].title,tok);
tok = strtok(NULL, " \n\0");
if (tok != NULL)
{
strcpy(mo->array[mo->size].director->director_surname,tok);
tok = strtok(NULL, " \n\0");
if (tok != NULL)
{
strcpy(mo->array[mo->size].director->director_name,tok);
tok = strtok(NULL, " \n\0");
if(tok!=NULL)
{
mo->array[mo->size].release_date->day = atoi(tok);
tok = strtok(NULL, " \n\0");
if(tok!=NULL)
{
mo->array[mo->size].release_date->month = atoi(tok);
tok = strtok(NULL, " \n\0");
if(tok!=NULL)
{
mo->array[mo->size].release_date->year = atoi(tok);
tok = strtok(NULL, " \n\0");
}
}
}
}
}
}
mo->size++;
}
fclose(fp);
}
因此,有人可以帮我发现插入函数(1.)发生了什么吗? 并且欢迎对解决方案提出任何意见。
非常感谢!