0% found this document useful (0 votes)
1 views

函数指针练习

The document contains C code that demonstrates the use of function pointers for sorting a student structure by age and score. It includes functions to populate an array with random values and to compare students based on their attributes. The main function showcases sorting and printing the student data using bubble sort algorithm.

Uploaded by

lijiade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

函数指针练习

The document contains C code that demonstrates the use of function pointers for sorting a student structure by age and score. It includes functions to populate an array with random values and to compare students based on their attributes. The main function showcases sorting and printing the student data using bubble sort algorithm.

Uploaded by

lijiade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>

#if 0
typedef int (*func)(int, int);

int max(int x, int y)


{
return (x > y ? x : y);
}

int main(void)
{
int (*p)(int, int) = max;

int a = 2;
int b = 3;

printf("max=%d\n", p(2, 3));


printf("max=%d\n", max(2, 3));
}
#endif

#if 0
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
for (size_t i = 0; i < arraySize; i++)
{
array[i] = getNextValue();
}
}

int getNextRandomValue(void)
{
return rand();
}

int main(void)
{
int myArray[10];

srand(time(0));
populate_array(myArray, 10, getNextRandomValue);

for (size_t i = 0; i < 10; i++)


{
printf("%d ", myArray[i]);
}
printf("\n");

return 0;
}
#endif

typedef struct student


{
char name[20];
int age;
float score;
} Student;

int CmpByAge(Student s1, Student s2)


{
if (s1.age > s2.age)
return 1;
else if (s1.age < s2.age)
return -1;
else
return 0;
}

int CmpByScore(Student s1, Student s2)


{
if (s1.score > s2.score)
return 1;
else if (s1.score < s2.score)
return -1;
else
return 0;
}

void SortStudent(Student* array, int n, int (*p) (Student, Student))


{
Student tmp;
int flag = 0;

for (int i = 0; i < n - 1 && flag == 0; i++)


{
flag = 1;
for (int j = 0; j < n - i - 1; j++)
{
if (p(array[j], array[j + 1]) > 0)
{
tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
flag = 0;
}

}
}
}

void PrintStu(Student* array, int n)


{
for (int i = 0; i < n; i++)
{
printf("%s %d %.2f\n", array[i].name, array[i].age, array[i].score);
}
printf("\n");
}

int main(void)
{
Student s1 = { "Jade", 25, 80 };
Student s2 = { "paul", 26, 78 };
Student s3 = { "mike", 21, 88 };
Student stuArray[3] = { s1, s2, s3 };

SortStudent(stuArray, 3, CmpByAge);
PrintStu(stuArray, 3);

SortStudent(stuArray, 3, CmpByScore);
PrintStu(stuArray, 3);
}

You might also like