#include
#define TSIZE 45
#define FMAX 5
struct film{
char title[TSIZE];
int rating;
};
int main()
{
struct film movies[FMAX];
int i = 0;
int j ;
puts("enter first movie title ");
while (i< FMAX && gets(movies[i].title)!= NULL && movies[i].title[0] != '\0') {
puts("Enter your rating 0-10");
scanf("%d",&movies[i++].rating);
while (getchar() != '\n') {
continue;
}
puts("enter next movie ttile (empty line to stop ");
}
if (i == 0) {
printf("no data entered ");
}
else{
printf("here is the movie list \n");
}
for (j = 0 ; j < i ; j ++) {
printf("Movie : %s rating : %d \n",movies[j].title,movies[j].rating);
}
printf("Bye");
}
链表
#include
#include
#include
#define TSIZE 45
struct film{
char title[TSIZE];
int rating;
struct film * next;
};
int main()
{
struct film *prev = NULL , *current;
struct film *head = NULL;
char input[TSIZE];
puts("enter first movie title ");
while ( gets(input) != NULL && input[0] != '\0' ) {
current = (struct film * )malloc(sizeof(struct film ));
if (head == NULL) {
head = current ;
}
else
prev ->next = current;
current ->next = NULL;
strcpy(current->title, input);
puts("enter your rating <0 - 10>");
scanf("%d",¤t -> rating );
while (getchar() != '\n')
{
continue;
}
puts("enter netx moive title (empty line to stop ");
prev = current;
}
if (head == NULL)
{
printf("no data enterd ");
}
else{
printf("here is the movie list \n");
}
current = head;
while (current != NULL)
{
printf("move %s rating :%d \n", current ->title , current-> rating );
current = current ->next;
}
current = head;
while (current != NULL) {
free(current);
current = current->next;
}
}