-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
47 lines (40 loc) · 1 KB
/
book.cpp
File metadata and controls
47 lines (40 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <string.h>
#define MAXTITL 41
#define MAXAUTL 31
struct book { /*结构模板,标记是 book */
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
char * s_gets(char *, int);
int main()
{
struct book library; /* 把 library 声明为一个 book 类型的变量*/
printf("请输入书的标题:\n");
s_gets(library.title, MAXTITL);
printf("现在输入书的作者姓名:\n");
s_gets(library.author, MAXAUTL);
printf("现在输入书本的价格:\n");
scanf("%f", &library.value);
printf("%s by %s: $%.2f\n", library.title, library.author, library.value);
printf("%s: \"%s\"($%.2f)\n", library.author, library.title, library.value);
printf("Done.\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) //如果地址不是NULL
*find = '\0'; //在此放置一个空字符
else
while (getchar() != '\n')
continue;
}
return ret_val;
}