1.创建一个Size结构体,包含width,height两个变量.
并写以下函数:
函数1,判断两个size是否等宽.
函数2,判断两个size是否在等高.
函数3,判断两个size是否相等.
***MyFuncation.h***
typedef struct size {
int width;
int height;
} size;
BOOL equalWidth(size size1, size size2);
BOOL equalHeight(size size1, size size2);
BOOL equal(size size1, size size2);
***MyFuncation.m***
BOOL equalWidth(size size1, size size2) {
return size1.width == size2.width;
}
BOOL equalHeight(size size1, size size2) {
return size1.height == size2.height;
}
BOOL equal(size size1, size size2) {
return (size1.height == size2.height) && (size1.width == size2.width);
***main.m***
size size1 = {12,10};
size size2 = {15,10};
if (equalWidth(size1, size2)) {
printf("宽相等\n");
} else {
printf("宽不相等\n");
}
if (equalHeight(size1, size2)) {
printf("高相等\n");
} else {
printf("高不相等\n");
}
if (equal(size1, size2)) {
printf("相等\n");
} else {
printf("不相等\n");
}
2.定义一个结构体,有两个成员变量,一个是姓名,一个是速度,然后计算两人相距100公里,第一个人的速度4公里/h 第二个人的速度6公里/h.
函数1, 两人相向而行,相遇的时候第一个人走了多远.
函数2, 第一个人先走40公里,第二个人走多远能追上第一个人.
***MyFuncation.h***
typedef struct people {
char name[20];
int speed;
} people;
int firstPeopleMoved(people p1, people p2, int distance);
int chaseFirstPeople(people p1, people p2, int distance, int firstMoved);
***MyFuncation.m***
int firstPeopleMoved(people p1, people p2, int distance) {
int time = distance / (p1.speed + p2.speed);
int p1Moved = p1.speed * time;
return p1Moved;
}
int chaseFirstPeople(people p1, people p2, int distance, int firstMoved) {
int time = (distance + firstMoved) / (p2.speed - p1.speed);
int p2Moved = p2.speed * time;
return p2Moved;
}
***main.m***
people p1 = {"littleRed", 4};
people p2 = {"littleBlue", 6};
int temp = firstPeopleMoved(p1, p2, 100);
printf("相遇时第一个人走了%d公里\n", temp);
int temp2 = chaseFirstPeople(p1, p2, 100, 40);
printf("第二个人走%d公里能追上第一个人\n", temp2);
3.某班有 5 个学生,三门课。分别编写 3 个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名.
***MyFuncation.h***
typedef struct student {
char stuName[20];
long stuNum;
int score1;
int score2;
int score3;
} Student;
void printStu(Student stu);
void avgScore(Student stu1, Student stu2, Student stu3, Student stu4, Student stu5, int num);
void notPass(Student stu);
void goodStu(Student stu);
***MyFuncation.m***
void printStu(Student stu) {
printf("姓名: %s 学号: %ld\t", stu.stuName, stu.stuNum);
}
void avgScore(Student stu1, Student stu2, Student stu3, Student stu4, Student stu5, int num) {
float avg1 = (stu1.score1 + stu2.score1 + stu3.score1 + stu4.score1 + stu5.score1) / num;
printf("第一门的平均成绩为: %g\n", avg1);
float avg2 = (stu1.score2 + stu2.score2 + stu3.score2 + stu4.score2 + stu5.score2) / num;
printf("第二门的平均成绩为: %g\n", avg2);
float avg3 = (stu1.score3 + stu2.score3 + stu3.score3 + stu4.score3 + stu5.score3) / num;
printf("第三门的平均成绩为: %g\n", avg3);
}
void notPass(Student stu) {
int count = 0;
if (stu.score1 < 60) {
count++;
}
if (stu.score2 < 60) {
count++;
}
if (stu.score3 < 60) {
count++;
}
if (count >= 2) {
printStu(stu);
}
}
void goodStu(Student stu) {
int avg = (stu.score1 + stu.score2 + stu.score3) / 3.0;
if (avg >= 85 && avg <= 90) {
printStu(stu);
}
}
***main.m***
Student stu1 = {"zhansan", 1234, 65, 70, 90};
Student stu2 = {"lisi", 2314, 50, 66, 40};
Student stu3 = {"wangermazi", 4231, 70, 59, 90};
Student stu4 = {"zhaowu", 3241, 90, 90, 90};
Student stu5 = {"laoliu", 1432, 21, 22, 55};
Student stu[5] = {stu1, stu2, stu3, stu4, stu5};
avgScore(stu1, stu2, stu3, stu4, stu5, 5);
printf("有两门以上不及格的学生为: ");
for (int i = 0; i < 5; i++) {
notPass(stu[i]);
} printf("\n");
printf("三门成绩在85 - 90的学生为: ");
for (int i = 0; i < 5; i++) {
goodStu(stu[i]);
} printf("\n");