结构体排序
2018-01-11 20:59
85 查看
// 1、假如有5个学生,按照年龄排序,如果年龄相同,按照成绩排序 // struct student // { // char name[50];//姓名 // unsigned int age; // int score;//成绩 // }; #include <stdio.h> typedef struct student { char name[50];//姓名 unsigned int age; int score;//成绩 }student; void print_stu(student *stu, int n) { int i = 0; printf("name\tage\tscore\n"); for(i = 0; i < n; i++) { printf("%s\t%d\t%d\n", stu[i].name, stu[i].age, stu[i].score); }; } void sort_stu(student *stu, int n) { int i = 0; int j = 0; student tmp; //临时结构体变量 //冒泡排序,按照年龄排序,如果年龄相同,按照成绩排序 for(i = 0; i < n-1; i++) { for(j = 0; j < n-1-i; j++) { if(stu[j].age > stu[j+1].age) //升序 { tmp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = tmp; } else if(stu[j].age == stu[j+1].age)//如果年龄相同,按照成绩排序 { if(stu[j].score > stu[j+1].score) { tmp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = tmp; } } } } } int main() { student stu[5] = { {"mmm", 18, 90}, {"jjj", 18, 80}, {"lily", 30, 70}, {"lucy", 22, 88}, {"lilei", 33, 60} }; int n = sizeof(stu)/sizeof(stu[0]); printf("排序前:\n"); print_stu(stu, n); //排序 sort_stu(stu, n); printf("排序后:\n"); print_stu(stu, n); return 0; }
模拟闹钟
#include <stdio.h> #include <time.h> #include <unistd.h> typedef struct Time {//表示时间的数据类型 int hour; int minute; int second; }Time; int main() { Time m_date; m_date.hour = 10; m_date.minute = 00; m_date.second = 00; printf("当前时间为:\n"); while(1) { sleep(1); m_date.second++; if(m_date.second >= 60) { m_date.second = 0; m_date.minute++; if(m_date.minute >= 60) { m_date.minute = 0; m_date.hour++; if(m_date.hour >= 24) { m_date.hour = 0; } } } printf("\r%02d:%02d:%02d",m_date.hour,m_date.minute,m_date.second); fflush(stdout); } }
相关文章推荐
- 容器中数据类型为结构体时对其进行排序
- C++ sort 对结构体进行排序
- 【1015】德才论 (结构体排序)
- HDOJ 1862 EXCEL排序 (结构体排序的构造)
- (DS1.5.4)POJ 1581 A Contesting Decision(结构体排序)
- 结构体简单排序练习 题目1014:排名
- HDU 2093 水题,就是注意一下细节,有个结构体的排序,重载运算符小于
- C++ vector的排序------对于结构体vector, 需要自己实现比较
- ACM中的格式化问题/结构体排序格式化输出/水果
- Gym - 101149F The Weakest Sith (结构体排序)
- 对结构体中按年龄的排序
- BToj 1031问题 B: KACA的数字排序 【大数比较--结构体sort && java大数比较】
- C语言-对一个结构体中的字段进行排序
- HDU 1862 EXCEL排序(结构体排序)
- HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了(结构体排序,背包????)
- 【慢慢学算法】:成绩排序(qsort对结构体的多级排序)
- 结构体案例之学生成绩输入及排序
- c++中sort对结构体排序
- PAT (Advanced Level) 1012. The Best Rank (25) 最佳排名,结构体排序,哈希查询
- HDU 2093 考试排名---结构体排序