您的位置:首页 > 编程语言 > C语言/C++

《IOS_C语言》结构体、结构体数组

2015-08-29 15:33 417 查看
一:结构体声明

typedef struct 结构体名

{

类型修饰符 成员名;

类型修饰符 成员名;

········

类型修饰符 成员名;

}结构体别名;

如:typedef struct stu

{

int num;

char name[40];

float score;

}Stu;

二:结构体变量定义

结构体变量:由结构体类型修饰的变量,称为结构体变量

定义格式:(1)struct 结构体名 变量名={初值};

(2)结构体别名 变量名={初值};//这种方法更好,简便

如:struct stu stu1={1,"wukong",89.4};

Stu stu1={a,"wukong",89.5};//这种更好

结构体成员变量的访问:

由“结构体变量名.成员变量名”组成,如:stu1.name//stu1的学号

注:结构体成员变量与普通变量⼀一样,可直接赋值,如:stu1.score=80;

但是:stu1.name="zhangsan";是错误的,因为是字符串,要用则需要用strcpy(stu1.name,"zhangsan")才可以

结构体变量也可以直接复制,如:stu1=stu2;

数组是不可以直接赋值的,但可以通过把数组放在结构体内实现数组的直接赋值

结构体的内存占用情况:

以最大的成员变量所站的空间为分配单位,按结构体成员变量声明顺序自上而下分配

注:分配空间不⾜足以存储成员变量时,分配新的空间单位

结构体的嵌套使用

如:

typedef struct date{

int year;

int month;

int day;

}MyDate;

typedef struct stu{

char name[40];

MyDate birthday;//访问方法:stu1.birthday.year;

};

三:结构体应用

如://练习1:有三个学⽣生,编程找出分数最⾼高者以及年龄最⼩小者

main.m文件

#import <Foundation/Foundation.h>

#import"Function.h"

//练习1:有三个学⽣生,编程找出分数最⾼高者以及年龄最⼩小者

//typedef struct student

//{

// int number;

// char name[40];

// int age;

// float score;

//}Student;

int main(int argc, const char * argv[]) {

//定义三个结构体变量stu1,stu2,stu3

Student stu1={1,"zhangsan",25,87};

Student stu2={2,"lisi",32,95};

Student stu3={3,"wangqiang",45,80};

// printf("%d %s %d %f\n",stu1.number,stu1.name,stu1.age,stu1.score);

Student maxScoreStudent=getMaxScoreStudent( stu1, stu2,stu3);

print(maxScoreStudent);

Student minAgeStudent=getMinAgeStudent(stu1, stu2, stu3);

print(minAgeStudent);

return 0;

}

Function.h文件

#import <Foundation/Foundation.h>

typedef struct student

{

int number;

char name[40];

int age;

float score;

}Student;

void print(Student stu);

Student getMaxScoreStudent(Student stu1,Student stu2,Student stu3);

Student getMinAgeStudent(Student stu1,Student stu2,Student stu3);

Function.m文件

#import "Function.h"

//由于返回的是整个结构体变量的信息,则返回的还是结构体类型的Student

Student getMaxScoreStudent(Student stu1,Student stu2,Student stu3)

{

Student maxScoreStudent=stu1;

if (maxScoreStudent.score<stu2.score){

maxScoreStudent=stu2;

}

if (maxScoreStudent.score<stu3.score){

maxScoreStudent=stu3;

}

return maxScoreStudent;

}

Student getMinAgeStudent(Student stu1,Student stu2,Student stu3)

{

Student minAgeStudent=stu1;

if (minAgeStudent.age>stu2.age){

minAgeStudent=stu2;

}

if (minAgeStudent.age>stu3.age){

minAgeStudent=stu3;

}

return minAgeStudent;

}

void print(Student stu){

printf("%d,%s,%d,%f\n",stu.number,stu.name,stu.age,stu.score);

}

四:结构体与数组

结构体与数组组合,更加灵活使用,意思是将多个结构体变量(stu1,stu2,stu3,```stu7)放在一起,构成结构体数组

如:Stu stus[7]={stu1,stu2,stu3,stu4,stu5,stu6,stu7};

访问时,则可以stus[0].name,也就是stu1.name

课上练习://求5个学生的年份由小到大排序,年份比较,再按排序后输出所有学生信息

本题用到结构体嵌套+结构体数组+函数调用知识点

main.m文件

#import <Foundation/Foundation.h>

#import "Function.h"

//练习2:

//定义嵌套结构体

//typedef struct date

//{

// int year;

// int month;

// int day;

//}MyDate;

//typedef struct person

//{

// char name[40];

// MyDate birthday;

//}Person;

int main(int argc, const char * argv[]) {

MyDate date1={1999,2,23};

MyDate date2={1992,9,23};

MyDate date3={1993,8,23};

MyDate date4={1997,2,23};

MyDate date5={1995,3,23};

Person person1={"zhangyi",date1};

Person person2={"zhanger",date2};

Person person3={"zhangsan",date3};

Person person4={"zhangsi",date4};

Person person5={"zhangwu",date5};

//定义结构体数组,主要是为了便于多结构体变量的某个成员变量的排序问题。

Person persons[5]={person1,person2,person3,person4,person5};

//求这些人的年份由小到大排序,年份比较,但是人person交换

sortByYear(persons,5);

//上面只是实现了排序,交换认得信息,下面print()是打印出所有结构体变量的信息

print(persons,5);

return 0;

}

Function.h文件

#import <Foundation/Foundation.h>

typedef struct date

{

int year;

int month;

int day;

}MyDate;

typedef struct person

{

char name[40];

MyDate birthday;

}Person;

void sortByYear(Person persons[],int n);

void print(Person persons[] ,int n);

Function.m文件

#import "Function.h"

void sortByYear(Person persons[],int n){

for(int i=0;i<n-1;i++){

for (int j=0; j<n-1-i; j++) {

//年份比较

if (persons[j].birthday.year>persons[j+1].birthday.year) {

//人整个数组信息交换

Person person=persons[j];//定义一个临时的结构体变量person来装person[i]结构体的所有信息

persons[j]=persons[j+1];

persons[j+1]=person;

}

}

}

}

void print(Person persons[], int n){

for (int i=0; i<n; i++) {

printf("name=%s,year=%d,month=%d,day=%d\n",persons[i].name,persons[i].birthday.year,persons[i].birthday.month,persons[i].birthday.day);

}

}

//练习二:

main.m文件

#import <Foundation/Foundation.h>

#import"Function.h"

//练习3:有5名学⽣生保存在结构体数组中,编程查找成绩最⾼高者,输出该学⽣生全部信息

//typedef struct student

//{

// char name[20];

// int number;

// int age;

// float score;

//}Student;

int main(int argc, const char * argv[]) {

Student stu1={"zhangsan",1,34,98};

Student stu2={"lisi",2,23,78};

Student stu3={"wangwang",3,14,67};

Student stu4={"huangli",4,56,96};

Student stu5={"hudan",5,21,100};

//学生的结构体数组:

Student stus[5]={stu1,stu2,stu3,stu4,stu5};

//取最大值方法一:(不好)

// Student maxScoreStudent=getTwoMaxScoreStudent(stu1,stu2 );

// maxScoreStudent=getTwoMaxScoreStudent(maxScoreStudent,stu3 );

// maxScoreStudent=getTwoMaxScoreStudent(maxScoreStudent,stu4 );

// maxScoreStudent=getTwoMaxScoreStudent(maxScoreStudent,stu5 );

// printf("成绩最高的学生信息是\n");

// print(maxScoreStudent);

int count=5;

//取最大值方法二:

printf("成绩最高的学生信息是\n");

getMaxScoreStudent(stus,count);

//对上述5名学⽣生数组,按成绩从⾼高到低排序,并输出

printf("按成绩从⾼高到低排序,并输出:\n");

sortArray(stus,count);//函数调用(里面再调用输出函数print()函数)

return 0;

}

Function.h文件

#import <Foundation/Foundation.h>

typedef struct student

{

char name[20];

int number;

int age;

float score;

}Student;

void print(Student student);

void sortArray(Student stus[],int count);

void getMaxScoreStudent(Student stus[],int count);

Function.m文件

#import "Function.h"

//本函数只是输出一个人的信息,不同那个输出所有学生信息的函数,那个则

//需要传入结构体数组名,以及打印的人数print(Student stus[],int count)

void print(Student student){

printf("name=%s,number=%d,age=%d,score=%.2f\n",student.name,student.number,student.age,student.score);

}

//取最大值方法一:(不好,不简便)

//Student getTwoMaxScoreStudent(Student stu1,Student stu2 )

//{

// Student maxScoreStudent=stu1.score>stu2.score?stu1:stu2;

// return maxScoreStudent;

//}

//取最大值方法二

void getMaxScoreStudent(Student stus[],int count)

{

Student maxScoreStudent=stus[0];//把stus[0]结构体赋给maxScoreStudent结构体,进行比较

for (int i=1; i<count; i++) {

if(maxScoreStudent.score<stus[i].score){

//成绩比较,但实际上是人的信息进行交换

maxScoreStudent=stus[i];

}

}

//调用输出一个结构体信息的函数

print(maxScoreStudent);

}

//对上述5名学⽣生数组,按成绩从⾼高到低排序,并输出函数

void sortArray(Student stus[],int count)

{

//降序排序

for (int i=0; i<count-1; i++) {

for (int j=0; j<count-1-i; j++) {

if (stus[j].score<stus[j+1].score) {

Student stu=stus[j];

stus[j]=stus[j+1];

stus[j+1]=stu;

}

}

}

//调用输出函数

for (int i=0; i<count; i++) {

print(stus[i]);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: