生成上述类并编写主函数,根据输入的学生基本信息,建立一个学生对象,计算其cpp总评成绩,并输出其学号、姓名、总评成绩。
输入格式: 测试输入包含若干测试用例,每个测试用例占一行(学生姓名 学号 年龄 cpp成绩 cpp考勤)。当读入0时输入结束,相应的结果不要输出。
输入样例:
Mike 10005 17 95.0 5
0
输出样例:
10001 Bob 75.9
10005 Mike 95.5
Bob 10001 18 75.5 4
#include <iostream>
#include<string>
#include <iomanip>
#include<stdio.h>
using namespace std;
class Person
{
protected:
string name;
int age;
public:
Person(){};
Person (string p_name, int p_age);
void display ()
{
cout<<name<<":"<<age<<endl;
}
};
Person::Person (string p_name, int p_age)
{
name=p_name;
age=p_age;
}
class Student :public Person
{
int ID;//学号
float cpp_score;//cpp上机成绩
float cpp_count;//cpp上机考勤
float cpp_grade;//cpp总评成绩
//总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;
public:
Student(){};
//这里用构造函数也行,另加一个set函数也行
// Student(string n,int id,int age,float score,float cont):Person(n,age)
// {
// ID=id;
// cpp_score=score;
// cpp_count=cont;
// cpp_grade=cpp_score * 0.9 + cpp_count * 2
// }
void set(string n,int id,int age,float score,float cont)
{
this->age = age ;
this->name = n ;
ID=id;
cpp_score=score;
cpp_count=cont;
cpp_grade=cpp_score * 0.9 + cpp_count * 2;
}
void print()
{
cout<<ID<<" "<<name<<" "<<fixed<<setprecision(1)<<cpp_grade<<endl; ///这里用c输出就没问题,我原本是想用c++的一个控制精度函数,但是忘了加fixed;
///cout<<ID<<" "<<name<<" "; ///fixed:表示普通方式输出,不采用科学计数法。fixed也可以放在setprecision(n)后面
//printf("%.1f\n",cpp_grade);
}
};
int main()
{
Student stu[100];
int i=1;
string n;
int id;
int age;
float cpp_score;
float cpp_count;
Student st;
char c ;
cin>>n;
while(n[0]!='0')
{
cin>>id>>age>>cpp_score>>cpp_count;
//cin.get(c); 可不加这句
cout<<id<<age<<cpp_score<<cpp_count;
st.set(n,id,age,cpp_score,cpp_count);///声明初始化
stu[i]=st;
//stu[i].print();
i++;
cin>>n;
}
int j=i-1;
for(i=1;i<=j;i++)
stu[i].print();
return 0;
}