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

2018-06-18 03:46:07来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 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
 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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:c复杂函数指针

下一篇:[日常] 算法-单链表的创建-尾插法