Code case (struct, function pointer, pointer function, bubble sort)

Source: Internet
Author: User
Typedef struct {char name [20]; int age; float score;} Stu; # import <Foundation/Foundation. h> // void sortbyname (Stu * 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) {Stu temp = * (p + J); * (p + J) = * (p + J + 1 ); * (p + J + 1) = temp ;}}// age void sortbyage (Stu * P, int count) {for (INT I = 0; I <count-1; I ++) {for (Int J = 0; j <count-1-I; j ++) {If (p + J) -> age> (p + J + 1)-> Age) {Stu temp = * (p + J); * (p + J) = * (p + J + 1); * (p + J + 1) = temp ;}}// score void sortbyscore (Stu * P, int count) {for (INT I = 0; I <count-1; I ++) {for (Int J = 0; j <count-1-I; j ++) {If (p + J)-> score> (p + J + 1)-> score) {Stu temp = * (p + J); * (p + J) = * (p + J + 1); * (p + J + 1) = temp ;}}} void output (Stu * P, int count) {for (INT I = 0; I <count; I ++) {printf ("name = % s, age = % d, score = %. 2f \ n ", (p + I)-> name, (p + I)-> age, (p + I)-> score );}} typedef void (* Fun) (Stu * P, int count); void student (fun func, Stu * P, int count) {func (p, count );} int main (INT argc, const char * argv []) {Stu STU [5] ={{ "Zhang", 20,80 },{ "Wang }, {"Li", },{ "Zhao", },{ "Liu", 20,89 }}; Stu * P = NULL; P = Stu; student (output, p, 5); student (sortbyname, P, 5); student (output, P, 5); // output (p, 5); // sortbyname (p, 5); // output (p, 5); Return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.