Write a program that will read data about n movies (data about a movie is defined in structure).
Sort data about movies (criteria of sorting is by title of a movie and by surname and name of an actor).
Read data about one actor, and print data about all movies that contain read actor.
How to implement function for printing data about all movies that contain one read actor?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
typedef struct
{
char name[MAX];
char surname[MAX];
}ACTOR;
typedef struct
{
char title[MAX];
int length,year;
int numberOfActors;
ACTOR *actors;
}MOVIE;
ACTOR readActor();
MOVIE* readMovies(int *n);
void sort(MOVIE *arr,int n);
void print(MOVIE *arr,int n, ACTOR *act);
ACTOR readActor()//read data about one actor
{
ACTOR act;
printf("Name: ");
scanf("%s",act.name);
printf("Surname: ");
scanf("%s",act.surname);
return act;
}
MOVIE* readMovies(int *n)//read data about movies with actors
{
MOVIE *me;
do
{
printf("n=");
scanf("%d",n);
}
while(*n < 1);
me=(MOVIE*)malloc(*n * sizeof(MOVIE));
me->actors=(ACTOR*)malloc(me->numberOfActors * sizeof(ACTOR));
int i,j;
for(i = 0; i < *n; i++)
{
printf("%d. movie: \n",i+1);
fflush(stdin);
printf("Title: ");
gets(me->title);
printf("Year: ");
scanf("%d",&me->year);
printf("Length: ");
scanf("%d",&me->length);
printf("Number of actors: ");
scanf("%d",&me->numberOfActors);
for(j = 0; j < me->numberOfActors; j++)
{
printf("%d. actor: \n",j+1);
me->actors[j]=readActor();
}
}
return me;
}
void sort(MOVIE *arr,int n)//sort data by title, surname and name
{
int i,j,u,v;
for(i = 0; i < n-1; i++)
{
for(j = i+1; j < n; j++)
{
if(strcmp(arr[i].title,arr[j].title) > 0)//compare movie title
{
MOVIE temp1=arr[i];
arr[i]=arr[j];
arr[j]=temp1;
}
for(u = 0; u < arr->numberOfActors - 1; u++)
{
for(v = u+1; v < arr->numberOfActors; v++)
{
if(strcmp(arr->actors[i].surname,arr->actors[j].surname) > 0)//if surname of actors are different
{
ACTOR temp2=arr->actors[i];
arr->actors[i]=arr->actors[j];
arr->actors[j]=temp2;
}
else if((strcmp(arr->actors[i].surname,arr->actors[j].surname) == 0)//if surnames are the same, sort by name
&& (strcmp(arr->actors[i].name,arr->actors[j].name) > 0))
{
ACTOR temp2=arr->actors[i];
arr->actors[i]=arr->actors[j];
arr->actors[j]=temp2;
}
else if((strcmp(arr->actors[i].surname,arr->actors[j].surname) == 0)//break if name and surname are the same
&& (strcmp(arr->actors[i].name,arr->actors[j].name) == 0))
break;
}
}
}
}
}
void print(MOVIE *arr,int n, ACTOR *act)//print movies with specific actor
{
//...//
}
int main()
{
MOVIE *me;
int n;
me=readMovies(&n);
MOVIE *arr;
arr=(MOVIE*)malloc(n * sizeof(MOVIE));
sort(arr,n);
return 0;
}