北理07年複試上機之實現學生類並實現信息一些操作

題目

自定義一個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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章