首页 > 学院 > 开发设计 > 正文

北理07年复试上机之实现学生类并实现信息一些操作

2019-11-08 02:12:38
字体:
来源:转载
供稿:网友

题目

自定义一个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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表