About dynamic sequencing and the use of structs

Source: Internet
Author: User
Tags sorted by name strcmp

Topic:

First, the project needs:

  1. Create a pair of files Teacher.h teacher.m. 1 min
  2. In Teacher.h, a teacher structure is defined, and member variables include: name, gender, age, employee number, rating. 2 min
  3. Declares a function that prints Teacher, void Printteacher (Teacher * Teacher). 1 points to achieve Part 4 points
  4. Defines a function pointer type, typedef BOOL (*comparefunctionpointer) (Teacher teacher1, Teacher teacher2); 1 points
  5. Declares an implementation of the teacher array sorting function, void Sortteachers (Teacher teacher[], int count, comparefunctionpointer CFP); 1 cent implementation Part 4 points
  6. Declares a function that prints all the teachers in an array, void printteachers (Teacher * teachers, int count), 1 points to achieve Part 4 points
  7. Two functions that compare Teacher names are declared separately: bool Comparenamebyascending (Teacher teacher1, Teacher teacher2), (ascending mode), BOOL Comparenamebydescending (Teacher teacher1, Teacher teacher2);(descending mode). 2 points to achieve part 8 points
  8. Declare two functions that compare Teacher employee numbers separately: bool Comparenumberbyascending (Teacher teacher1, Teacher teacher2), (ascending), bool Comparenumberbydescending (Teacher teacher1, Teacher teacher2);(descending method) 2 cent implementation part 8 points
  9. Declare two functions that compare Teacher employee ratings: bool Comparescorebyascending (Teacher teacher1, Teacher teacher2), (ascending mode), BOOL Comparescorebydescending (Teacher teacher1, Teacher teacher2);(descending method) 2 cent implementation part 8 points

10. Declare a function that outputs all male teachers in the teacher's array, void Printmaleteacher (Teacher * teachers, int count), 1 points to achieve Part 4 points

11. Declare a function that outputs all female teachers in an array of teachers, void Printfemaleteacher (Teacher * Teacher, int count), 1 points to achieve Part 4 points

12. Declare an enumeration functionname, where each enumeration value corresponds to the function number one by one entered by the user from the console. For example: When you enter 1 o'clock from the console, the implementation is sorted by name in ascending order. 2 cent Realization Part 8 points (above declaration part total 17 cent realization part is 52 points)

13. In TEACHER.M, all functions described above are implemented.

The above declaration part and the realization part are altogether 69 points

15. In the main.m file, ask:

(1) Requirements in the console, the output of user operating instructions. 3 min

(2) Create an teacher array that contains 5 data of the teacher struct type. (Teacher's name uses pinyin, does not use Chinese characters; gender uses ' m ' to denote male, ' F ' for female, rating using float type. ) 7 min

(3) According to the console input number, control the output result:

1) Use the while loop to implement the re-entry function. 7 min

2) Use Switch...case implementation: According to the console input between the 1~8, the teacher array is sorted differently and output results. The value of case uses the enumeration value corresponding to the 1~8 number. 6 min

3) Enter the number 1~6, sort the teacher array, use sortteachers functions, compare functions to sort, and then print out all teacher information using printteachers function after sorting. 4 min

4) input digital 7~8, according to gender screening teachers, respectively, using Printmaleteacher, Printfemaleteacher function output results. 2 min

5) If you enter a number other than 1~8, the output "Sorry, the number you entered does not have a corresponding function, please re-enter!!!!". 2 min

Programming implementation

MAIN.M file

int main (int argc, const char * argv[])

{

Teacher Teac1 = {"Xiaomi", ' m ', 25,15,98.5};

Teacher Teac2 = {"Yunjie", ' F ', 18,11,97.5};

Teacher teac3 = {"Huige", ' m ', 35,1,99};

Teacher teac4 = {"Bobo", ' m ', 29,3,95.5};

Teacher teac5 = {"Cuige", ' m ', 27,6,96.5};

Teacher Teacarray[5] = {TEAC1,TEAC2,TEAC3,TEAC4,TEAC5};

Teacher *p = &teac1; Validation function Pritteacher

Pritteacher (P);

Comparefunctionpointer CFP;

CFP = comparefunctionstudent;

Printteachers (teacarray,5); To validate a function that prints an array of structural bodies

Enum functionname

{

comparenamebyascending = 1,

Comparenamebydescending,

Comparenumberbyascending,

Comparenumberbydescending,

Comparescorebyascending,

Comparescorebydescending,

Printmaleteacher,

Printfemaleteacher

};

int putchoicenumber = 0;

bo=

printf ("Input 1: implementation in ascending order by name \ n Input 2: implementation in descending order by name \ n Input 3: implementation in ascending order by employee number \ n Input 4: implementation in descending order by employee number \ n Input 5: Achieve ascending order by rating \ N Input 6: implementation descending by rating \ n Input 7: Achieve output all female teacher \ n Input 8: Achieve output All male teacher \n==============================\n please input the corresponding number of the implementation function: ");

scanf ("%d", &putchoicenumber);

CFP = Comparenum (Putchoicenumber);

Sortteacher (teacarray,4, CFP); Using struct-body implementations

Printteachers (teacarray,4); Using struct-body implementations

Findfunction (Putchoicenumber,teacarray); Using enumerations to implement

}while (putchoicenumber<1| | PUTCHOICENUMBER>8);

return 0;

}

Function.h file

typedef struct teacher{

Char name[30];

char sex;

int age;

int number;

Float score;

}teacher;

void Pritteacher (Teacher *teacher); Print personal Information

BOOL comparefunctionstudent (Teacher teacher1,teacher teacher2);

typedef BOOL (*comparefunctionpointer) (Teacher teacher1,teacher teacher2);

void Sortteacher (Teacher teacher[],int count, Comparefunctionpointer CFP); Rank two teachers by rating

void Printteachers (Teacher *teachers,int count); Print information about all the teachers in the array

BOOL comparenamebyascending (Teacher teacher1,teacher teacher2); Sort by name Ascending

BOOL comparenamebydescending (Teacher teacher1,teacher teacher2);//Sort by name descending

BOOL comparenumberbyascending (Teacher teacher1,teacher teacher2);//Sort Ascending by number

BOOL comparenumberbydescending (Teacher teacher1,teacher teacher2);//Sort by number Descending

BOOL comparescorebyascending (Teacher teacher1,teacher teacher2);//Sort by score ascending

BOOL comparescorebydescending (Teacher teacher1,teacher teacher2);//sorted by score descending order

void Printmaleteacher (Teacher *teachers,int count);//output All male teacher's function

void Printfemaleteacher (Teacher *teachers,int count);//output All female teacher's function

typedef struct functionnamestruct{

int putnum;

Comparefunctionpointer Choicenum;

}functionnamestruct;

Comparefunctionpointer comparenum (int);

void findfunction (int, Teacher *);

FUNCTION.M file

#import "Teacher.h"

void Pritteacher (Teacher *teacher)

{

printf ("Name:%s\t Gender:%c\t Age:%d\t Employee Code:%d\t Rating:%.2f\n",teacher->name,teacher->sex,teacher->age,teacher-> Number,teacher->score);

}

Sort function

void Sortteacher (Teacher teacher[],int count, Comparefunctionpointer CFP)

{

for (int i = 0; i < count-1; i++) {

for (int j = 0; J < Count-1-i; J + +) {

if (CFP (teacher[j],teacher[j+1))) {

Teacher temp = teacher[j+1];

TEACHER[J+1] = Teacher[j];

TEACHER[J] = temp;

}

}

}

}

BOOL comparefunctionstudent (Teacher teacher1,teacher teacher2)

{

Return teacher1.score>teacher2.score;

}

Print array

void Printteachers (Teacher *teachers,int count)

{

for (int i = 0; i < count; i++) {

printf ("Name:%s\t Gender:%c\t Age:%d\t Employee Code:%d\t Rating:%.2f\n", Teachers[i].name,teachers[i].sex,teachers[i].age,teachers[i]. Number,teachers[i].score);

}

}

Sort by name Ascending

BOOL comparenamebyascending (Teacher teacher1,teacher teacher2)

{

Return strcmp (Teacher1.name, Teacher2.name) >0;

}

Sort by name Descending

BOOL comparenamebydescending (Teacher teacher1,teacher teacher2)

{

Return strcmp (Teacher1.name, Teacher2.name) <0;

}

Sort Ascending by number

BOOL comparenumberbyascending (Teacher teacher1,teacher teacher2)

{

return teacher1.number> Teacher2.number;

}

Sort by number Descending

BOOL comparenumberbydescending (Teacher teacher1,teacher teacher2)

{

return teacher1.number< Teacher2.number;

}

Sort by score Ascending

BOOL comparescorebyascending (Teacher teacher1,teacher teacher2)

{

return teacher1.score> Teacher2.score;

}

Sort by score Descending

BOOL comparescorebydescending (Teacher teacher1,teacher teacher2)

{

return teacher1.score< Teacher2.score;

}

Functions for outputting all male teachers

void Printmaleteacher (Teacher *teachers,int count)

{

for (int i = 0; i < count; i++) {

if (Teachers[i].sex = = ' m ') {

Pritteacher (Teachers+i);

}

}

}

function to output all female teachers

void Printfemaleteacher (Teacher *teachers,int count)

{

for (int i = 0; i < count; i++) {

if (Teachers[i].sex = = ' F ') {

Pritteacher (Teachers+i);

}

}

}

Comparefunctionpointer comparenum (int a)

{

Functionnamestruct choicenum1={1,comparenamebyascending};

Functionnamestruct choicenum2={2,comparenamebydescending};

Functionnamestruct choicenum3={3,comparenumberbyascending};

Functionnamestruct choicenum4={4,comparenumberbydescending};

Functionnamestruct choicenum5={5,comparescorebyascending};

Functionnamestruct choicenum6={6,comparescorebydescending};

Functionnamestruct Choicenum[6] = {CHOICENUM1,CHOICENUM2,CHOICENUM3,CHOICENUM4,CHOICENUM5,CHOICENUM6};

for (int i = 0; i < 6; i++) {

if (Choicenum[i].putnum = = a) {

return choicenum[i].choicenum;

}

}

return choicenum[4].choicenum;

}

void findfunction (int a,teacher *teacarray)

{

Switch (a) {

Case 1:

CFP = comparenamebyascending;

Sortteacher (teacarray,5, comparenamebyascending);

Printteachers (teacarray,5);

Break

Case 2:

CFP = comparenamebydescending;

Sortteacher (teacarray,5, comparenamebydescending);

Printteachers (teacarray,5);

Break

Case 3:

CFP = comparenumberbyascending;

Sortteacher (teacarray,5, comparenumberbyascending);

Printteachers (teacarray,5);

Break

Case 4:

CFP = comparenumberbydescending;

Sortteacher (teacarray,5, comparenumberbydescending);

Printteachers (teacarray,5);

Break

Case 5:

CFP = comparescorebyascending;

Sortteacher (teacarray,5, comparescorebyascending);

Printteachers (teacarray,5);

Break

Case 6:

CFP = comparescorebydescending;

Sortteacher (teacarray,5, comparescorebydescending);

Printteachers (teacarray,5);

Break

Case 7:

Printfemaleteacher (teacarray,5);

Break

Case 8:

Printmaleteacher (teacarray,5);

Break

Default

printf ("Sorry, the number you entered does not have a corresponding function, please re-enter!!!!" \ n ");

Break

}

}

About dynamic sequencing and the use of structs

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.