C Introductory Section 11th

Source: Internet
Author: User
Tags strcmp

int maxValue (int x,int y)
{
return x > Y? x:y;
}

void Printhello ()
{
printf ("hello\n");
}

int max (int x,int y)
{
return x < y? x:y;
}
int sum (int x,int y)
{
return x + y;
}

int getValue (int x,int y,int (*p) (int, int))
{
P is a function pointer, which points to a function that is determined by the function name passed in when the function is called
int r = p (x, y);
return R;
}


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

#pragma mark--------------------the function pointer--------------------
/*
Function: Code that implements a particular function
When the program compiles, the system assigns the function code to the code area, where the starting address of the storage space is the address of the function. Also known as
Pointers to functions
The function and number names are the same address

Calling a function from a function name
printf ("%d", MaxValue (3,5));

function pointers: pointers to functions. Functions that can be called by a function pointer
Define a function pointer to the MAXVALUE function
The pointer type must match the type of the data pointed to, the function has a type, and the function pointer must be consistent with the type of the function
Type of maxvalue function int (int,int)
return value type (* function pointer name) (parameter type) = function name

Int (*p) (int x,int y) = MaxValue;
printf ("%d", p (30,50));

function pointer type: return value type (*) (parameter type) For example:
Int (*) (int x, int y);

Exercise: void Printhello (); Define a function pointer that can point to the above function, and call the function with a function pointer implementation

void (*p) () = Printhello;
P ();


Practice defining two functions, one for the greatest value, one for the sum, and the other for the max or sum to be 3,5. A function pointer, which points to a different function according to the input content of the output, and the last one to complete.

Char A[5] = {0};
scanf ("%s", a);
if (strcmp (A, "sum") = = 0)
{
Int (*p) (int x,int y) = sum;
printf ("%d", p (3,5));

}
if (strcmp (A, "max") = = 0)
{
Int (*p) (int x,int y) = max;
printf ("%d", p (3,5));

}

printf ("\ n");
*/
#pragma mark--------------callback function------------------
/*
function pointers as function arguments
Int (*p) (int, int) = max;
Int (*q) (int, int) = SUM;
int max1 = GetValue (3, 5, p);
int sum2 = GetValue (3, 5, q);
printf ("%d%d\n", max1,sum2);

Callback procedure: In the process of getvalue execution, a function pointer is passed into the body of each country, the call executes a function
callback function: During function execution. function called by function pointer
Function callback: Action to invoke callback function
*/
Practice writing? A function to find students with scores of more than 90 points, so that the callback function after the name of the "" Rich handsome.
/*
Student Stu1[3] =
{
{1, "Zhangsan", 84.0},
{2, "Lisi", 59.0},
{3, "Wangwu", 96.0}
};
Student *p = STU1;
Findstudentscorechengji (P);
*/
/*
Student stu1 = {"Wukong", ' m ', 500, 60.0};
Student stu2 = {"Zixia", ' W ', 18, 98.0};
Student stu3 = {"Sanzang", ' W ', 40, 100.0};
Student Stu4 = {"Longma", ' W ', 27, 93.0};
Student Stu5 = {"Bajie", ' W ', 300, 59.0};
Student Stus[5] = {stu1, STU2, Stu3, Stu4, stu5};
Defines a function pointer, pointing to the concatenation string function
void (*p) (char*) = Addstr;

Findstudentbyscore (Stus, 5, p);
Printallstudent (Stus, 5);
*/

#pragma mark----------Dynamic sorting--------------

Sorted by: Sorting rules different, age, name, score, ascending, descending
Using callback functions for dynamic sequencing

Student stu1 = {"Wukong", ' m ', 500, 60.0};
Student stu2 = {"Zixia", ' W ', 18, 98.0};
Student stu3 = {"Sanzang", ' W ', 40, 100.0};
Student Stu4 = {"Longma", ' W ', 27, 93.0};
Student Stu5 = {"Bajie", ' W ', 300, 59.0};
Student Stus[5] = {stu1, STU2, Stu3, Stu4, stu5};
printf ("Sort by name \ n");
Sortstudentbyname (stus,5);
Allstudent (stus,5);
printf ("Sort by fractions \ n");
Sortstudentbyscore (stus,5);
Allstudent (stus,5);
printf ("Sort by age \ n");
Sortstudentbyage (stus,5);
Allstudent (stus,5);
//
printf ("New play \ n");
printf ("Sort by name \ n");
Sortstudent (Stus,5,compartstudentbyname);
Allstudent (stus,5);
printf ("Sort by fractions \ n");
Sortstudent (Stus,5,compartstudentbyscore);
Allstudent (stus,5);
printf ("Sort by age \ n");
Sortstudent (Stus,5,compartstudentbynage);
Allstudent (stus,5);

The return value of #pragma mark------------function is a pointer to the function--------------

M.h

typedef struct
{
Char name[30];
char sex;
int age;
Float score;
}student;

void Findstudentscorechengji (Student *p);
Sort by name Size
void Sortstudentbyname (Student *stus,int count);
According to the score row
void Sortstudentbyscore (Student *stus,int count);
According to the Age platoon
void Sortstudentbyage (Student *stus,int count);
Traverse output
void Allstudent (Student *stus,int count);

New gameplay
typedef BOOL (*sort) (student,student);
Sort represents a type function pointer, and the return value of the function pointed to is a bool type, and the arguments are two student variables
Implementing a Sort function
void Sortstudent (Student *stus,int count,sort p_sort);
BOOL compartstudentbyname (Student stu1,student stu2);
BOOL Compartstudentbyscore (Student stu1,student stu2);
BOOL compartstudentbynage (Student stu1,student stu2);

M.m

/*
void Findstudentscorechengji (Student *p)
{
for (int i = 0; i < 3; i + +)
{
if ((P + i) score > 90)
{
Strcat ((p + i), name, "Gao Fu Shuai");
printf ("%d%s%.1f\n", (P + i), num, (p+i), name, (P + i), score);
}
}
}
*/
/*
void Findstudentbyscore (Student *stus,int count,void (*p) (char *)) {
for (int i = 0; i < count;i++) {
if (Stus[i].score > 90) {
P (stus[i].name);//Function callback
}
}
}
void Printallstudent (Student *stus,int count) {
for (int i = 0; i < count; i++) {
printf ("name =%s, sex =%c, age =%d, score =%.2f\n", stus[i].name,stus[i].sex,stus[i].age,stus[i].score);
}

}
*/

Sort by name Size
void Sortstudentbyname (Student *stus,int count)
{
for (int i = 0; i < count-1; i + +)
{
for (int j = 0; J < count-1-I; j + +)
{
if (strcmp (stus[j].name,stus[j+1].name))
{
Student temp = stus[j];
STUS[J] = stus[j + 1];
Stus[j + 1] = temp;

}
}
}
}
According to the score row
void Sortstudentbyscore (Student *stus,int count)
{
for (int i = 0; i < count-1; i + +)
{
for (int j = 0; J < count-1-I; j + +)
{
if (Stus[j].score > Stus[j+1].score)
{
Student temp = stus[j];
STUS[J] = stus[j + 1];
Stus[j + 1] = temp;

}
}
}
}
According to the Age platoon
void Sortstudentbyage (Student *stus,int count)
{
for (int i = 0; i < count-1; i + +)
{
for (int j = 0; J < count-1-I; j + +)
{
if (Stus[j].age > Stus[j+1].age)
{
Student temp = stus[j];
STUS[J] = stus[j + 1];
Stus[j + 1] = temp;
}
}
}

}

Traverse output
void Allstudent (Student *stus,int count)
{
for (int i = 0; i < count; i++) {
printf ("name:%s sex:%c age:%d score:%.1f\n", Stus[i].name,stus[i].sex,stus[i].age,stus[i].score);
}

}

Implementing a Sort function
void Sortstudent (Student *stus,int count,sort p_sort)
{
for (int i = 0; i < count-1; i + +)
{
for (int j = 0; J < count-i-1; j + +)
{
if (P_sort (stus[j],stus[j+1]))
            {                                                                                                                                                                                                                              
Student temp = stus[j];
STUS[J] = stus[j+1];
STUS[J+1] = temp;
}
}
}
}
BOOL compartstudentbyname (Student stu1,student stu2)
{
Return strcmp (Stu1.name, stu2.name) > 0;
}
BOOL Compartstudentbyscore (Student stu1,student stu2)
{
return stu1.score > Stu2.score;
}

BOOL compartstudentbynage (Student stu1,student stu2)
{
return stu1.age > Stu2.age;
}

C Introductory Section 11th

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.