C++程序练习-3719:学生信息用qsort排序

描述
将输入的学生信息按名字排序后输出。
输入
每个学生信息是两行,第一行是名字,由英文字母和空格构成,最长18个字符;第二行格式为:

学号,性别 年龄

学号是一个不超过100,000的整数; 性别是一个字符,为'M' 或'F'; 年龄是一个不大于100的整数
输入数据最后有可能有若干个回车,也有可能没有


学生不会超过100个,不会出现两个学生的名字仅大小写有差别的情况
输出
格式和输入数据基本一样,唯一不同在于输出学号的时候,必须用前导0补足8位
样例输入
Tom Hanks
7863,M 18
Mary Lu
18343,F 21
Santa Fe
27863,M 17
样例输出
Mary Lu
00018343,F 21
Santa Fe
00027863,M 17
Tom Hanks
00007863,M 18

参考代码//http://poj.grids.cn/practice/3719/ //1.输入数据最后有可能有若干个回车,也有可能没有 //2.不会出现两个学生的名字仅大小写有差别的情况 //3.提示不可信,造就多次CE。(用 stricmp 函数作大小写无关的字符串比较。如果在POJ上交,名字要改成 _stricmp) //4.此题很简单,休息一下,仔细想想就明白了。 // //哥爱水水,主教级别TX不要BS哥。 //Accepted 256kB 0ms 1218 B G++ #include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> #include <cstring> #include <algorithm> using namespace std; //Student information typedef class Student{ public: char name[19]; char low_name[19]; int id; char sex; int age; void tolowername(char *s); }Student; Student stu[101]; void Student::tolowername(char *s){ int i,len = strlen(s); for(i = 0;i < len;++ i){ this->low_name[i] = tolower(s[i]); } this->low_name[i] = '\0'; } //compare int comp(const void *x,const void *y){ return strcmp(((Student*)x)->low_name, ((Student*)y)->low_name); } int main(){ Student pstu; int i,j; char ch; //initialize and input data i = 0; while(std::cin.getline(pstu.name,19)){ if(strcmp(pstu.name,"")==0) continue; std::cin>>pstu.id>>ch>>pstu.sex>>pstu.age; getchar(); pstu.tolowername(pstu.name); stu[i ++] = pstu; } //sorted by name asc qsort(stu,i,sizeof(Student),comp); //output result for(j = 0;j < i;++ j){ std::cout<<stu[j].name<<std::endl; std::cout<<std::setw(8)<<std::setfill('0')<<stu[j].id<<","<<(char)stu[j].sex<<" "<<stu[j].age<<std::endl; } return 0; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值