C语言结构体+公用体+枚举训练

结构体大小和内存结构

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 //结构体需要根据数据类型进行内存对齐
 7 //struct stus
 8 //{
 9 //    char name[20];//20
10 //    unsigned int age;//4
11 //    char tel[15];//15
12 //    char sex;//1  52
13 //    float scores[3];//12
14 //}stu;
15 
16 struct stus
17 {
18     //char * p;   //4
19     //char arr[2];//2  8
20     //short d; //2   16
21     //int c;  //4 
22     //long g;//4    
23     //double f;//8  24
24     //float h[2];//8   40
25 
26 
27     double f;//8  24
28     float h[2];//8   40
29     long g;//4    
30     int c;  //4 
31     char * p;   //4
32     short d; //2   16
33     char arr[2];//2  8
34 
35 
36     /*
37     姓名:  char name[200];
38     等级: int 
39     当前经验:int 
40     攻击: int 
41     防御:int 
42     技能冷却:foat 
43 
44     */
45 
46 
47 }stu;
48 
49 
50 int main()
51 {
52     
53     printf("结构体大小%d\n", sizeof(stu));
54 
55     system("pause");
56     return EXIT_SUCCESS;
57 }

结构体学生成绩排序

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stu1
 7 {
 8     //成员列表
 9     char name[21];
10     float scores[3];
11 };
12 int main()
13 {
14     struct stu1 s[3];
15     for (int i = 0; i < 3; i++)
16     {
17         printf("请您输入学生 姓名   成绩 :\n");
18         scanf("%s%f%f%f", s[i].name, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2]);
19     }
20     //冒泡排序
21     for (int i = 0; i < 3 - 1; i++)
22     {
23         for (int j = 0; j < 3 - i - 1; j++)
24         {
25 
26             int sum1 = s[j].scores[0] + s[j].scores[1] + s[j].scores[2];
27             int sum2 = s[j + 1].scores[0] + s[j + 1].scores[1] + s[j + 1].scores[2];
28             if (sum1 > sum2)
29             {
30                 //结构体交换   交换所有成员列表中的数据
31                 //交换姓名
32                 char temp[21] = { 0 };
33                 strcpy(temp, s[j].name);
34                 strcpy(s[j].name, s[j + 1].name);
35                 strcpy(s[j + 1].name, temp);
36 
37                 //交换成绩
38 
39                 for (int k = 0; k < 3; k++)
40                 {
41                     float temp = s[j].scores[k];
42                     s[j].scores[k] = s[j + 1].scores[k];
43                     s[j + 1].scores[k] = temp;
44                 }
45 
46             }
47         }
48     }
49 
50 
51 
52 
53     for (int i = 0; i < 3; i++)
54     {
55         printf("姓名:%s\n", s[i].name);
56         printf("成绩: %.1f   %.1f   %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
57     }
58 
59 
60     system("pause");
61     return EXIT_SUCCESS;
62 }

学生成绩优化

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stu1
 7 {
 8     //成员列表
 9     char name[21];
10     float scores[3];
11 };
12 int main()
13 {
14     struct stu1 s[3];
15     for (int i = 0; i < 3; i++)
16     {
17         printf("请您输入学生 姓名   成绩 :\n");
18         scanf("%s%f%f%f", s[i].name, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2]);
19     }
20     //冒泡排序
21     for (int i = 0; i < 3 - 1; i++)
22     {
23         for (int j = 0; j < 3 - i - 1; j++)
24         {
25 
26             int sum1 = s[j].scores[0] + s[j].scores[1] + s[j].scores[2];
27             int sum2 = s[j + 1].scores[0] + s[j + 1].scores[1] + s[j + 1].scores[2];
28             if (sum1 > sum2)
29             {
30                 //结构体交换   交换所有成员列表中的数据
31                 ////交换姓名
32                 //char temp[21] = { 0 };
33                 //strcpy(temp, s[j].name);
34                 //strcpy(s[j].name, s[j + 1].name);
35                 //strcpy(s[j + 1].name, temp);
36 
37                 ////交换成绩
38 
39                 //for (int k = 0; k < 3; k++)
40                 //{
41                 //    float temp=s[j].scores[k];
42                 //    s[j].scores[k] = s[j + 1].scores[k];
43                 //    s[j + 1].scores[k] = temp;
44                 //}
45 
46                 //结构体变量交换
47                 struct stu1 temp = s[j];
48                 s[j] = s[j + 1];
49                 s[j + 1] = temp;
50 
51             }
52         }
53     }
54 
55 
56 
57 
58     for (int i = 0; i < 3; i++)
59     {
60         printf("姓名:%s\n", s[i].name);
61         printf("成绩: %.1f   %.1f   %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
62     }
63 
64 
65     system("pause");
66     return EXIT_SUCCESS;
67 }

结构体成员为指针

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stuinfo
 7 {
 8     char *name;
 9     int age;
10 };
11 
12 int main()
13 {
14     struct  stuinfo si;
15     si.name = (char *)malloc(sizeof(char) * 21);
16 
17     strcpy(si.name, "张三");
18     si.age = 18;
19 
20     printf("%s   %d\n", si.name, si.age);
21 
22     free(si.name);
23 
24 
25     system("pause");
26     return EXIT_SUCCESS;
27 }

结构体指针

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct sinfo
 7 {
 8     char *name;
 9     int age;
10 }stu;
11 int main()
12 {
13     struct sinfo * s = &stu;
14     s->name = (char *)malloc(sizeof(char) * 21);
15     strcpy(s->name, "李芮");
16     s->age = 50;
17     printf("%s   %d\n", s->name, s->age);
18 
19     free(s->name);
20 
21 
22 
23     system("pause");
24     return EXIT_SUCCESS;
25 }

 

 

 

堆空间开辟结构体

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct tec
 7 {
 8     char *name;//4
 9     int age;//4
10 }t;
11 
12 int main()
13 {
14     struct tec * p = (struct tec *)malloc(sizeof(t));
15     p->name = (char *)malloc(sizeof(char) * 21);
16 
17     strcpy(p->name, "牛玲");
18     p->age = 18;
19 
20     printf("%s   %d\n", p->name, p->age);
21 
22     if (p->name != NULL)
23     {
24         free(p->name);
25         p->name = NULL;
26     }
27 
28     if (p)
29     {
30         free(p);
31         p = NULL;
32     }
33 
34 
35     printf("%d\n", sizeof(struct tec *));//无论为什么类型的指针,都是4个
36 
37     system("pause");
38     return EXIT_SUCCESS;
39 }

 

 

 学生成绩

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 struct stu2
 6 {
 7     //成员列表
 8     //char name[21];
 9     char * name;
10     float * scores;
11 };
12 
13 int main()
14 {
15     struct stu2 *p = (struct stu2 *)malloc(sizeof(struct stu2) * 3);
16     for (int i = 0; i < 3; i++)
17     {
18         p[i].name = (char *)malloc(sizeof(char) * 21);
19         p[i].scores = (float *)malloc(sizeof(float) * 3);
20 
21         printf("请您输入学生 姓名   成绩 :\n");
22         scanf("%s%f%f%f", p[i].name, &p[i].scores[0], &p[i].scores[1], &p[i].scores[2]);
23 
24     }
25 
26     //冒泡排序
27     for (int i = 0; i < 3 - 1; i++)
28     {
29         for (int j = 0; j < 3 - i - 1; j++)
30         {
31             float sum1 = p[j].scores[0] + p[j].scores[1] + p[j].scores[2];
32             float sum2 = p[j + 1].scores[0] + p[j + 1].scores[1] + p[j + 1].scores[2];
33             if (sum1 > sum2)
34             {
35                 struct stu2 temp = p[j];
36                 p[j] = p[j + 1];
37                 p[j + 1] = temp;
38             }
39         }
40     }
41 
42 
43     for (int i = 0; i < 3; i++)
44     {
45 
46         printf("姓名:%s\n", p[i].name);
47         printf("成绩: %.1f   %.1f   %.1f\n", p[i].scores[0], p[i].scores[1], p[i].scores[2]);
48     }
49 
50 
51     //释放
52     for (int i = 0; i < 3; i++)
53     {
54         free(p[i].name);
55         free(p[i].scores);
56     }
57 
58     free(p);
59 
60 
61 
62     system("pause");
63     return EXIT_SUCCESS;
64 }

结构体和函数

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct info
 7 {
 8     char name[21];
 9     int age;
10 };
11 
12 void fun01(struct info s)
13 {
14     strcpy(s.name, "李四");
15     s.age = 20;
16 
17     printf("%s   %d\n", s.name, s.age);
18 
19 }
20 void fun02(struct info *s)
21 {
22     strcpy(s->name, "李四");
23     s->age = 20;
24 
25 }
26 
27 
28 
29 int main1()
30 {
31     struct info s = { "张三",18 };
32 
33     //fun01(s);
34     fun02(&s);
35 
36     printf("%s   %d\n", s.name, s.age);
37 
38     system("pause");
39     return EXIT_SUCCESS;
40 }
41 struct info  fun03()
42 {
43     struct info s;
44     strcpy(s.name, "李四");
45     s.age = 20;
46 
47     return s;
48 
49 }
50 struct info * fun04()
51 {
52     struct info s;
53     strcpy(s.name, "李四");
54     s.age = 20;
55 
56     return &s;
57 }
58 
59 //int fun04()
60 //{
61 //    int a = 10;
62 //    return a;
63 //}
64 int main()
65 {
66     struct info s = fun03();
67     printf("%s   %d\n", s.name, s.age);
68 
69 
70     //int a = fun04();
71 
72 
73     //struct info * s = fun04();
74     //printf("%s   %d\n", s->name, s->age);
75     system("pause");
76     return 0;
77 
78 }

 结构体嵌套结构体

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 struct stra
 7 {
 8     int a;  //4   4
 9     float b;//4   8
10     char c;//1   12  9
11     char arr[7];   //16
12     double h;//24
13 }abc;
14 
15 struct  strb
16 {
17     struct stra abc;//12  16
18     short f;  //2
19     char * e;  //4
20     short g;
21     //double d; //8
22 };
23 
24 
25 /*
26     技能cd:
27     skill01 10
28     skill02 3
29     skill03 7
30 
31 */
32 /*
33 
34     名称
35     等级
36     攻击
37     技能:
38 
39 
40 */
41 int main()
42 {
43     struct strb  strbb;
44     //strbb.d = 10.0f;
45     //strbb.abc.a = 100;
46 
47 
48     //printf("%d\n", strbb.abc.a);
49 
50     printf("%d\n", sizeof(strbb));
51 
52 
53     system("pause");
54     return EXIT_SUCCESS;
55 }

共用体

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 //共用体   union 共用体名称  成员列表  共用体变量名
 7 union vars
 8 {
 9     double a;
10     float b;
11     int c;
12     short d;
13     char f;
14     char arr[17];//17
15 }var;
16 int main()
17 {
18     printf("%d\n", sizeof(var));
19     var.a = 100;
20     var.b = 3.14;
21     var.c = 66;
22     printf("%f\n", var.a);
23     printf("%f\n", var.b);
24     printf("%d\n", var.c);
25 
26     system("pause");
27     return EXIT_SUCCESS;
28 }

 

枚举

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 enum colors
 7 {
 8     red = 10, bule, yellow = 20, black, white, green
 9 }clo;
10 /*
11 插卡
12 输入密码
13 锁定
14 取款
15 查询
16 退卡
17 锁定解除
18 
19 */
20 /*
21 10
22 移动
23 攻击
24 技能
25 死亡
26 20
27 亮牌子
28 跳舞
29 
30 
31 */
32 int main()
33 {
34     clo = 0;
35 
36     int val;
37     scanf("%d", &val);
38     switch (val)
39     {
40     case red:
41         break;
42     case bule:
43         break;
44     case yellow:
45         break;
46     case black:
47         break;
48     case white:
49         break;
50     case green:
51         break;
52     default:
53         break;
54     }
55     //switch (val)
56     //{
57     //case red:
58     //    printf("请输入您的密码");
59     //    clo = 1;
60     //    printf("红色\n");
61     //    break;
62     //case bule:
63     //    printf("");
64     //    clo = 2;
65     //    clo = 3;
66     //    clo = 4;
67 
68     //    printf("蓝色\n");
69 
70     //    break;
71     //case yellow:
72     //    printf("黄色\n");
73 
74     //    break;
75     //case black:
76     //    break;
77     //case white:
78     //    break;
79     //case green:
80     //    break;
81     //default:
82     //    break;
83     //}
84 
85     system("pause");
86     return EXIT_SUCCESS;
87 }

 

 

转载于:https://www.cnblogs.com/WangGuiHandsome/p/9899005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值