Auto commit

This commit is contained in:
2025-02-20 22:02:41 +08:00
parent cec338f3d2
commit a800cf8ba5
13 changed files with 0 additions and 856 deletions
+297
View File
@@ -0,0 +1,297 @@
### 贪吃蛇
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <process.h>
#define WIDTH 50 //宽度
#define HEIGHT 30 //高度
#define SNAKE_LENGHT 100 //蛇的最大长度
#define UP 1
#define DOWN 2
#define LEFT 3
#define RGIHT 4
char map[HEIGHT][WIDTH];//地图字符数组
int sankeX[SNAKE_LENGHT], sankeY[SNAKE_LENGHT];//蛇的坐标
int foodX, foodY;//食物的坐标
int size = 1;//当前蛇的长度
int gameStatus = 1;
int direction = RGIHT;
/*
_kbhit()用来判断输入的是否是空格
_getch()用来判断键盘输入的内容
*/
void draw(void* param){
int i, j;
char buffer[(HEIGHT+1)*(WIDTH+1)];
int k;
while(gameStatus){
system("cls");
k=0;
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
buffer[k++] = map[i][j];
}
buffer[k++] = '\n';
}
buffer[k] = '\0';
printf("%s\n", buffer);
Sleep(200);
}
}
//打印地图
void changeMap(void* param){
int i, j, k;
int isFill;
while(gameStatus){
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
if(i==0 || i == HEIGHT-1 || j==0 || j == WIDTH - 1){
map[i][j] = '#';
}else if(i == foodY && j == foodX){
map[i][j] = '$';
}else if(i == sankeY[0] && j == sankeX[0]){
map[i][j] = '0';
}else{
isFill = 0;
for(k=1;k<size;k++){
if(i == sankeY[k] && j == sankeX[k]){
map[i][j] = 'o';
isFill = 1;
}
}
if(!isFill){
map[i][j] = ' ';
}
}
}
}
}
}
void changeSnake(void* param){
//蛇的移动
while(gameStatus){
int sankeXpre = sankeX[0], sankeYpre=sankeY[0];
int sankeXpre2, sankeYpre2;
int i;
Sleep(400-size*2);
sankeX[0] += (direction == RGIHT)?1:(direction == LEFT)?-1:0;
sankeY[0] += (direction == DOWN)?1:(direction == UP)?-1:0;
for(i=1;i<size;i++){
sankeXpre2 = sankeX[i];
sankeYpre2 = sankeY[i];
sankeX[i] = sankeXpre;
sankeY[i] = sankeYpre;
sankeXpre = sankeXpre2;
sankeYpre = sankeYpre2;
}
//判断蛇是否吃到食物
if(sankeX[0] == foodX && sankeY[0] == foodY){
size++;
foodX = rand()%(WIDTH-5)+3;
foodY = rand()%(HEIGHT-5)+3;
}
//边界检测、以及是否撞到自己
for(i=1;i<size;i++){
if(sankeX[0] == sankeX[i] && sankeY[0] == sankeY[i]){
gameStatus = 0;
}
}
if(sankeX[0] == 0 || sankeX[0] == WIDTH || sankeY[0]==0 || sankeY[0] == HEIGHT){
gameStatus = 0;
}
}
}
void input(void* param){
char key;
while(gameStatus){
if(_kbhit()){
key = _getch();
switch(key){
case 'w':direction=UP;break;
case 's':direction=DOWN;break;
case 'a':direction=LEFT;break;
case 'd':direction=RGIHT;break;
case 'q':gameStatus=0;break;
}
}
}
}
int main() {
system("color 02");
srand((unsigned)time(NULL));
sankeX[0] = WIDTH/2;
sankeY[0] = HEIGHT/2;
foodX = rand()%(WIDTH-5)+3;
foodY = rand()%(HEIGHT-5)+3;
_beginthread(changeMap, 0, NULL);
_beginthread(draw, 0, NULL);
_beginthread(input, 0, NULL);
_beginthread(changeSnake, 0, NULL);
while(gameStatus){
}
printf("游戏结束您的得分是%d\n", size-1);
return 0;
}
```
<img src="https://yp.smallkun.cn/markdown/image-20250104225941821.png!compress" alt="image-20250104225941821" style="zoom:50%;" />
<img src="https://yp.smallkun.cn/markdown/image-20250104225957306.png!compress" alt="image-20250104225957306" style="zoom:50%;" />
### 飞机大战
```c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <process.h>
#include <Windows.h>
#define HEIGHT 40
#define WIDTH 30
char map[HEIGHT][WIDTH];
int score = 0;
int gameStatus = 1;
int planeX = 12;
int planeY = 36;
#define bulletNum 1000
int bulletX[bulletNum];
int bulletY[bulletNum];
int bulletRear = 0;//最后的子弹
int bulletFront = 0;//最前面的子弹
char* plane[6] = {" * ", "*****", " * * "};
void changeMap(void* param){
int i, j;
while(gameStatus){
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
if(i == HEIGHT-1){
map[i][j] = '~';
}else if(j == 0 || j == WIDTH-1){
map[i][j] = '|';
}else{
map[i][j] = ' ';
}
}
}
for(i = 0;i<3;i++){
for(j = 0;j<5;j++){
map[planeY+i][planeX+j] = plane[i][j];
}
}
i=bulletFront;
while(i!=bulletRear){
map[bulletY[i]][bulletX[i]] = '*';
i=(i+1)%bulletNum;
}
Sleep(100);
}
}
void darwMap(void* param){
int i, j, k;
char buffer[100000];
while(gameStatus){
k=0;
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
buffer[k++] = map[i][j];
}
buffer[k++] = '\n';
}
buffer[k] = '\0';
system("cls");
printf(buffer);
printf("您的得分:%d\n", score);
printf("操作说明:WSAD分别对印前后左右移动\n**空格是发出子弹**\n");
}
}
void changeBullets(void* param){
int i;
while(gameStatus){
Sleep(500);
i=bulletFront;
while(i!=bulletRear){
bulletY[i]--;
if(bulletY[i] < 0){
bulletFront=(bulletFront+1)%bulletNum;
}
i=(i+1)%bulletNum;
}
}
}
void shootBullets(){
// planeY+1
bulletX[bulletRear] = planeX+2;
bulletY[bulletRear++] = planeY-1;
}
void input(void* param){
char key;
while(gameStatus){
if(_kbhit()){
key = _getch();
switch(key){
case 'q':gameStatus = 0;break;
case 'w':planeY--;break;
case 's':planeY++;break;
case 'a':planeX--;break;
case 'd':planeX++;break;
case 32:shootBullets();break;
}
}
}
}
int main(void){
system("color 02");
_beginthread(changeMap, 0, NULL);
_beginthread(darwMap, 0, NULL);
_beginthread(input, 0, NULL);
_beginthread(changeBullets, 0, NULL);
while(gameStatus){
}
return 0;
}
```
<img src="https://yp.smallkun.cn/markdown/image-20250105004011680.png!compress" alt="image-20250105004011680" style="zoom:50%;" />
+67
View File
@@ -0,0 +1,67 @@
### 标准输入输出
```c
#include <stdio.h>
/*
scanf 格式化输入
printf 格式化输出
getchar 读取单个字符
putchar 输出单个字符
gets 读取一行字符串 空格也会读入
puts 输出字符串(会自带一个换行)
*/
int main(){
char str[100];
/*
scanf("%s", str);//占位符遇到空格结束
*/
gets(str);//读取一行字符串
printf("%s\n", str);
return 0;
}
```
### 字符串处理
```c
#include <stdio.h>
#include <string.h>
/*
strlen 返回当前字符串长度 以结束符为标记
strcpy(目标串, 需要复制的串) 复制字符串
strncpy(目标串, 需要复制的串) 复制指定长度的字符串
strcat(字符串1, 字符串2) 拼接两个字符串,相当于在字符串1后加上字符串2后返回字符串1的首地址
strncat(字符串1, 字符串2) 拼接指定长度字符串(字符串2的长度)
*/
int main(void){
/*
烫烫烫:未初始化的字符数组打印出来
屯屯屯:未初始化的堆空间(C++)
锟斤拷:将GBK字符编码的文字转换为UTF-8后再转换成GBK
*/
char *str = "HelloWolrd";//指向一个字符串常量的首地址
char str2[20];
char str3[20];
char *str4;
char str5[20] = "";
strcpy(str2, str);//将字符串str的数据复制到str2这个字符数组中
strncpy(str3, str, 5);//复制str中的前5个字符到str3中
str3[5] = '\0';//设置结束标记
puts(str2);//会一直打印字符直到遇到字符串结束标记
puts(str3);
str4 = strcat(str2, str3);//str4相当于指向str2的首地址
puts(str2);//打印str2字符数组
puts(str4);//str4是一个字符串指针 指向str2的首个元素
strncat(str5, str3, 3);//将str5和str3中的三个字符拼接起来并存储在str5中
puts(str5);//打印str5
return 0;
}
```
+111
View File
@@ -0,0 +1,111 @@
```c
/*
这段代码实现的是经典的 汉诺塔问题 的递归解法。
汉诺塔问题 是一个数学问题,具体描述如下:
给定三个柱子:A、B、C
有若干个大小不等的圆盘(通常为n个),初始时所有圆盘都堆叠在A柱子上,从小到大按顺序叠放;
目标是将这些圆盘从A柱子移动到C柱子上,并且每次只能移动一个圆盘,且大的圆盘不能放在小的圆盘上面。
在这段代码中,hanoi 函数递归地解决了这个问题,参数 n 表示要移动的圆盘数,x、y、z 分别表示源柱子、辅助柱子和目标柱子。
代码详解:
基本思路:
如果只有一个盘子(n == 1),直接从 x 移动到 z。
如果有多个盘子(n > 1),可以通过以下步骤完成: 先将前 n-1 个盘子从 x 移到 y,这时 z 作为辅助柱子。
将第 n 个盘子从 x 移动到 z。
最后将 n-1 个盘子从 y 移到 z,这时 x 作为辅助柱子。
函数 hanoi 的参数:
n:表示要移动的圆盘的数量。
x:源柱子。
y:辅助柱子。
z:目标柱子。
递归过程:
对于每个递归调用,问题规模逐渐减小,最终会到达只有一个盘子的情况(基准条件)。
程序输出:
A -> C
A -> B
C -> B
这个输出表示了将两个圆盘从A柱子移动到C柱子所需的步骤。输出结果每一行表示一个移动的操作。
总结:
这段代码是解决汉诺塔问题的经典递归解法。通过分而治之的策略,把问题分解为更小的子问题来求解。
*/
```
### 函数递归打印汉诺塔移动步骤
```c
#include <stdio.h>
/*
n:盘片数
x:源柱子
y:中间柱子
z:目标柱子
判断盘子个数:
如果只有一个盘子,那么直接搬到目标柱子
否则先将上面的n-1的盘子通过目标柱子移
动到非目标柱子上后再将剩下的最大的盘子移动到目标柱子
*/
//n的盘子从x通过y移动到z
void hanoi(int n, char x, char y, char z){
if(n == 1){
printf("%c -> %c\n", x, z);
}else{
hanoi(n-1, x, z, y);
printf("%c -> %c\n", x, z);
hanoi(n-1, y, x, z);
}
}
int main(void){
hanoi(3, 'A', 'B', 'C');
return 0;
}
```
### 函数递归求汉诺塔移动的次数
```c
#include <stdio.h>
/*
n:盘片数
x:源柱子
y:中间柱子
z:目标柱子
判断盘子个数:
如果只有一个盘子,那么直接搬到目标柱子
否则先将上面的n-1的盘子通过目标柱子移
动到非目标柱子上后再将剩下的最大的盘子移动到目标柱子
*/
//n的盘子从x通过y移动到z
//int count = 0;//移动盘片的次数
//而不是只经过B柱子的次数
int hanoi(int n, char x, char y, char z){
int count = 0;//局部变量 用来存储当前这次函数调用中的移动盘子的次数
if(n == 1){
//printf("%c -> %c\n", x, z);
count++;
}else{
count += hanoi(n-1, x, z, y);
//printf("%c -> %c\n", x, z);
count++;
count += hanoi(n-1, y, x, z);
}
return count;
}
int main(void){
printf("%d\n", hanoi(3, 'A', 'B', 'C'));
return 0;
}
```
+107
View File
@@ -0,0 +1,107 @@
### 写法1-暴力写法 使用数组来模拟队列
```c
#include <stdio.h>
/*
约瑟夫环,人数n,编号m
队列 先进先出
a 3 1 2
头 下标
尾 下标
*/
//rear尾巴 front头
int queue[200] = {0};
int rear=0, front=0;
//用来拿到队列最前面的元素值
int get(){
int temp = queue[front];
queue[front++] = 0;
return temp;
}
//用来在队列末尾差值
void put(int n){
queue[rear++] = n;
}
//打印队列当前状态
void print(){
int i;
for(i=0;i<200;i++){
if(queue[i] != 0){
printf("%d ", queue[i]);
}
}
printf("\n");
}
//判断队列是否为空
int isEmpty(){
int count = 0;
int i;
for(i=0;i<200;i++){
if(queue[i] != 0){
count++;
}
}
return count;
}
int main(){
int n, m;//n人数 m为弹出的编号
int i, count = 0;
scanf("%d %d", &n, &m);
for(i=1;i<=n;i++){
put(i);
}
print();
while(isEmpty()){
count++;
if(count % m == 0){
printf("%d ", get());
}else{
put(get());
}
}
return 0;
}
```
### 写法2-循环队列 使用数组模拟循环队列
```c
#include <stdio.h>
/*
使用数组模拟循环队列
*/
int main(){
int joseph[100] = {0};
int n, m, count = 0;//n:人数 m:编号数 count:已出局的人数
int i = 1, k = 0;//i:用来遍历数组 k用来记录已经报数的编号
printf("请输入人数以及编号数:");
scanf("%d %d", &n, &m);
for(i = 0; i < n;i++){
joseph[i] = i+1;//标记为1 则为非空代表当前下标编号有人
}
i = 0;
while(n > count){
if(joseph[i]){
k++;//报数
if(k%m == 0){//判断是否弹出
printf("%d ", joseph[i]);
joseph[i] = 0;//标记为0 代表当前下标的人已经弹出
count ++;
}
}
i = (i+1)%n;
}
printf("\n");
return 0;
}
```
+156
View File
@@ -0,0 +1,156 @@
### 顺序表
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <Windows.h>
/*
顺序表 连续存储的动态数组
链表 分散存储的动态数组
栈 后进先出的数组
队列 先进先出的数组
*/
//顺序表
typedef struct List{
int *arr;//用来存储数据首地址
int size;//用来存储当前已经插入的元素个数
int capacity;//用来存储最大的容量
}List;
//函数声明
//对顺序表进行初始化
void init(List *list, int n){
//对List结构体中arr指针变量 指向一块动态内存分配的地址
list->arr = (int *)malloc(sizeof(int)*n);
//malloc 用来分配指定大小的内存空间并返回 void*类型的首地址
list->size = 0;//设置当前元素个数为0
list->capacity = n;//设置当前数组容量为n
memset(list->arr, 0, sizeof(int)*n);
//memset对指定内存空间 设置初始值为0
}
//传入list结构体指针 并对其中
void enlargeList(List *list){
//对数组元素进行扩容 数组起始地址 扩大后的内存大小
list->arr = (int *)realloc(list->arr, sizeof(int)*list->capacity*2);
//对扩容后的空间进行初始化赋值给0
memset(list->arr + list->capacity, 0, list->capacity*sizeof(int));
//容量扩大一倍
list->capacity *= 2;
}
//在顺序表最后面插值
void add(List *list, int num){
//对数组最后一个位置进行赋值
//当前数组元素个数应小于当前容量
if(list->size == list->capacity){
enlargeList(list);
}
*(list->arr + list->size) = num;
list->size++;//元素个数加一
}
//遍历数组中元素并打印
void traverse(List *list){
int i;
for(i=0;i<list->size;i++){
printf("%2d ", *(list->arr + i));
if((i+1)%10==0){
printf("\n",(i+1));
//Sleep(1000);
}
}
printf("\n");
}
int getRandNum(int startNum, int endNum){
return rand()%(endNum - startNum) + startNum;
//计算出start~end之间的随机整数并包括start和end
}
//在指定位置插值
void addByIndex(List *list, int num, int n){
int i;
if(list->size == list->capacity){
enlargeList(list);
}
//将这个位置-1开始的所有元素整体往后挪动一个位置
for(i=list->size-1;i>=n-1;i--){
*(list->arr+i+1) = *(list->arr+i);
}
*(list->arr+n-1) = num;
list->size++;//元素个数+1
}
//删除指定位置元素并返回该元素
int deleteByIndex(List *list, int n){
int i;
//从删除位置n-1开始 元素整体前移,覆盖掉n-1位置需要删除的元素
for(i=n-1;i<list->size-1;i++){
*(list->arr + i) = *(list->arr + i + 1);
}
*(list->arr + list->size - 1) = 0;
list->size--;
}
//判断顺序表是否为空
//size为0则返回1 代表数组是空的
int isEmpty(List *list){
return list->size?0:1;
}
//判断顺序表是否已满
//size==capacity 为真
int isFull(List *list){
return list->size == list->capacity?1:0;
}
//获取顺序表中的元素个数
int size(List *list){
return list->size;
}
//对顺序表中元素进行升序排序
void sort(List *list){
int i, j, temp;
for(i=0;i<list->size-1;i++){
for(j=0;j<list->size-1-i;j++){
if(list->arr[j] > list->arr[j+1]){
temp = list->arr[j];
list->arr[j] = list->arr[j+1];
list->arr[j+1] = temp;
}
}
}
}
int main(void){
List list;//定义结构体变量
List *p = &list;
int i;
system("color f4");
srand((unsigned)time(NULL));//以当前时间为随机数种子
init(p, 1);//顺序表的初始化
add(p, 1);
add(p, 2);
add(p, 3);
addByIndex(p, 66, 2);
addByIndex(p, 100, 1);
deleteByIndex(p, 3);
sort(p);
traverse(p);
return 0;
}
```
+334
View File
@@ -0,0 +1,334 @@
### 结构体类型定义
```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;
}
```