class-notes/2207/C语言/结构体.md
2025-02-20 22:02:41 +08:00

335 lines
7.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 结构体类型定义
```c
/*
struct 结构体名{
数据类型 变量名;
数据类型 变量名;
};
CREATE TABLE 表名(
字段名 数据类型,
字段名 数据类型
);
*/
//声明了一个结构体类型 学生类型
struct student{
int id;//学号
char name[20];//姓名
int age;//年龄
char sex;//性别
double score;//成绩
};
```
### 结构体变量定义
```c
#include <stdio.h>
#include <string.h>
/*
struct 结构体名{
数据类型 变量名;
数据类型 变量名;
};
CREATE TABLE 表名(
字段名 数据类型,
字段名 数据类型
);
*/
//定义了一个结构体类型 学生类型
struct student{
int id;//学号
char name[20];//姓名
int age;//年龄
char sex[2];//性别
double score;//成绩
};
int main(void){
//结构体类型如果没有取别名的情况下
//需要 使用 struct 结构体名 结构体变量名; 来定义结构体变量
//定义了一个学生变量s1
struct student s1;//存储一个的学生
//对结构体变量进行赋值操作
s1.id = 100;
strcpy(s1.name, "小明");
s1.age = 18;
strcpy(s1.sex, "男");
s1.score = 99;
//对结构体变量进行取值操作
printf("%d %s %d %s %.0lf\n", s1.id, s1.name, s1.age, s1.sex, s1.score);
return 0;
}
```
```c
#include <stdio.h>
#include <string.h>
//结构体类型的定义
//1.在主函数内 通过struct 结构体类型名 变量1, 变量2;
//2.在定义结构体类型的时候定义
//3.只在定义结构体类型时定义变量(结构体类型只使用一次)
struct student{
int id;//学号
char name[20];//姓名
int age;//年龄
char sex[2];//性别
double score;//成绩
}stu1, stu2;
//在定义结构体类型的时候同时定义了两个结构体变量stu1, stu2
int main(void){
stu1.id = 1;
stu2.id = 2;
printf("%d %d\n", stu1.id, stu2.id);
return 0;
}
```
### 使用typedef对结构体类型起别名
```c
#include <stdio.h>
#include <string.h>
/*
struct 结构体名{
数据类型 变量名;
数据类型 变量名;
};
CREATE TABLE 表名(
字段名 数据类型,
字段名 数据类型
);
*/
//定义了一个结构体类型 学生类型
//typedef 数据类型 类型别名;
//typedef int element;
/*
typedef struct 结构体类型名{
数据类型 变量名1;
数据类型 变量名2;
}别名;
*/
typedef struct student{
//4+20+4+2+8
int id;//学号
char name[20];//姓名
int age;//年龄
char sex[2];//性别
double score;//成绩
}student;
//struct student <=> student
int main(void){
//结构体类型如果没有取别名的情况下
//需要 使用 struct 结构体名 结构体变量名; 来定义结构体变量
//定义了一个学生变量s1
struct student s1;//存储一个的学生
student s2;
//对结构体变量进行赋值操作
s1.id = 100;
strcpy(s1.name, "小明");
s1.age = 18;
strcpy(s1.sex, "男");
s1.score = 99;
//对结构体变量进行取值操作
printf("%d %s %d %s %.0lf\n", s1.id, s1.name, s1.age, s1.sex, s1.score);
return 0;
}
```
### 习题-1
```c
#include <stdio.h>
/*
1.定义一个图书类型,要求存储以下数据,图书编号、图书名、作者名、出版社、价格
定义两个图书变量别分命名为book1、book2存储以下数据
1 数据结构 桃桃 高等教育出版社 59.9
2 操作系统 台球王子 高等教育出版社 58.9
并打印出来
*/
struct book{
int id;//图书编号
char title[50];//图书名称
char author[50];//作者名称
char publishing[50];//出版社
double price;//价格
};
int main(){
struct book book1={1, "数据结构", "桃桃", "高等教育出版社", 59.9};
struct book book2={2, "操作系统", "台球王子", "高等教育出版社", 58.9};
printf("%d\t %s\t %s\t\t %s\t %.1lf\n", book1.id, book1.title,
book1.author, book1.publishing, book1.price);
printf("%d\t %s\t %s\t %s\t %.1lf\n", book2.id, book2.title,
book2.author, book2.publishing, book2.price);
return 0;
}
```
### 习题-2
```c
#include <stdio.h>
/*
2.定义一个动物类型,要求存储以下数据,自行推断成员变量类型
1 斑马 素食 一级保护动物
2 小熊猫 素食 一级保护动物
3 蝙蝠 肉食 其他动物
*/
typedef struct animal{
int id;
char type[20];
char foodType[20];
char leve[20];
}animal;
int main(){
animal a1 = {1, "斑马", "素食", "一级保护动物"};
animal a2 = {2, "小熊猫", "素食", "一级保护动物"};
animal a3 = {3, "蝙蝠", "肉食", "一级保护动物"};
printf("%d\t %s\t %s\t %s\n", a1.id, a1.type, a1.foodType, a1.leve);
printf("%d\t %s\t %s\t %s\n", a2.id, a2.type, a2.foodType, a2.leve);
printf("%d\t %s\t %s\t %s\n", a3.id, a3.type, a3.foodType, a3.leve);
return 0;
}
```
### 结构体数组、指针
```c
#include <stdio.h>
/*
2.定义一个动物类型,要求存储以下数据,自行推断成员变量类型
1 斑马 素食 一级保护动物
2 小熊猫 素食 一级保护动物
3 蝙蝠 肉食 其他动物
*/
typedef struct animal{
int id;
char type[20];
char foodType[20];
char leve[20];
}animal;
int main(){
int i;
animal aList[3] = {
{1, "斑马", "素食", "一级保护动物"},
{2, "小熊猫", "素食", "一级保护动物"},
{3, "蝙蝠", "肉食", "一级保护动物"}};
animal *pA = aList;//定义一个动物指针指向数组的第一个结构体变量的元素
//(*pA).id <=> pA->id
//1.使用*访问符得到元素
//printf("%d\n", (*pA).id);
//printf("%d\n", pA->id);
for(i=0;i<3;i++){
//aList[i] <=> *(aList+i
printf("%d\t %s\t %s\t %s\n", (aList+i)->id, (aList+i)->type,
(aList+i)->foodType, (aList+i)->leve);
}
return 0;
}
```
### 结构体类型作为函数的参数
```c
#include <stdio.h>
/*
2.定义一个动物类型,要求存储以下数据,自行推断成员变量类型
1 斑马 素食 一级保护动物
2 小熊猫 素食 一级保护动物
3 蝙蝠 肉食 其他动物
*/
typedef struct animal{
int id;
char type[20];
char foodType[20];
char leve[20];
}animal;
//结构体变量作为函数的参数
void print(animal a){
printf("%d\t %s\t %s\t %s\n", a.id, a.type, a.foodType, a.leve);
}
//结构体的指针变量作为函数的参数
void print2(animal *a){
printf("%d\t %s\t %s\t %s\n", a->id, a->type, a->foodType, a->leve);
}
int main(){
int i;
animal aList[3] = {
{1, "斑马", "素食", "一级保护动物"},
{2, "小熊猫", "素食", "一级保护动物"},
{3, "蝙蝠", "肉食", "一级保护动物"}};
animal *pA = aList;//定义一个动物指针指向数组的第一个结构体变量的元素
//(*pA).id <=> pA->id
//1.使用*访问符得到元素
//printf("%d\n", (*pA).id);
//printf("%d\n", pA->id);
for(i=0;i<3;i++){
print2(aList+i);
}
return 0;
}
```
### 使用结构体存储点通过函数计算两个点之间的距离
```c
#include <stdio.h>
#include <math.h>
typedef struct point{
int x;
int y;
}point;
float distance(point *p1, point *p2){
/*
pow((float)(p2->y - p1->y), 2) <=> (y2-y1)*(y2-y1)
*/
return sqrt(pow((float)(p2->y - p1->y), 2) + pow((float)(p2->x - p1->x), 2));
}
int main(){
point p1 = {1, 2}, p2 = {2, 2};
printf("两点之间的距离为%.2lf\n", distance(&p1, &p2));
return 0;
}
```