现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。
输入时,首先要输入学生数(正整数)N。接着输入N组学生成绩,每组成绩包括两项:第一项是学生姓名,第二项是学生的成绩(整数)。
输出时,依次输出各个学生的序号(从1开始顺序编号),学生姓名,成绩等级(不小于60为PASS,否则为FAIL)
函数接口定义:
面向Student类对象的流插入和流提取运算符
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main(){
int i, repeat;
Student st;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>st;
cout<<st<<endl;
}
return 0;
}
输入样例:
3
Li 75
Zhang 50
Yang 99
输出样例:
1. Li PASS
2. Zhang FAIL
3. Yang PASS
AC代码如下:
class Student{
private:
string name;
int score;
public:
Student(){}
Student(string na,int s){name=na;score=s;}
friend istream & operator>>( istream & is, Student &s);
friend ostream & operator<<( ostream & os,Student &s);
};
istream & operator>>( istream & is, Student &st){
is>>st.name>>st.score;
}
ostream & operator<<( ostream & os,Student &st){
static int num=1;
if(st.score>=60)
{ os<<num<<". "<<st.name<<" PASS";
}
else{os<<num<<". "<<st.name<<" FAIL";}
num++;
}
面向Student类对象的流插入和流提取运算符提示要有友元函数的插入与提取的重载;(定义为友元函数方便访问类的成员)
声明加上类外定义即可;
完整代码如下:
#include<iostream>
using namespace std;
class Student{
private:
string name;
int score;
public:
Student(){}
Student(string na,int s){name=na;score=s;}
friend istream & operator>>( istream & is, Student &s);
friend ostream & operator<<( ostream & os,Student &s);
};
istream & operator>>( istream & is, Student &st){
is>>st.name>>st.score;
}
ostream & operator<<( ostream & os,Student &st){
static int num=1;
if(st.score>=60) {
os<<num<<". "<<st.name<<" PASS";
}
else{os<<num<<". "<<st.name<<" FAIL";}
num++;
}
//静态成员num为了记录调用次数 ,非静态的话num值不会随着调用次数改变而增加;
int main()
{
int i, repeat;
Student st;//提示记得要有无参构造函数的存在
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>st;
cout<<st<<endl;
}
return 0;
}