zoukankan      html  css  js  c++  java
  • C(十六)结构体

    一、结构体

      为什么需要结构体

        为了表示一些复杂的事物,而普通的基本类型变量无法满足实际要求

       什么叫结构体

        把一些基本类型数据组合在一起形成的一个新的复合数据类型,这个叫做结构体

      怎么使用结构体变量

        赋值和初始化

          定义的同时可以整体赋初值

          如果定义完之后,则只能单个赋初值

        如何取出结构体变量中的每一个成员(重点)

          1. 结构体变量名,成员名

          2. 指针变量名 -〉 成员名 (这种方式更常用)

              指针变量名 -〉 成员名  在计算机内部会被转化成(*指针变量名).成员名

              的方式来执行

              所以说这两种方式是等价的

       

        结构体变量和结构体指针变量作为函数参数传递的问题

            推荐使用结构体指针变量作为函数参数来传递

           结构体变量的运算

            结构体变量不能相加,不能想减,也不能相互乘除

            但结构体变量可以相互赋值

         举例:

          动态的构造存放学生信息的结构体数组

                       

      枚举

        什么是枚举
          把一个事物所有的可能的确值一一列举出来

        枚举的优缺点

          代码更安全

          书写麻烦

      补码

        原码

          也叫 符号-绝对值码

          最高位0表示正 1表示负,其余二进制位是该数字的绝对值的二进制位

           原码简单易懂

          加减运算复杂

          存在加减乘除四种运算,增加CPU的复杂度

          零的表示不唯一

        反码

          反码运算不便,也没有在计算机中使用

          

        移码

          移码表示数值平移n位,n称为移码量

          移码主要用于浮点数的阶码的存储

        补码

          已知十进制求二进制

            已知正整数转二进制

              除2取余,直至商为零,余数倒叙排序

            求负整数转二1进制

              先求与该负数相对应的正整数的二进制代码,然后将

              所有位取反,末尾加1,不够位数时,左边补1。

            求零转二进制

              全是零

          已知二进制求十进制

              如果首位是0,则表明是正整数,按普通方法来求

              如果首位是1,则表明是负整数,末尾加1,所得数字就是该负数的绝对值

          

        学习目标:

          在Vc++6.0中一个int类型的变量所能存储的数字的范围是多少

              int类型变量所能存储的 最大整数用十六进制表示是:7FFFFFFF

              int类型变量所能存储的绝对值最大的负整数用十六进制表示是:8000000

          绝对值最小负数的二进制代码是多少

          最大正数的二进制代码是多少

          已知一个整数的二进制代码求出原始的数字

          数字超过最大正数会怎样      会溢出

              

            

    二、结构体举例

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <malloc.h>
     4 
     5 struct Student
     6 {
     7     int age;
     8     float score;
     9     char sex;
    10 
    11 }
    12 
    13 int main(void)
    14 {
    15     struct Student st = {80,66.6,'F'};
    16     
    17     system("pause");
    18     return 0;
    19 }
    什么叫结构体
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 //第一种
     4 struct Student
     5 {
     6     int age;
     7     float score;
     8     char sex;
     9 }
    10 //第二种
    11 struct Student2
    12 {
    13     int age;
    14     float score;
    15     char sex;
    16 
    17 }st2:
    18 //第三种
    19 struct
    20 {
    21     int age;
    22     float score;
    23     char sex; 
    24 }
    25 
    26 int main()
    27 {
    28     
    29     system("pause");
    30     return 0;
    31 }
    32 //推荐使用第一种
    如何定义结构体
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 struct Student
     5 {
     6     int age;
     7     double score;
     8     char sex;
     9 };
    10 
    11 int main(void)
    12 {
    13     struct Student st = {80,66.6,'F'};//初始化  定义同时赋初值
    14     struct Student st2;
    15     st2.age =10;
    16     st2.score = 88.5;
    17     st2.sex = 'F';
    18     
    19     printf("%d %f %c
    ",st.age,st.score,st.sex);
    20     printf("%d %f %c
    ",st2.age,st2.score,st2.sex);
    21     system("pause");
    22     return 0;
    23 }
    结构体的赋值和初始化
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 // 这只是定义了一个新的数据类型,并没有定义变量
     4 struct Student
     5 {
     6     int age;
     7     double score;
     8     char sex;
     9 };
    10 
    11 int main(void)
    12 {
    13     struct Student st = {80,66.6,'F'};//初始化  定义同时赋初值
    14     
    15     struct Student * pst = &st; //&st不能改成st
    16     
    17     pst -> age = 88; //第二种方式
    18     st.score = 66.6;//第一种方式
    19     printf("%d %f
    ",st.age,pst->score);
    20     system("pause");
    21     return 0;
    22 }
    23 
    24 /*
    25     1. pst ->age 在计算机内部会被转化成(*pst).age,没有为什么,
    26        这就是->的含义,这也是一种硬性规定
    27     2. 所以pst->等价于(*pst).age 也等价于 st.age
    28     3. 我们之所以知道pst->age等价于 st.age,是因为pst->age是被转化成了
    29     (*pst).age执行
    30     4. pst->age的含义;
    31         pst所指向的那个结构体变量中的age这个成员
    32 */
    如何取出结构体变量中的每一个成员_1
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 struct Student
     5 {
     6     int age;
     7     int sex;
     8     char name[100];
     9 };//分号不能省
    10 
    11 int main(void)
    12 {
    13     struct Student st = {17,'M',"LTT"};
    14     struct Student  * pst = &st;
    15     
    16     printf("%d %c %s
    ",st.age,st.sex,st.name);
    17     printf("%d %c %s
    ",pst->age,pst->sex,pst->name);
    18     
    19     system("pause");
    20     return 0;
    21 }
    如何取出结构体变量中的每一个成员_2
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 
     6 
     7 struct Student
     8 {
     9     int age;
    10     int sex;
    11     char name[100];
    12 };//分号不能省
    13 void InputStudent(struct Student * );
    14 void OutputStudent(struct Student TT);
    15 
    16 int main(void)
    17 {
    18     struct Student st ;
    19   
    20     InputStudent(&st);//对结构体变量输入 必须发送st的地址
    21     OutputStudent(st);//对结构体变量输出 可以发送st的地址也可以直接发送st的内容
    22     
    23     system("pause");
    24     return 0;
    25 }
    26 
    27 void InputStudent(struct Student * pstu) //pstu只占4个字节
    28 {
    29     (*pstu).age = 10;
    30     strcpy(pstu->name,"张三");
    31     pstu->sex ='F';
    32 }
    33 
    34 void OutputStudent(struct Student TT)
    35 {
    36     printf("%d %c %s
    ",TT.age,TT.sex,TT.name);
    37 }
    通过函数完成对结构体的输入输出
     1 /*
     2     指针的优点之一;
     3         快速的传递数据
     4         耗用内存小
     5         快速的传递数据
     6         执行速度快
     7 */
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 #include <string.h>
    11 
    12 struct Student
    13 {
    14     int age;
    15     char sex;
    16     char name[100];
    17 };//分号不能省
    18 void InputStudent(struct Student * );
    19 void OutputStudent(struct Student *);
    20 
    21 int main(void)
    22 {
    23     struct Student st ;
    24     printf("%d
    ",sizeof(st));
    25     InputStudent(&st);//对结构体变量输入 必须发送st的地址
    26     OutputStudent(&st);//对结构体变量输出 可以发送st的地址也可以直接发送st的内容 推荐发送地址
    27     
    28     system("pause");
    29     return 0;
    30 }
    31 
    32 void InputStudent(struct Student * pstu) //pstu只占4个字节
    33 {
    34     (*pstu).age = 10;
    35     strcpy(pstu->name,"张三");
    36     pstu->sex ='F';
    37 }
    38 
    39 void OutputStudent(struct Student *pstu)
    40 {
    41     printf("%d %c %s
    ",pstu->age,pstu->sex,pstu->name);
    42 }
    应该发送内容还是应该发送地址
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 void sort(int * a, int len)
     5 {
     6     int i,j,t;
     7     for(i=0; i<len-1; ++i)
     8     {
     9         for(j=0; j<len-1-i; ++j)
    10         {
    11             if(a[j]>a[j+1]) // >表示升序 〈表示降序
    12             {
    13                 t = a[j];
    14                 a[j] = a[j+1];
    15                 a[j+1] = t;
    16             }
    17         }
    18     }
    19 }
    20 
    21 int main(void)
    22 {
    23     int a[6] = {-8,0,2,5,10,11};
    24     int i = 0;
    25     
    26     sort(&a,6);
    27     
    28     for(i=0; i<6; ++i)
    29     {
    30         printf("%d ",a[i]);
    31     }
    32     printf("
    ");
    33     
    34     system("pause");
    35     return 0;
    36 }
    冒泡排序
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <malloc.h>
     4 
     5 struct Student
     6 {
     7     int age;
     8     float score;
     9     char name[100];
    10 };
    11 
    12 
    13 int main(void)
    14 {
    15     int i,j;
    16     int len;
    17     struct Student *pArr;
    18     struct Student t;
    19 
    20     //动态的构造一维数组
    21     printf("请输入学生的个数:
    ");
    22     printf("len = ");
    23     scanf("%d",&len);
    24     
    25     pArr = (struct Student *)malloc(len * sizeof(struct Student));
    26     
    27     for(i=0; i<len; ++i)
    28     {
    29         printf("请输入第%d个学生的信息;
    ",i+1);
    30         printf("age = ");
    31         scanf("%d",&pArr[i].age);
    32         
    33         printf("name = ");
    34         scanf("%s",pArr[i].name); //name 是数组名,本身就已经是数组首元素的地址 所以pArr[i],name 就不能是 &pArr[i],name
    35     
    36         printf("score = ");
    37         scanf("%f",&pArr[i].score);
    38     }
    39     //按学生的成绩升序排序 冒泡算法
    40     for(i=0;i<len-1;++i)
    41     {
    42         for(j=0;i<len-1-i;++j)
    43         {
    44             if(pArr[j].score > pArr[j+1].score)
    45             {
    46                 t = pArr[j];
    47                 pArr[j] = pArr[j+1];
    48                 pArr[j+1] = t;
    49             }
    50         }
    51     }
    52     printf("
    
    学生的信息是:
    
    ");
    53     //输出
    54       for(i=0; i<len; ++i)
    55     {
    56         printf("第%d个学生的信息是:
    ",i+1);
    57         printf("age = %d 
    ",pArr[i].age);
    58         printf("name = %s 
    ",pArr[i].name);
    59         printf("score = %f 
    ",pArr[i].score);
    60         printf("
    ");
    61     }
    62     system("pause");
    63     return 0;
    学生管理系统
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 enum WeekDay
     4 {
     5     MonDay,TueDay,WednesDay,Thursday,FriDay,SaturDay,SunDay
     6 };
     7 
     8 int mian(void)
     9 {
    10     enum WeekDay day = WednesDay;
    11     printf("%d
    ",day);
    12     system("pause");
    13     return 0;
    14 }
    枚举
  • 相关阅读:
    [leetCode]127. 单词接龙
    [leetCode]450. 删除二叉搜索树中的节点
    [leetCode]701. 二叉搜索树中的插入操作
    [leetCode]235. 二叉搜索树的最近公共祖先
    [leetCode]501. 二叉搜索树中的众数
    $Abstract^2 Interpretation$
    图说 Python 内存管理
    Python 解释器初探
    幸福之路
    Spark编程基础
  • 原文地址:https://www.cnblogs.com/Maxwell599/p/3178543.html
Copyright © 2011-2022 走看看