题目
自定义一个Student类,属性包括:char name[10], int num.编程实现学生信息的输入,查询,浏览,其中浏览分为:升序和降序两种。
code
#include<stdio.h>
#include<vector>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
class Student
{
private:
char m_name[10];
int m_num;
public:
Student(){m_num = 0;}
~Student(){}
void input(char name[], int num);
void search(char name[]);
void show();
int getNum(){return m_num;}
};
void Student::input(char name[], int num)
{
strcpy(m_name, name);
m_num = num;
}
void Student::search(char name[])
{
if (strcmp(m_name, name) == 0)
{
cout << m_name << " " << m_num << endl;
}
}
void Student::show()
{
cout << m_name << " " << m_num << endl;
}
bool cmp(Student s1, Student s2)
{
return s1.getNum() < s2.getNum();
}
void aceBrowse(vector<Student>v_student)
{
for(int i = 0; i < v_student.size(); i++)
{
v_student[i].show();
}
}
void desBrowse(vector<Student>v_student)
{
for (int i = v_student.size() - 1; i >= 0; i--)
{
v_student[i].show();
}
}
int main()
{
vector<Student> v_student;
Student stu;
char name[10];
int num, i;
while (cin >> name >> num)
{
if (num == -1)
{
break;
}
stu.input(name, num);
v_student.push_back(stu);
}
int i_choice;
sort(v_student.begin(), v_student.end(), cmp);
cin.clear();
cout << "1 升序 2 降序 3 按姓名查询 4 退出" << endl;
while (cin >> i_choice)
{
if (i_choice == 1)
{
aceBrowse(v_student);
}
if (i_choice == 2)
{
desBrowse(v_student);
}
if (i_choice == 3)
{
cin >> name;
for (i = 0; i < v_student.size(); i++)
{
v_student[i].search(name);
}
}
}
return 0;
}