定义一个Student类,包括姓名、学号、成绩三个成员变量以及getName()、getNo()、getScore()三个方法。在构造函数中初始化姓名、学号、成绩三个成员变量。定义一个大小为20的类型为Student的数组,按学生成绩的高低,依次输出姓名和成绩。
//åèç¨åºï¼
#include "iostream"
#include "string"
using namespace std;
class Student
{
private:
int no; //å¦å·
string name; //å§å
int score; //æ绩
public:
Student()
{ }
Student(int no, string name, int score)
{
this->no = no;
this->name = name;
this->score = score;
}
string getName()
{
return name;
}
int getNo()
{
return no;
}
int getScore()
{
return score;
}
void display()
{
cout<<no<<"\t"<<name<<"\t"<<score<<endl;
}
};
void sort(Student stus[], int len)
{
int i, j, k;
Student temp;
for(i=0; i<len-1; i++)
{
k = i;
for(j=i+1; j<len; j++)
if(stus[j].getScore() > stus[k].getScore())
k = j;
if(k != i)
{
temp = stus[k];
stus[k] = stus[i];
stus[i] = temp;
}
}
}
void main()
{
int i;
int len = 5;
Student stus[20];
int no;
string name;
int score;
cout<<"请è¾å ¥20个å¦ççä¿¡æ¯(å¦å·ãå§åãæ绩) : "<<endl;
for(i=0; i<len; i++)
{
cout<<"No. "<<i+1<<" : ";
cin>>no>>name>>score;
stus[i] = Student(no, name, score);
}
sort(stus, len);
cout<<"å¦çä¿¡æ¯ä¸è§(ææ绩éåº) : "<<endl;
for(i=0; i<len; i++)
{
stus[i].display();
}
}
è¿è¡ç»æï¼
追é®ä½ 好 å¯ä¸å¯ä»¥ç¨javaè¯è¨å¢ï¼è°¢è°¢ï¼ï¼ï¼
追çæ©è¯´åï¼æçå°ä½ çæé®åç±»æ¯C/C++ï¼
import java.util.Scanner;
class Student
{
private int no; //å¦å·
private String name; //å§å
private int score; //æ绩
public Student()
{ }
public Student(int no, String name, int score)
{
this.no = no;
this.name = name;
this.score = score;
}
public String getName()
{
return name;
}
public int getNo()
{
return no;
}
public int getScore()
{
return score;
}
public void display()
{
System.out.println(no + "\t" + name + "\t" + score);
}
}
public class TestStudent {
public static void main(String[] args) {
int i;
int len = 20; //å¦ç人æ°
Student[] stus = new Student[len];
int no;
String name;
int score;
Scanner scan = new Scanner(System.in);
System.out.println("请è¾å
¥20个å¦ççä¿¡æ¯(å¦å·ãå§åãæ绩) : ");
for(i=0; i stus[k].getScore())
k = j;
if(k != i)
{
temp = stus[k];
stus[k] = stus[i];
stus[i] = temp;
}
}
}
}