欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

第 14 章 结构和其他数据形式(names)

程序员文章站 2022-03-14 14:10:08
1 * 2 names1.c -- 使用指向结构的指针 3 */ 4 5 #include 6 #include 7 8 #define NLEN 30 9 10 struct namect 11 { 12 char fname[NLEN]; 13 char ......
第 14 章 结构和其他数据形式(names)
 1 *---------------------------------
 2     names1.c -- 使用指向结构的指针
 3 ---------------------------------*/
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 
 8 #define NLEN 30
 9 
10 struct namect
11 {
12     char fname[NLEN];
13     char lname[NLEN];
14     int letters;
15 };
16 
17 void getinfo(struct namect *);
18 void makeinfo(struct namect *);
19 void showinfo(const struct namect *);
20 char* s_gets(char *st, int n);
21 
22 int main()
23 {
24     struct namect person;
25 
26     getinfo(&person);
27     makeinfo(&person);
28     showinfo(&person);
29 
30     return 0;
31 }
32 
33 void getinfo(struct namect *pst)
34 {
35     printf("Please enter your first name.\n");
36 
37     s_gets(pst->fname, NLEN);
38 
39     printf("Please enter your last name.\n");
40 
41     s_gets(pst->lname, NLEN);
42 }
43 
44 void makeinfo(struct namect *pst)
45 {
46     pst->letters = strlen(pst->fname) + strlen(pst->lname);
47 }
48 
49 void showinfo(const struct namect *pst)
50 {
51     printf("%s %s, your name contains %d letters.\n"
52         , pst->fname, pst->lname, pst->letters);
53 }
54 
55 char* s_gets(char *st, int n)
56 {
57     char *ret_val;
58     char *find;
59 
60     if (ret_val = fgets(st, n, stdin))
61     {
62         if (find = strchr(st, '\n'))            //查找换行符
63             *find = '\0';
64         else
65             while (getchar() != '\n') continue;    //处理输入行的剩余字符
66     }
67 
68     return ret_val;
69 }
names1.c
第 14 章 结构和其他数据形式(names)
 1 /*------------------------------
 2     names2.c -- 传递并返回结构
 3 ------------------------------*/
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 
 8 #define NLEN 30
 9 
10 struct namect
11 {
12     char fname[NLEN];
13     char lname[NLEN];
14     int letters;
15 };
16 
17 struct namect getinfo(void);
18 struct namect makeinfo(struct namect);
19 void showinfo(struct namect);
20 char* s_gets(char *st, int n);
21 
22 int main()
23 {
24     struct namect person;
25 
26     person = getinfo();
27     person = makeinfo(person);
28     showinfo(person);
29 
30     return 0;
31 }
32 
33 struct namect getinfo(void)
34 {
35     struct namect temp;
36 
37     printf("Please enter your first name.\n");
38 
39     s_gets(temp.fname, NLEN);
40 
41     printf("Please enter your last name.\n");
42 
43     s_gets(temp.lname, NLEN);
44 
45     return temp;
46 }
47 
48 struct namect makeinfo(struct namect info)
49 {
50     info.letters = strlen(info.fname) + strlen(info.lname);
51     return info;
52 }
53 
54 void showinfo(struct namect info)
55 {
56     printf("%s %s, your name contains %d letters.\n"
57         , info.fname, info.lname, info.letters);
58 }
59 
60 char* s_gets(char *st, int n)
61 {
62     char *ret_val;
63     char *find;
64 
65     if (ret_val = fgets(st, n, stdin))
66     {
67         if (find = strchr(st, '\n'))    //查找换行符,如果找到换行符,将换行符成空字符
68             *find = '\0';
69         else
70             while (getchar() != '\n') continue;    //处理输入行的剩余部分
71     }
72 
73     return ret_val;
74 }
names2.c

第 14 章 结构和其他数据形式(names)