题库 题库

【简答题】

试题五(共15分)
    阅读以下说明和 C++代码,将应填入 (n) 处的语句或语句成分写在答题纸的对应栏内。
【说明】
    某数据文件students.txt的内容为100名学生的学号和成绩,下面的程序将文件中的数据全部读入对象数组,按分数从高到低进行排序后选出排名前30%的学生。【C++代码】
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
    private:
    string sNO;    //学号
    int credit;      //分数
    public:
    Student(string a,int b) { sNO = a; credit = b;}
    Student(){}
    int getCredit();
    void out();
};
  (1)  ::getCredit() {
    return credit;
}
  (2)  ::out() {
    cout << "SNO: " << sNO << ", Credit=" << credit << endl;
}
class SortStudent {
    public:
    void sort(Student *s, int n);
    SortStudent(){}
};
void SortStudent::sort(Student *s,int n) {
    for(int i = 0; i < n-1; i++) {
    for(int j = i+1; j < n; j++) {
    if(s[i].  (3)  < s[j].  (4)  )    {
    Student temp = s[i];    s[i] = s[j];    s[j] = temp;
    }
    }
    }
}
int main(int argc, char* argv[])
{
    const int number = 100;    //学生总数
    ifstream students;
    students.open("students.txt");
    if(!students.is_open()) {
    throw 0;
    }
    Student *testStudent =  (5)  [number];
    int k = 0;
    string s;
    while (getline(students,s,'\n')) {    //每次读取一个学生的学号和成绩
    Student student(s.substr(0,s.find(',')), atoi(s.substr(s.find(',')+1).c_str()));
    testStudent[k++] = student;
    }
    students.close();
    (6)  ;
    ss.sort(testStudent,k);
    cout <<"top 30%: "<<endl;
    for(k = 0; k < number * 0.3; k++) {
    testStudent[k].out();
    }
    delete []testStudent;
    return 0;
}

参考答案

(1)int Student                                  (3分)
(2)void Student                                 (2分)
(3)getCredit()                                  (2分)
(4)getCredit()                                  (2分)
(5)new Student                                  (3分)
(6)SortStudent ss                               (3分)

相关试题