题目详情

阅读以下说明和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 << end1;

}

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%: "<<end1;

for(k = 0; k < number * 0.3; k++) {

testStudent[k].out();

}

delete [ ]testStudent;

return 0;

}

正确答案及解析

正确答案
解析

(1)int Stedent

(2)void Student

(3)getCredit()

(4)getCredit()

(5)new Student

(6)SortStudent ss

首先分析程序的整体结构,本题中定义了两个类:Student和SortStudent,分别用于定义学生和进行排序。类Student的定义中,第一部分为private访问权限的成员,要对其进行访问,需要通过具有相应访问权限的成员函数。在第二部分的接口定义为public。

Student类的成员函数的定义在类外,需要用类名约束机制,因此前面俩空需要补充函数的返回类型和类名。

SortStudent类中的成员函数sort和Student类的对象所构成的对象数组根据分数进行排序,而由于在Strdent类中学号和分数为private访问权限,不可从外部直接访问,所以要通过getCredit而得到。

Main函数中需要将学生信息读入,并根据学生信息创建对象并加入对象数组。空(5)为new Student。空(6)为单独语句,但是从其后续语句ss.sort的函数调用可知,使用了SortStudent的函数sort,使用前需要创建SortStudent类的对象。

你可能感兴趣的试题

单选题

中级软件设计师,章节练习,中级软件设计师系统开发运行知识

  • A.0
  • B.1
  • C.2
  • D.3
查看答案
单选题

中级软件设计师,章节练习,中级软件设计师系统开发运行知识

  • A.1、1
  • B.1、2
  • C.2、2
  • D.2、3
查看答案
单选题

中级软件设计师,章节练习,中级软件设计师系统开发运行知识

  • A.3
  • B.4
  • C.5
  • D.6
查看答案
单选题

中级软件设计师,章节练习,中级软件设计师系统开发运行知识

中级软件设计师,章节练习,中级软件设计师系统开发运行知识

  • A.V(S3)和V(S5)V(S6)
  • B.P(S3)和V(S5)V(S6)
  • C.V(S3)和P(S5)P(S6)
  • D.P(S3)和P(S5)P(S6)
查看答案
单选题

中级软件设计师,章节练习,中级软件设计师系统开发运行知识

  • A.243ms
  • B.246ms
  • C.254ms
  • D.280ms
查看答案

相关题库更多 +