C Language Foundation _teacher

Source: Internet
Author: User

1). Code in H

#import <Foundation/Foundation.h>typedefstructteacher{Charname[ -]; Chargender; intAge ; intNumber ; floatscore;} Teacher;voidPrintteacher (Teacher *teacher); typedef BOOL (*comparefunctionpointer) (Teacher teacher1,teacher teacher2);voidSortteachers (Teacher teacher[],intcount,comparefunctionpointer CFP);voidPrintteachers (Teacher *teacher,intcount); BOOL comparenamebyascending (Teacher teacher1,teacher teacher2); BOOL comparenamebydescending (Teacher teacher1,teacher teacher2); BOOL comparenumberbyascending (Teacher teacher1,teacher teacher2); BOOL comparenumberbydescending (Teacher teacher1,teacher teacher2); BOOL comparescorebyascending (Teacher teacher1,teacher teacher2); BOOL comparescorebydescending (Teacher teacher1,teacher teacher2);voidPrintmaleteacher (Teacher *teachers,intcount);voidPrintfemaleteacher (Teacher *teacher,intcount);enumfunctionname{comparenamebyascending=1, Comparenamebydescending, comparenumberbyascending, comparenumberbydescending, comparescorebyascending, Comparescorebydescending, Printfemaleteacher, Printmaleteacher,};

2). M in Code

#import"Teacher.h"voidPrintteacher (Teacher *teacher) {printf ("%s%c%d%d%.2f\n",teacher->name,teacher->gender,teacher->age,teacher->number,teacher->score);} typedef BOOL (*comparefunctionpointer) (Teacher teacher1,teacher teacher2);voidSortteachers (Teacher teacher[],intcount,comparefunctionpointer CFP) {     for(inti =0; I < count-1; i++) {         for(intj =0; J < Count-1-I.; J + +) {            if(CFP (teacher[j],teacher[j+1]) {Teacher temp=Teacher[j]; TEACHER[J]= teacher[j+1]; Teacher[j+1] =temp; }}} printteachers (Teacher,5);}voidPrintteachers (Teacher *teacher,intcount) {     for(inti =0; I < count; i++) {printf ("%s%c%d%d%.2f\n", Teacher[i].name,teacher[i].gender,teacher[i].age,teacher[i].number,teacher[i].score); }}bool comparenamebyascending (Teacher teacher1,teacher teacher2) {returnstrcmp (Teacher1.name, Teacher2.name) >0;} BOOL comparenamebydescending (Teacher teacher1,teacher teacher2) {returnstrcmp (Teacher1.name, Teacher2.name) <0;} BOOL comparenumberbyascending (Teacher teacher1,teacher teacher2) {returnTeacher1.number >Teacher2.number;} BOOL comparenumberbydescending (Teacher teacher1,teacher teacher2) {returnTeacher1.number <Teacher2.number;} BOOL comparescorebyascending (Teacher teacher1,teacher teacher2) {returnTeacher1.score >Teacher2.score;} BOOL comparescorebydescending (Teacher teacher1,teacher teacher2) {returnTeacher1.score <Teacher2.score;}voidPrintmaleteacher (Teacher *teachers,intcount) {      for(inti =0; I < count; i++) {        if(Teachers[i].gender = ='F') {Printteacher (&Teachers[i]); }     }}voidPrintfemaleteacher (Teacher *teacher,intcount) {     for(inti =0; I < count; i++) {        if(Teacher[i].gender = ='m') {Printteacher (&Teacher[i]); }    }}

3). The code in main

#import <Foundation/Foundation.h>#import"Teacher.h"intMainintargcConst Char*argv[]) {Teacher teacher[5] = {{"ABC",'m', -, -, -},                           {"def",'F', *, -, the},                           {"Hji",'m', -, the, +},                           {"ASD",'m', to, ., the},                           {"XYZ",'F', -, -, -}};  while(true) {printf ("Input 1: implementation by name in ascending order \ n"); printf ("Input 2: implementation by name in descending order \ n"); printf ("Input 3: implementation in ascending order by employee number \ n"); printf ("Input 4: Implement descending order by employee number \ n"); printf ("Input 5: Implementation is ranked in ascending order by rating \ n"); printf ("Input 6: Achieve descending order by rating \ n"); printf ("Input 7: Achieve output all female teachers \ n"); printf ("Input 8: Achieve output all the male teachers \ n"); printf ("==============================\n"); printf ("Please enter the number that corresponds to the implementation function: \ n"); intA; scanf ("%d",&a); Switch(a) { Casecomparenamebyascending: {sortteachers (teacher,5, comparenamebyascending); }                 Break;  Casecomparenamebydescending: {sortteachers (teacher,5, comparenamebydescending); }                 Break;  Casecomparenumberbyascending: {sortteachers (teacher,5, comparenumberbyascending); }                 Break;  Casecomparenumberbydescending: {sortteachers (teacher,5, comparenumberbydescending); }                 Break;  Casecomparescorebyascending: {sortteachers (teacher,5, comparescorebyascending); }                 Break;  Casecomparescorebydescending: {sortteachers (teacher,5, comparescorebydescending); }                 Break;  Caseprintfemaleteacher: {printmaleteacher (teacher,5); }                 Break;  Caseprintmaleteacher: {printfemaleteacher (teacher,5); }                 Break; default: {printf ("Sorry, the number you entered does not have a corresponding function, please re-enter \ n"); }                 Break; }    }        return 0;}

C Language Foundation _teacher

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.