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);
}