iOS开发学习之C语言---C10 函数指针-2
来源:互联网 发布:数据集市的作用 编辑:程序博客网 时间:2024/05/17 23:52
//
// main.m
// C10 函数指针-2
//
// Created by 康亮亮 on 15/10/20.
// Copyright (c) 2015年 Darling.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#pragma mark typede函数指针
typedef struct student{
char name[20];
int age;
float score;
}Student;
// 使用typedef给函数类型重定义
// 使用PFUNC来当做函数类型的新名字
typedefBOOL (*PFUNC)(Student,Student);
/*
// 定义一个函数,实现按照姓名升序排列
//void sortByName(Student *stu, int count){
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - 1 - i; j++) {
// if (strcmp(stu[j].name, stu[j + 1].name) > 0) {
// Student temp = stu[j];
// stu[j] = stu[j + 1];
// stu[j + 1] = temp;
// }
// }
// }
//}
//
//void sortByAge(Student *stu, int count){
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - 1 - i; j++) {
// if (stu[j].age > stu[j + 1].age) {
// Student temp = stu[j];
// stu[j] = stu[j + 1];
// stu[j + 1] = temp;
// }
// }
// }
//}
//
//void sortByScore(Student *stu, int count){
// for (int i = 0; i < count - 1; i++) {
// for (int j = 0; j < count - 1 - i; j++) {
// if (stu[j].score > stu[j + 1].score) {
// Student temp = stu[j];
// stu[j] = stu[j + 1];
// stu[j + 1] = temp;
// }
// }
// }
//}
*/
// 按照成绩升序
BOOL sortByScore(Student stu1,Student stu2){
if (stu1.score > stu2.score) {
return YES;
}else
return NO;
}
// 按照姓名
BOOL sortByName(Student stu1,Student stu2){
if (strcmp(stu1.name,stu2.name) >0) {
return YES;
}else
return NO;
}
// 按照年龄
BOOL sortByAge(Student stu1,Student stu2){
if (stu1.age > stu2.age) {
return YES;
}else
return NO;
}
// 定义一个中间函数,通过这个函数的指针参数,调用不同的排序规则函数
void sortArray(Student student[],int count, PFUNC p ){
for (int i =0; i < count - 1; i++) {
for (int j =0; j < count - 1 - i; j++) {
if(p(student[j], student[j + 1])){
Student temp = student[j];
student[j] = student[j +1];
student[j +1] = temp;
}
}
}
}
// 封装打印函数
void printStudent(Student *stu,int count){
for (int i =0; i < count; i++) {
printf("%s %d %.2f\n", stu[i].name, stu[i].age, stu[i].score);
}
}
int main(int argc,const char * argv[]) {
//一个学生结构体数组, 按照姓名给学生升序排列
Student students[5] = {
{"zhangsan",20, 65},
{"lisi",19, 75},
{"wuliu",23, 85},
{"jenny",32, 90},
{"danny",25, 96},
};
/*
printf("按姓名排序:\n");
sortByName(students, 5);
printStudent(students, 5);
printf("按年龄排序:\n");
sortByAge(students, 5);
printStudent(students, 5);
printf("按成绩排序:\n");
sortByScore(students, 5);
printStudent(students, 5);
*/
//根据用户输入的字符判断指针应该指向那个函数
char string[20];
printf("请输入要求:\n");
scanf("%s", string);
PFUNC p = NULL;
if (strcmp(string,"sortByName") == 0) {
p =sortByName;
}elseif(strcmp(string,"sortByAge") == 0){
p =sortByAge;
}elseif(strcmp(string,"sortByScore") == 0){
p =sortByScore;
}
if(p != NULL){
sortArray(students, 5, p);
printStudent(students, 5);
}
return 0;
}
- iOS开发学习之C语言---C10 函数指针-2
- iOS开发学习之C语言---C10 函数指针-1
- iOS开发学习之C语言---C10 函数指针-3
- iOS开发学习之C语言---C07 指针-2
- iOS开发之c语言基础-函数指针
- iOS开发学习之C语言---C05 函数-2
- ios开发-c语言之C语言函数的学习
- 【学习ios之路:C语言】指针函数
- ios开发-c语言之指针的学习
- iOS开发学习之C语言---C07 指针-1
- iOS开发学习之C语言---C08 高级指针-1
- iOS开发学习之C语言---C08 高级指针-12
- iOS开发学习笔记 2-9 C语言部分 内存分配函数 函数指针 指针函数 void*
- iOS开发之--C语言之指针
- IOS开发系列--C语言之指针
- IOS开发系列--C语言之指针
- iOS开发系列--C语言之指针
- IOS开发系列--C语言之指针
- 敏捷实况1:项目概况
- java中的匿名内部类总结
- iOS开发学习之C语言---C10 函数指针-1
- Android 怎样把底部栏顶上去
- 【转】java基础:堆&栈
- iOS开发学习之C语言---C10 函数指针-2
- leetcode Problem1 two sum
- 最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
- static 剖析(C++)
- JSP/Servlet-----10、JSP2的自定义标签
- iOS开发学习之C语言---C10 函数指针-3
- iOS开发学习之C语言---C11 二维数组、字符串数组、多维数组
- 值得推荐的C/C++框架和库
- leetcode15 3Sum