#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
void sort_student(char students[][30],int);
void insert_students(char students[][30],int &);
void input(char students[][30],int&);
void output(char students[][30],int);
void menu();
void select(char students[][30],int&);
int main()
{
char students[50][30];
int number=0;
select(students,number);
return 0;
}
void select(char students[50][30],int &number)
{
int n;
while(1)
{
menu();
cin>>n;
cin.ignore(1000,'\n');
switch(n)
{
case 1:
input(students,number);
break;
case 2:
insert_students(students,number);
cout<<"插入新纪录后的学生列表:"<<endl;
output(students,number);
break;
case 3:
sort_student(students,number);
cout<<"排序后的学生列表:"<<endl;
output(students,number);
break;
case 4:
output(students,number);
break;
case 5:
exit(1);
}
}
}
void input(char students[50][30],int &number)
{
cout<<"请输入学生人数:";
cin>>number;
cin.ignore(1000,'\n');
int i=0;
cout<<"请输入学生姓名:"<<endl;
for(i=0;i<number;i++)
cin.getline(students[i],30,'\n');
}
void output(char students[50][30],int number)
{
int i=0;
for(i=0;i<number;i++)
{
cout<<students[i]<<" ";
if(i%10==9)
cout<<endl;
}
cout<<endl;
}
void sort_student(char students[50][30],int number)
{
int i=0,j=0,k;
char temp[30];
for(i=0;i<number-1;i++)
{
k=i;
for(j=i;j<number;j++)
{
if(strcmp(students[j],students[k])<0)
k=j;
}
if(k!=i)
{
strcpy(temp,students[k]);
strcpy(students[k],students[i]);
strcpy(students[i],temp);
}
}
}
void insert_students(char students[50][30],int &number)
{
int i=0,j=0,k;
char name[30];
cout<<"请输入要插入的学生姓名:";
cin.getline(name,30,'\n');
if(strcmp(students[number-1],name)<0)
strcpy(students[number],name);
else
{
for(i=0;i<number;i++)
{
if(strcmp(students[i],name)>0)
{
k=i;
for(j=number-1;j>=i;j--)
{
strcpy(students[j+1],students[j]);
}
break;
}
}
strcpy(students[k],name);
}
++number;
}
void menu()
{
cout<<"1、输入名字。"<<endl;
cout<<"2、添加名字。"<<endl;
cout<<"3、排序名字。"<<endl;
cout<<"4、输出名字。"<<endl;
cout<<"5、退出。"<<endl;
cout<<"请输入:";
}
温馨提示:内容为网友见解,仅供参考