|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想问问其中的s_gets函数是来干什么的 还有我的第一个输入无效和怎么退不出循环了。。。。拜托了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TSIZE 45
struct film {
char title[TSIZE];
int rating;
struct film *next ;
};
char *s_gets(char * st , int n);
int main(void)
{
struct film *head = NULL ;
struct film *prev = NULL , *current;
char input[TSIZE];
//收集并采集数据
puts("Enter first movie title :");
while (s_gets(input , TSIZE) != 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 you next movies title (empty line to exit):");
prev = current;
}
// 显示电影列表
if (head == NULL)
printf("No data entered . ");
else
printf("Here is the movies list :");
current = head ;
while (current != NULL)
{
printf("Movies: %s Rating: %d \n",current ->title ,current ->rating);
current -> next = current ;
};
current = head ;
while (current != NULL)
{
free (current);
head = current ->next;
}
printf("Bye\n");
return 0;
}
char *s_gets(char * st , int n)
{
char * ret_val ;
char * find;
ret_val = fgets(st, n, stdin);
if(ret_val)
{
find = strchr(st, '\n');
if(find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
} |
|