首页 > 代码库 > C语言--结构体

C语言--结构体


#import <Foundation/Foundation.h>
typedef struct students{
    
    
}Stu;
int main(int argc, const char * argv[])
{
    //结构体,里面的是成员(变量)
    struct teacher {
      
        char name[30];
        char sex;
        int  age;
        char course[30];
    };
    //struct teacher类型 a变量名
    struct teacher a = {"cuilaoshi",'m',18,"language C"};
//    Teacher c = {"cuilaoshi",'m',18,"language C"};
    
    
    struct cup {
        float price;//价钱
        char color[30];//颜色
        int capacity;//容量
    };
    //struct cup类型 b变量名 {20.5,"red",500}初值
    struct cup b = {20.5,"red",500};
    
    //使用typedef 重新定义类型
    //1.定义结构体的同时,就取别名
    //2.先定义结构体,再取别名
    typedef struct teacher Teacher;
    Teacher c = {"cuilaoshi",'m',18,"language C"};
    //类跟结构体很相似
    // 访问结构体成员表示,结构体变量.成员  a.name
    strcpy(a.course, "Objective-C");
    a.sex='f';
    //打印结构体,可以在main函数,也可以定义函数打印
    printf("%s",a.name);
    printf(" %c ",a.sex);
    printf(" %d ",a.age);
    printf(" %s",a.course);
    //匿名结构体
    
    //结构体赋值
    //结构体可以直接赋值
    
    //有三个学生,编程找出分数最高者以及年龄最小者
    //字符串赋值拷贝
    
    return 0;
}




#import <Foundation/Foundation.h>
#import "Student.h"
#import "MyRect.h"

int main(int argc, const char * argv[])
{
    //    有三个学?生,编程找出分数最?高者以及
    //    年龄最?小者
#pragma mark----------------结构体数组
    stu s[3]={
        {"zhangsan",23,90,{1990,9,28}},
        {"zuoyoudong",25,88,{1991,9,20}},
        {"fanghao",24,89,{1992,8,8}}
    };
    
    stu max = s[0];
#pragma mark----------------结构体中求最大值与最小值
    //注意定义最小,不要初始值为0,否则最小总是0,可以定义为第一个元素s[0]
    stu min = s[0];
    //i可以从1开始,因为定义时,已经假设第一个元素s[0]最小或最大,然后让它去跟数组其它元素(第二个,第三个去计较就行了)
    for (int i = 0; i<3; i++) {
        if (min.age>s[i].age) {
            min.age=s[i].age;
        }
        if (max.score<s[i].score) {
            max.score = s[i].score;
        }
    }
#pragma mark----------------结构体中冒泡排序
//    for (int i = 0; i<3-1; i++) {
//        for (int j =0  ; j<3-1-i; j++) {
//            if (s[j].score<s[j+1].score) {
//                stu temp = {0};
//                temp = s[j];
//                s[j] = s[j+1];
//                s[j+1] = temp;
//            }
//        }
//    }
    bubbleSort(s, 3);
    printfStu(s, 3);
    
//    for (int i = 0; i<3; i++) {
//        printf("%s %d %.2f \n",s[i].name,s[i].age,s[i].score);
//    }
//    printf("分数最?高者是:%s,年龄最?小者是:%s",max.name,min.name);
    
    
    
    
#pragma mark----------------结构体中三目运算符求最大值,最小值
//    stu s = {"zuoyoudong fanghao zhangsan",{20,21,18},90.5};
//    stu s1 = {"zhangsan",23,90,{1990,9,28}};
//    stu s2 = {"zuoyoudong",25,88,{1991,9,20}};
//    stu s3 = {"fanghao",24,89,{1992,8,8}};
//    //点语法,可以读成的
//    printf("%d",s2.birthday.month);
//
////    //分数最高者
//    stu max = {0};
//    max = s1.score>s2.score?s1:s2;
//    max = max.score>s3.score?max:s3;
#pragma mark----------结构体占多少内存空间
//    //按最大类型的整数倍分配空间sizeof
//    printf("%s %lu",max.name,sizeof(stu));
////    //年龄最小者
//    stu min = {0};
//    min = s1.age<s2.age?s1:s2;
//    min = min.age<s3.age?min:s3;
//    printf(" %s",min.name);
    
    
    Lpoint p = LpointMake(100, 50);
    LSize si = LpsizeMake(100, 80);
    
    

    return 0;
}
#import "Student.h"
#pragma mark----------结构体声明写在.h文件中
struct date {
    int year;
    int month;
    int day;
};
typedef struct date Date;

typedef struct students{
    char name[20];//姓名
    int age;//年龄
    float score;//分数
    Date birthday;//生日
}stu;
void bubbleSort(stu s[],int count);
void printfStu(stu s[],int count);

 "Student.m"
void bubbleSort(stu s[],int count)
{
    for (int i = 0; i<count-1; i++) {
        for (int j = 0; j<count-1-i; j++) {
            if (s[j].score<s[j+1].score ) {
                stu temp=s[j];
                s[j]=s[j+1];
                s[j+1]=temp;
            }
        }
    }
    
}
void printfStu(stu s[],int count)
{
    for (int i = 0; i<count; i++) {
        printf("%s,%d,%.2f\n",s[i].name,s[i].age,s[i].score);
    }
}

<pre name="code" class="objc">#import "MyRect.h"
typedef struct Lpoint {
    float x;
    float y;
}Lpoint;

//构建一个LPoint
Lpoint LpointMake(float x,float y);
//在iOS里,你会见到 CGPoint CGPointMake(CGFloat x,CGFloat y);
//其中CGFloat是typedef出来的, typedef float CGFloat;


//判断两个点是否相同
BOOL LPointisEqual(Lpoint p1,Lpoint p2);

typedef struct LSize
{
    float width;
    float hight;
}LSize;

LSize LpsizeMake(float width, float height);

 "MyRect.m"
//构建一个LPoint
Lpoint LpointMake(float x,float y)
{
    Lpoint p = {x,y};
    return p;
}
//判断两个点是否相同
BOOL LPointisEqual(Lpoint p1,Lpoint p2)
{
    if (p1.x == p2.x && p1.y == p2.y) {
        return YES;
        //不写else相当于必走
    }else{
        return NO;
    }
}
LSize LpsizeMake(float width, float height)
{
    LSize siz = {width,height};
    return siz;
}