2023-2024-1 20231301 《计算机基础与程序设计》第十三周学习总结

发布时间 2023-12-24 19:56:51作者: 20231301周子昂

2023-2024-1 20231301 《计算机基础与程序设计》第十三周学习总结

作业信息

作业 链接
作业课程 <班级>(2023-2024-1-计算机基础与程序设计
作业要求 <作业>(2023-2024-1计算机基础与程序设计第十三周学习总结)
作业目标 <《C语言程序设计》预习第十二章> 《C语言程序设计》
作业正文 <博客>(第十三周学习总结

学习内容总结

《C语言程序设计》第十二章

12.1 结构体

数组:是将同种属性的数据进行一次处理
结构体:将不同类型的数据成员组织到统一的名字下
共用体:将不同类型的数据成员组织到统一的名字下,但是每一时刻只有一个数据成员起作用

struct student
{
    long studentID;
    char studentName;
    char studentSex;
    int yearOfBirth;
    int score[4];
}stu1;

用typedef定义数据类型
typedef int INTEGER;INTEGER和int是同义词。

typedef struct student STUDENT;
//等价于
typedef struct student
{
long studentID;
char studentName;
char studentSex;
int yearOfBirth;
int score[4];
}STUDENT;

二者为struct student结构体定义了一个新名字STUDENT。

结构体变量的初始化

STUDENT stu1={111,“张三”,“男”,1991,{80,80,80,80}};
//等价于:
struct student stu1={111,“张三”,“男”,1991,{80,80,80,80}};

嵌套的结构体

typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
 
typedef struct student
{
    long studentID;
    char studentName;
    char studentSex;
    DATE Birthday;
    int score[4];
}STUDENT;
STUDENT stu1={111,“张三”,“男”,{1998,6,4},{80,80,80,80}};

结构体变量的引用和赋值

#include<stdio.h>
typedef struct date
{
     int year;
     int month;
     int day;
}DATE;
 
typedef struct student
{
     long studentID;
     char studentName[20];
     char studentSex;
     DATE birthday;
     int score[4];
}STUDENT;
 
int main()
{
     STUDENT stu1={111,"张三","男",{1998,6,4},{80,80,80,80}};
     STUDENT stu2;
     stu2=stu1;//相同类型的结构体的变量可以整体赋值
    printf("%ld%s%c%d/%d/%d%d%d%d%d\n",stu2.studentID,
    stu2.studentName,stu2.studentSex,stu2.birthday.year,
    stu2.birthday.month,stu2.birthday.day,stu2.score[0],
    stu2.score[1],stu2.score[2],stu2.score[3]);
     return 0;
}

并非所有的结构体成员都可以用赋值运算符(=)进行赋值,对于字符数组类型的结构体成员赋值时,必须用字符串处理函数strcpy():strcpy(stu2.studentName,stu2.studentName);

结构体所占内存的字节数

#include<stdio.h>
typedef struct sample
{
    char m1;
    int m2;
    char m3;
}SAMPLE;
int main()
{
    SAMPLE S={'A',2,'b'};
    printf("%d\n",sizeof(s));
    return 0;
}

结构体数组的定义和初始化

#include<stdio.h>
typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
typedef struct student
{
    long studentID;
    char studentName[20];
    char studentSex;
    DATE birthday;
    int score[4];
}STUDENT;
int main()
{
    int i,j,sum[30];
    STUDENT stu[30]={{111,"张三","男",{77,77,77,77}},
    {112,"李四","男",{77,77,77,77}},{113,"王五","女",{77,77,77,77}},
    {114,"周六","女",{77,77,77,77}}};
    for(i=0;i<4;i++)
    {
        sum[i]=0;
        for(j=0;j<4;j++)
        {
            sum[i]+=stu[i].score[j];
        }
        printf("%ld%s%c%d/%d/%d%d%d%d%d%f\n",stu2.studentID,stu2.studentName,
        stu2.studentSex,stu2.birthday.year,stu2.birthday.month,stu2.birthday.day,
        stu2.score[0],stu2.score[1],stu2.score[2],stu2.score[3],sum[i]/4.0);
    }
    return 0;
}

结构体指针定义和初始化

STUDENT *p;
p = &stu1;
//等价于
STUDENT *P = &stu1;

指针变量指向的结构体成员用指向运算符

p->studentID=111;
//等价于:
(*p).studentID=111;
p->birthday.year=1991;//对于结构体内部数组成员的访问

向函数传递结构体

#include<stdio.h>
struct date
{
    int year;
    int month;
    int day;
};
//void fun(struct date *p)
void fun(struct date p)
{
    p.year=2000;
    p.month=5;
    p.day=22;
}
int main()
{
    struct date b;
    b.year=1999;
    b.month=4;
    b.day=21;
    //fun(&b);传址调用,结构体变量的成员值会修改
    fun(b);//结构体变量作为实参,传值调用,结构体变量的成员值不会修改
    return 0;
}
 
 
#include<stdio.h>
struct date
{
    int year;
    int month;
    int day;
};
struct date fun(struct date p)
{
    p.year=2000;
    p.month=5;
    p.day=22;
    return p;
}
int main()
{
    struct date b;
    b.year=1999;
    b.month=4;
    b.day=21;
    b=fun(b);//将函数返回值作为结构体变量的值
    return 0;
}

12.2共用体

#include<stdio.h>
union sample
{
    short i;
    char ch;
    float f;
};
typedef union sample SAMPLE;
int main()
{
    printf("%d\n",sizeof(SAMPLE));
    SAMPLE num; 
    num.i=20;
    return 0;
}

枚举数据类型

enum response{no,yes,none};//定义了一个枚举类型,no->0,yes->1,none->2;
enum response answer;//定义了一个枚举类型的变量
if(answer==yes)
{
    …;
}
//定义并初始化
enum{no,yes,none}answer;

定义一个枚举类型的数组

enum response answer[10];
enum response{no=-1,yes=1,none=0};//指定枚举常量的值
enum response{no=1,yes,none};//其后依次递增1

12.3动态数据结构—单向链表

//链表只能顺序访问,不能随机访问,尾插法
#include<stdio.h>
struct link
{
    int date;
    struct link *next;
};
struct link *AppendNode(struct link *head)
{
    struct link *p=NULL,*pr=head;
    int date;
    p=(struct link *)malloc(sizeof(struct link));
    if(p==NULL)
    {
        exit(0);
    }
    if(head==NULL)
    {
        head=p;
    }
    else
    {
        while(pr->next!=NULL)
        {
            pr=pr->next;
        }
        pr->next=p;
        scanf("%d",&date);
        p->date=date;
        p->next=NULL;
        return head;
    }
}
 
void DisplyNode(struct link *head)
{
    struct link *p=head;
    while(p!=NULL)
    {
       printf("%d ",p->date);
        p=p->next;
    }
}
 
void DeleteMemory(struct link *head)
{
    struct link *p=head,*q=NULL;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
        free(q);
    }
}
 
int main()
{
    int i=0;//节点的个数
    char c;
    struct link *head=NULL;
    scanf(" %c",&c);//前面有一个空格
    while(c='y'||c='Y')
    {
        head=AppendNode(head);
        DisplyNode(head);
        scanf(" %c",&c);
        i++;
    }
    DeleteMemory(head);
    return 0;
}

学习中的问题

1.keepass口令管理实践
2.GPG实践
3.OpenSSL(系列)