//
// main.cpp
// select_sortc++
//
// Created by duanqibo on 2019/7/17.
// Copyright © 2019年 duanqibo. All rights reserved.
// 选择排序 c++
#include <iostream>
#include <string>
#include <iomanip>
#define N 10
using namespace std;
class student
{
private:
int num;
char name[20];
char sex[2];
int age;
public:
student(){}; //无参数构造函数
//带参数的构造函数
student(int n,char na[20],char se[2],int ag)
{
num=n;
strcpy(name,na); //字符串的赋值
strcpy(sex,se); //字符串的赋值
age=ag;
}
void friend select_sort(student stud[],int n);
void show();
};
//按姓名选择排序
void select_sort(student stud[],int n)
{
int min,i,j;
student temp;
for(i=0;i<=n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(strcmp(stud[j].name,stud[min].name)<0)
min=j;
}
if(min!=j)
{
temp=stud[min];
stud[min]=stud[i];
stud[i]=temp;
}
}
for(i=0;i<n;i++)
{
cout<<' '<<stud[i].num
<<' '<<stud[i].name
<<' '<<stud[i].sex
<<' '<<stud[i].age
<<endl;
}
}
int main(int argc, const char * argv[]) {
// insert code here...
student stu[4]={student(1001,"zhang","m",20),
student(1002,"wang","f",19),
student(1003,"liu","f",18),
student(1004,"chen","m",20)
};
int len=sizeof(stu)/sizeof(stu[0]);
cout<<' '<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<endl;
select_sort(stu,len);
return 0;
}
运行结果: