[说明]
某数据文件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.colse();
(6) ;
ss.sort(testStudent,k);
cout <<"top 30%: "<<end1;
for(k=0; k<number * 0.3; k++)
testStudent[k].out();
delete []testStudent;
return 0;
(4)处应填()。