采用封装的思想对学生姓名成绩年龄排序
来源:互联网 发布:mac默认的管理员密码 编辑:程序博客网 时间:2024/04/29 21:32
<span style="font-size:18px;">//// main.m// LessonFunctionPointer2//// Created by lanouhn on 14-7-30.// Copyright (c) 2014年 Summer. All rights reserved.//#import <Foundation/Foundation.h>typedef struct student{ char name[20]; //存储姓名 int age; //存储年龄 float score; //存储成绩}Student;//按年龄排序BOOL sortStudentByAge(Student stu1, Student stu2){ return stu1.age > stu2.age;}//按成绩排序BOOL sortStudentByScore(Student stu1, Student stu2){ return stu1.score > stu2.score;}//按姓名排序BOOL sortStudentByName(Student stu1, Student stu2){ return strcmp(stu1.name, stu2.name) > 0;}typedef BOOL (*Pstudent)(Student, Student);//建立字符串和函数之间的一一对应关系typedef struct nameFunctionPair{ char name[20]; //存储函数对应的字符串 Pstudent function; //存储字符串对应函数的地址}NameFunctionPair;//根据给定的字符串查找匹配表,找出对应的函数//name 用来接收匹配的字符串//p 用来接收匹配表//count 用来接收匹配表中元素的个数Pstudent getFunctionByName(char *name, NameFunctionPair *p, int count){ //根据输入的内容查匹配表找到对应的函数 for (int i = 0; i < count; i++) { if (strcmp(name, (p + i)->name) == 0) { //如果匹配到对应的函数,将函数地址返回 return (p + i)->function; } } //如果没有匹配到对应的函数,就返回NULL. return NULL;}//三个函数之间唯一的不同就在于 冒泡排序中的判断条件不同void sortStudent(Student *p, int count, char *name, NameFunctionPair *pair, int number){ //接收一下匹配到的函数的地址 Pstudent function = getFunctionByName(name, pair, number); for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - 1 - i; j++) { if (function(*(p + j), *(p + j + 1))) { Student temp = {0}; temp = *(p + j); *(p + j) = *(p + j + 1); *(p + j + 1) = temp; } } }}//输出函数void outputStudentInfo(Student *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); }}int main(int argc, const char * argv[]){ //方法一 Student stu[5] = { {"summer", 19, 20.1}, {"mahaitao", 20, 90.0}, {"xiaoyu", 21, 12.3}, {"dream", 22, 23.3}, {"rain", 21, 25.3} }; //创建匹配表 NameFunctionPair pair[3] = { {"name", sortStudentByName}, {"age", sortStudentByAge}, {"score", sortStudentByScore} }; char tempName[20] = {0}; //用来接收从控制台输入的字符串 printf("请输入排序的方式(姓名:name,年龄:age,成绩:score):\n"); scanf("%s", tempName); //对学生排序 sortStudent(stu, 5, tempName, pair, 3); outputStudentInfo(stu, 5); //方法二// Student stu[5] = {// {"summer", 19, 20.1},// {"mahaitao", 20, 90.0},// {"xiaoyu", 21, 12.3},// {"dream", 22, 23.3},// {"rain", 21, 25.3}// };// //1.按姓名升序排序// void sortStudentByName(Student *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) {// Student temp = {0};// temp = *(p + j);// *(p + j) = *(p + j + 1);// *(p + j + 1) = temp;// }// }// }// }// //2.按年龄升序排序// void sortStudentByAge(Student *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) {// Student temp = {0};// temp = *(p + j);// *(p + j) = *(p + j + 1);// *(p + j + 1) = temp;// }// }// }// }// //3.按成绩升序排列// void sortStudentByScore(Student *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) {// Student temp = {0};// temp = *(p + j);// *(p + j) = *(p + j + 1);// *(p + j + 1) = temp;// }// }// }// }// sortStudentByName(stu, 5);// outputStudentInfo(stu, 5);// printf("\n");// printf("\n");// sortStudentByAge(stu, 5);// outputStudentInfo(stu, 5);// printf("\n");// printf("\n");// sortStudentByScore(stu, 5); return 0;}</span>
0 0
- 采用封装的思想对学生姓名成绩年龄排序
- 函数指针按姓名,年龄,成绩对学生排序
- 姓名,年龄,成绩的排序
- 数组排序之对一组学生按照成绩, 学号,年龄, 姓名排序~
- 学生成绩,年龄排序实现
- 对学生成绩的排序,
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序, 成绩, 年龄
- 定义类Person,可以存放每个学生的姓名,年龄,成绩
- 1061 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。
- 封装排序专题,按姓名排序,年龄排序,Block排序
- 函数指针,包装,函数指针按姓名,年龄,成绩排序
- 27.有10个学生,输出成绩最低的学生的信息(包括姓名、年龄、成绩、出生日期)(用结构体知识)。
- TreeMap集合的应用根据学生的年龄或者姓名排序
- 带姓名的成绩排序
- TreeMap练习:对学生对象的姓名进行升序排序
- 一组成绩,一组学生,按照成绩的大小,由高到底排序出姓名和成绩(二叉树)实现
- Linux-C基础知识学习:C语言作业-输入某个学生的信息(姓名,年龄,5门功课成绩),计算平均成绩并输出。(待完善)
- 输入学生的姓名学号成绩,并按平均分由低到高排序
- 配置JDK环境变量
- Akka
- MongoDB入门教程
- JSP自定义标签
- Delphi中destroy, free, freeAndNil, release用法和区别
- 采用封装的思想对学生姓名成绩年龄排序
- Binary Tree Inorder Traversal
- 详解JVM的内存管理机制
- 设计模式实战应用之五:工厂方法模式
- REST服务开发实战
- 输出n对括号的所有有效排列组合
- python编码处理:unicode字节串转成中文 各种字符串举例说明
- SGU 275. To xor or not to xor(高斯消元)
- java操作 mongo DB 数据库例子