#import <Foundation/Foundation.h>
struct student{
char name[20];
int age;
float score;
char address[40];
};
typedef struct student Student;
//(1)写一个函数打印学生的信息(传一个学生进去,打印信息);
void printStudents(Student stu );
void printStudents(Student stu )
{
printf("%s %d %f %s",stu.name,stu.age,stu.score,stu.address);
}
//(2)写一个函数打印所有学生信息(把装学生的数组传进去,全部打印);
void printAllStudents(Student array[],int count );
void printAllStudents(Student array[] ,int count )
{
for (int i=0; i<count ; i++) {
printStudents(array[i]);
}
}
//(3)写一个函数,找出数组中四个学生成绩最高的一个并打印他的信息
void findStudentOfHightScore(Student stuarr[],int count );
void findStudentOfHightScore(Student stuarr[],int count )
{
Student maxarr={0};
for (int i=0; i<count ; i++) {
if (maxarr.score<stuarr[i].score) {
maxarr = stuarr[i];
}
}
printStudents(maxarr);
}
//(4)写一个函数对数组中的四个学生按照他们的成绩升序排列
void sortStudentScore(Student array[],int count);
void sortStudentScore(Student array[],int count)
{
Student temp={0};
for (int i=0; i<count-1; i++) {
for (int j =0; j<count-1-i; j++) {
if (array[j].score>array[j+1].score) {
temp = array[j];
array[j]=array[j+1];
array[j+1]=temp ;
}
}
}
printAllStudents(array, count );
}
//(5)写一个函数对数组中的学生按照年龄降序排列
void sortStudentAge(Student array[],int count);
void sortStudentAge(Student array[],int count)
{
Student temp1 = {0};
for (int i=0; i<count-1 ; i++) {
for (int j=0; j<count-1-i; j++) {
if (array[j].age<array[j+1].age) {
temp1 = array[j];
array[j]=array[j+1];
array[j+1]=temp1 ;
}
}
}
printAllStudents(array, count );
}
int main(int argc,const char * argv[]) {
/*
创建一个学生结构体(名字,年龄,分数,家庭住址),别名
创建4个学生加入到数组中
写一个函数打印学生的信息(传一个学生进去,打印信息);
写一个函数打印所有学生信息(把装学生的数组传进去,全部打印);
写一个函数,找出数组中四个学生成绩最高的一个并打印他的信息
写一个函数对数组中的四个学生按照他们的成绩升序排列
写一个函数对数组中的学生按照年龄降序排列
*/
Student stu1 = {"小敏",18,88,"广东"};
Student stu2 = {"小明",15,98,"北京"};
Student stu3 = {"小芳",21,81,"陕西"};
Student stu4 = {"小文",19,94,"上海"};
Student stus[4] = {stu1,stu2,stu3,stu4};
printStudents(stu1);
printf("\n..................................\n");
printAllStudents(stus,4);
printf("\n..................................\n");
findStudentOfHightScore(stus,4);
printf("\n..................................\n");
sortStudentScore(stus,4);
printf("\n..................................\n");
sortStudentAge(stus,4);
return 0;
}