目录
1.结构体数组当作形参
和数组传参类似,将结构体数组的数组名当作形参传入函数
void sortStruct(STU student[], int length)
定义一个长度为5的结构体数组,做一个输入输出函数,再将5个人的成绩最大的和成绩最小的找出来,再通过ID来找到某个同学,并把信息输出,再做一个用名字查人的函数
#include<stdio.h>
#include<string.h>
typedef struct stu
{
int id;
char name[20];
int age;
float score;
}STU;
void scanf_struct(STU students[],int length);
void print_struct(STU students[],int length);
void sort_struct(STU students[],int length);
void name_struct(STU students[],int length);
STU max_struct(STU *pstudents,int length);
STU min_struct(STU *pstudents,int length);
STU find_id(STU *pstudents,int length,int id);
STU find_score(STU *pstudents,int length,float score);
STU find_name(STU *pstudents,int length,char *name);
int main()
{
STU students[5];
printf("请输入5个同学的个人信息及成绩:\n");
scanf_struct(students,5);
printf("5个同学的个人信息如下:\n");
print_struct(students,5);
sort_struct(students,5);
printf("根据学生的成绩进行如下排序:\n");
print_struct(students,5);
name_struct(students,5);
printf("根据学生的姓名进行如下排序:\n");
print_struct(students,5);
STU ret = max_struct(students,5);
printf("5个同学中成绩最高的是:");
printf("%d %s %d %f\n",ret.id,ret.name,ret.age,ret.score);
STU ret1 = min_struct(students,5);
printf("5个同学中成绩最低的是:");
printf("%d %s %d %f\n",ret1.id,ret1.name,ret1.age,ret1.score);
STU ret3 = find_id(students,5,1003);
printf("查看特定同学的ID:\n");
printf("%d %s %d %f\n",ret3.id,ret3.name,ret3.age,ret3.score);
STU ret4 = find_score(students,5,100);
printf("查看特定同学的SCORE:\n");
printf("%d %s %d %f\n",ret4.id,ret4.name,ret4.age,ret4.score);
STU ret5 = find_name(students,5,"zhishixuebao");
printf("查看特定同学的NAME:\n");
printf("%d %s %d %f\n",ret5.id,ret5.name,ret5.age,ret5.score);
return 0;
}
void scanf_struct(STU students[],int length)
{
int i;
for(i=0;i<length;i++)
{
scanf("%d%s%d%f",&students[i].id,students[i].name,&students[i].age,&students[i].score);
}
}
void print_struct(STU students[],int length)
{
int i;
for(i=0;i<length;i++)
{
printf("%d %s %d %.1f\n",students[i].id,students[i].name,students[i].age,students[i].score);
}
}
void sort_struct(STU students[],int length)
{
int i,j;
STU temp;
for(i=0;i<length-1;i++)
{
for(j=0;j<length-1-i;j++)
{
if(students[j].score>students[j+1].score)
{
temp = students[j];
students[j]=students[j+1];
students[j+1]=temp;
}
}
}
}
void name_struct(STU students[],int length)
{
int i,j;
STU temp;
for(i=0;i<length-1;i++)
{
for(j=0;j<length-1-i;j++)
{
if(strcmp(students[j].name,students[j+1].name)<0)
{
temp = students[j];
students[j]=students[j+1];
students[j+1]=temp;
}
}
}
}
STU max_struct(STU *pstudents,int length)
{
int i;
STU temp;
float max_score = pstudents[0].score;
for(i=0;i<length;i++)
{
if(max_score<pstudents[i].score)
{
max_score = pstudents[i].score;
temp = pstudents[i];
}
}
return temp;
}
STU min_struct(STU *pstudents,int length)
{
int i;
STU temp;
float min_score = pstudents[0].score;
for(i=0;i<length;i++)
{
if(min_score>pstudents[i].score)
{
min_score = pstudents[i].score;
temp = pstudents[i];
}
}
return temp;
}
STU find_id(STU *pstudents,int length,int id)
{
int i;
int flag = 0;
STU temp = {0,NULL,0,0};
for(i=0;i<length;i++)
{
if(id == pstudents[i].id)
{
return pstudents[i];
flag = 1;
}
}
if(flag ==0)
{
printf("找不到该学生的学号\n");
return temp;
}
}
STU find_score(STU *pstudents,int length,float score)
{
int i;
int flag = 0;
STU temp = {0,NULL,0,0};
for(i=0;i<length;i++)
{
if(score == pstudents[i].score)
{
return pstudents[i];
flag =1;
}
}
if(flag ==0)
{
printf("找不到该学生的成绩\n");
return temp;
}
}
STU find_name(STU *pstudents,int length,char *name)
{
int i;
int flag = 0;
STU temp = {0,NULL,0,0};
for(i=0;i<length;i++)
{
if(name == pstudents[i].name)
{
return pstudents[i];
flag =1;
}
}
if(flag ==0)
{
printf("找不到该学生的姓名\n");
return temp;
}
}
2.结构体嵌套指针
定义一个结构体的时候,定义一个指针,这个指针指向另一个结构体
typedef struct home
{
int id;
char name[20];
char addr[20];
}HOME;
typedef struct per
{
char name[20];
int age;
HOME *home;
} PER;
用冒泡排序根据年龄的大小,将输入的信息从小到大排序
#include<stdio.h>
#include<string.h>
typedef struct home
{
int id;
char name[20];
char addr[20];
}HOME;
typedef struct per
{
char name[20];
int age;
HOME *home;
} PER;
void scanfStruct(PER *per);
void printStruct(PER *per);
void sortStruct(PER *per);
int main()
{
HOME home = {1001, "lilei", "西安"};
PER person1 = {
.name = "lilei",
.age = 23,
.home = &home}, person2;
// printf("%s, %d, %d, %s, %s\n", person1.name, person1.age, person1.home->id, person1.home->name, person1.home->addr);
PER per[6];
scanfStruct(per);
printStruct(per);
sortStruct(per);
printStruct(per);
}
void scanfStruct(PER *per)
{
HOME home = {0,NULL,NULL};
printf("请输入姓名,年龄,学号,房产证名字,地址\n");
for(int i = 0; i < 6; i++)
{
per[i].home = &home;
scanf("%s%d%d%s%s", per[i].name, &per[i].age, &home.id, home.name, home.addr);
per[i].home->id = home.id;
strcpy(per[i].home->name, home.name);
strcpy(per[i].home->addr, home.addr);
}
}
void printStruct(PER *per)
{
for(int i = 0; i < 6; i++)
printf("%s, %d, %d, %s, %s\n", per[i].name, per[i].age, per[i].home->id, per[i].home->name, per[i].home->addr);
printf("\n");
}
void sortStruct(PER *per)
{
PER temp;
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 5 - i; i++)
{
if(per[j].age > per[j + 1].age)
{
temp = per[j];
per[j] = per[j + 1];
per[j + 1] = temp;
}
}
}
}
这里要记得,给带指针的结构体为他们赋值要分别赋值
3.赋值三种方法
void scanf_struct(PER person[],int length)
{
int i;
HOME home = {0,NULL,NULL};
for(i=0;i<length;i++)
{
person[i].home = &home;
//方法一:
// scanf("%s%d",person[i].name,&person[i].age);
// scanf("%d%s%s",&home.id,home.name,home.addr);
// person[i].home->id = home.id;
// strcpy(person[i].home->name,home.name);
// strcpy(person[i].home->addr,home.addr);
//方法二:
// scanf("%s%d%d%s%s",person[i].name,&person[i].age,&home.id,home.name,home.addr);
// person[i].home->id = home.id;
// strcpy(person[i].home->name,home.name);
// strcpy(person[i].home->addr,home.addr);
//方法三:
scanf("%s%d%d%s%s",person[i].name,&person[i].age,&person[i].home->id,person[i].home->name,person[i].home->addr);
}
}