C9_作业

1.(* *)写一个函数交换两个结构体变量

***MyFuncation.h***

typedef struct exChange {
    char name[20];
    int num;
}exChange;

void exchange (exChange *p1, exChange *p2);
***MyFuncation.m***

void exchange (exChange *p1, exChange *p2) {
    // 注意传址,不能传值!
    exChange temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}
***main.m***

    exChange per1 = {"zhangsan", 15};
    exChange per2 = {"lsii", 12};
    exChange *p1 = &per1;
    exChange *p2 = &per2;
    exchange(p1, p2);
    printf("%s ,%d\n", p1->name, p1->num);
    printf("%s ,%d\n", p2->name, p2->num);

2.

***MyFuncation.h***

typedef struct student {
    char name[20];
    int num;
    float score;
} student;

void printStu (student *p); // 打印信息函数
***MyFuncation.m***

void printStu (student *p) {
    printf("学生姓名: %s 学生学号: %d 学生成绩: %g\n", p->name, p->num, p->score );
}
***main.n***

    student stu1 = {"zhangsan", 1, 70};
    student stu2 = {"lisi", 2, 80};
    student stu3 = {"wangermazi", 3, 77};
    student stu4 = {"zhaosi", 5, 66};
    student stu5 = {"wangwu", 4, 90};
    student stu[5] = {stu1, stu2, stu3, stu4, stu5};
    student *p = stu;

(1).(* *)有一学生数组写一函数打印出指定分数段的学生信息

***MyFuncation.h***

void Score(student *p, int count, int floor, int upper);
***MyFuncation.m***

void Score(student *p, int count, int floor, int upper) {
    for (int i = 0; i < count; i++) {
        if ((p + i)->score >= floor && (p + i)->score <= upper) {
            printStu(p + i);
        }
    }
}
***main.m***

Score(p, 5, 75, 80);

(2).(* *)有一学生数组,包含5个学生,写一个函数,对学生排序(按学号 从小到大),使用结构体指针操作数组元素.

***MyFuncation.h

void numSort(student *p, int count);
***MyFuncation.m***

void numSort(student *p, int count) {
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if ((p + j)->num > (p + j + 1)->num) {
                student temp = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }
}
***main.m***

    numSort(p, 5);
    for (int i = 0; i < 5; i++) {
        printStu(p + i);
    }

(3).(* *)有一学生数组,包含5个学生,写一个函数,对学生排序(按姓名 从小到大),使用结构体指针操作数组元素

***MyFuncation.h***

void nameSort(student *p, int count);
***Myfuncation.m***

void nameSort(student *p, int count) {
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if (strcmp((p + j)->name, (p + j + 1)->name) > 0) {
                student temp = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }
}
***main.m***

    nameSort(p, 5);
    for (int i = 0; i < 5; i++) {
        printStu(p + i);
    }

(4).(* *)有一学生数组,包含5个学生,写一个函数,对学生排序(按分数 从小到大),使用结构体指针操作数组元素.

***MyFuncation.h***

void scoreSort(student *p, int count);
***MyFuncation.m***

void scoreSort(student *p, int count) {
    for (int i = 0; i < count; i++) {
        for (int j =0; j < count - 1 - j; j++) {
            if ((p + j)->score > (p + j + 1)->score) {
                student temp = *(p + j);
                *(p + j) = *(p + j + 1);
                *(p + j + 1) = temp;
            }
        }
    }
}
***mian.m***

 scoreSort(p, 6);
    for (int i = 0; i < 5; i++) {
        printStu(p + i);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值