声明;定义;应用;结构体与数组
结构体成员访问
typedef struct point{
float x;
float y;
}MyPoint;
typedef struct Size{
float weight;
float height;
} MySize;
typedef struct Ret{
MyPoint point;
MySize size;
}MyRect;
int main(int argc, const char * argv[])
{
MyRect rect1={100,10,120,50};// MyRect rect1={{100,10},{120,50}};//
printf("%f,%f,%f,%f",rect1.point.x,rect1.point.y,rect1.size.height,rect1.size.weight);
scanf和getchar
构造类型:由基本数据类型组成的
结构体也属于构造类型
,
结构体比数组更加灵活,可以存放不同类型的变量.int array[3]={0},array[0]数组元素,那么二维数组array[i][j]——双下表变量
结构体是由若干成员组成的.成员既可以是基本数据类型,又可以是构造类型.比如数组.
结构体属于自定义数据类型.
声明:
struct 结构体名
{
类型说明符 成员名;
…………;
类型说明符 成员名;
}; //注意此处分号不能少
结构体变量定义
结构体变量定义
结构体是自定义类型,结构体变量时由------------结构体类型修饰的变量.
定义结构体变量
struct 结构体名 变量名={初值};
例如:struct student stu1={1,”zhangsan”,’m’,60};
int =====struct studeng ========类型修饰符
结构体成员访问
结构体成员的表示形式:
例如:
stu1.num;//stu1的学号
stu1.name;//stu1的姓名
结构体成员的使用和普通变量使用没有区别,可以直接赋值.
typedef
typedef
typedef 为现有类型创建一个新的名字,或称为类型别名.
typedef 原类型 新类型
例如:typedef int integer
这样 int a=10;
可以写成
integer a=10
typedef struct student{
short number;//学号
char name[20];//姓名(数组,构造类型)
char sex;//性别
float score;//成绩
}Stu;
typedef struct student{
short number;//学号
char name[20];//姓名(数组,构造类型)
char sex;//性别
float score;//成绩
}Stu;
//struct student{
// short number;//学号
// char name[20];//姓名(数组,构造类型)
// char sex;//性别
// float score;//成绩
//};
// short number;//学号
// char name[20];//姓名(数组,构造类型)
// char sex;//性别
// float score;//成绩
//};
//typedef 来给已有结构体类型起个别名
//typedef struct student Stu;
结构体变量
//typedef struct student Stu;
结构体变量
结构体变量能直接赋值
例如:stu1=stu2;
注意:
虽然数组和结构体都属于构造类型,但是结构体变量能直接赋值,数组不能直接赋值.
小技巧:可以通过把数组放在结构体内实现数组的直接赋值.
结构体内存占用
结构体内存占用
结构体所占的空间可以简单的认为是各个成员所占内存空间之和
实际是最大成员变量所含空间的最小整数倍
结构体嵌套
结构体嵌套
结构体的成员荏苒是结构体
typedef struct point{
float x;
float y;
}MyPoint;
typedef struct Size{
float weight;
float height;
} MySize;
typedef struct Ret{
MyPoint point;
MySize size;
}MyRect;
int main(int argc, const char * argv[])
{
MyRect rect1={100,10,120,50};// MyRect rect1={{100,10},{120,50}};//
printf("%f,%f,%f,%f",rect1.point.x,rect1.point.y,rect1.size.height,rect1.size.weight);
void NBRectMake(NBRect rect){
for(int i=0;i<=rect.point.y+rect.size.h;i++){
for (int j=0; j<=rect.point.x+rect.size.w; j++) {
if (((i==rect.point.y)&&(j>=rect.point.x&&j<=rect.point.x+rect.size.w))||((i==rect.point.y+rect.size.h)&&(j>=rect.point.x&&j<=rect.point.x+rect.size.w))||((i>=rect.point.y&&i<=rect.point.y+rect.size.h)&&(j==rect.point.x))||((i>=rect.point.y&&i<=rect.point.y+rect.size.h)&&(j==rect.point.x+rect.size.w))) {
printf("*");
}
结构体数组
可以将多个结构体变量放到数组中,构成结构体数组
例如:struct stu students[10]={0};
通过下标可以访问每个结构体变量
// Stu stu2={111,"alisa",'F',20,85};
// Stu stu3={112,"mike",'F',30,60};
for(int i=0;i<=rect.point.y+rect.size.h;i++){
for (int j=0; j<=rect.point.x+rect.size.w; j++) {
if (((i==rect.point.y)&&(j>=rect.point.x&&j<=rect.point.x+rect.size.w))||((i==rect.point.y+rect.size.h)&&(j>=rect.point.x&&j<=rect.point.x+rect.size.w))||((i>=rect.point.y&&i<=rect.point.y+rect.size.h)&&(j==rect.point.x))||((i>=rect.point.y&&i<=rect.point.y+rect.size.h)&&(j==rect.point.x+rect.size.w))) {
printf("*");
}
结构体数组
可以将多个结构体变量放到数组中,构成结构体数组
例如:struct stu students[10]={0};
通过下标可以访问每个结构体变量
typedef struct stu{
int number;
char name[20];
float score;
} Student;
// Stu stu2={111,"alisa",'F',20,85};
// Stu stu3={112,"mike",'F',30,60};
typedef struct stu{
int number;
char name[20];
float score;
} Student;
void bubbleSort(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 tempStu=stus[j];//交换结构体
stus[j]=stus[j+1];
stus[j+1]=tempStu;
}
}
}
}
//打印
void printStudentInfor(Student stu){//结构体名
printf("name%s,number%d,score%f\n",stu.name,stu.number,stu.score);
}
int main(int argc, const char * argv[])
{
Student students[5]={{1101,"duke",75},{1102,"mike",56},{1103,"alisa",95},{1104,"Lucy",65},{1105,"Lily",88}};//类型,数组名
bubbleSort(students, 5);
printStudentInfor(students[0]);
return 0;
}
字符串存储,字符串数组
y往下为正,x右为正,以*打印出矩形(2,2,10,10)
1.有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序。
2.NBRect结构体描述二维坐标系中得一个矩形,两个成员分别为NBPoint和NBSize结构体,定义一个NBRectMake(float x, float y, float w, float h)返回值为NBRect类型, NBRect变量来控制打印输出一个矩形。
void printRect(NBRect myrect)
{
int x=myrect.m_point.m_x;
int y=myrect.m_point.m_y;
int w=myrect.m_size.width;
int h=myrect.m_size.heigth;
for (int i=1; i<y+h; i++) {
for (int j=1; j<x+w; j++) {
if ((i==y||i==y+h-1)&&j>=x) {
printf("*");
}
else if((j==x||j==x+w-1)&&i>=y){
printf("*");
}
else printf(" ");
}
printf("\n");
}
}
3、定义一个结构体变量(包含年月日),计算该日在本年中为第几天?要求写一个days函数。参数是此结构体类型的变量,返回值是整数。