用c++编写一个班里每个学生的三门成绩各门优秀 良好 及格和不及格的人数

用类写,还有最好用一个函数。
2025-03-13 07:48:05
推荐回答(4个)
回答1:

#include
#include//包含格式的头文件
#define N 3 //学生人数设为3
class score //定义成绩类
{
private:
float score1,score2,score3; //三门成绩
public:
score() //构造函数
{
score1=score2=score3=0;
}

void inputscore(float sc1,float sc2,float sc3);//输入三门课成绩的函数
float getscore(int i); //获得想要学生某门课的成绩
void show(); //显示出三门课成绩
};
//以下为函数的实现过程
void score::inputscore(float sc1,float sc2,float sc3)
{
score1=sc1;
score2=sc2;
score3=sc3;

}
float score::getscore(int i)
{
if(i==0)return score1;
else if(i==1)return score2;
else if(i==2)return score3;

}
void score::show()
{
cout<
}
//主函数
void main()
{
score student[N];
float s1,s2,s3;
cout<<"输入每个学生三门课的成绩(百分制):"< //输入N个学生的三门课成绩N可自己设置 暂设为3个学生
for(int i=0;i {
cin>>s1>>s2>>s3;
student[i].inputscore(s1,s2,s3);

}

cout<<"------------------------------------------"< //输出每位学生成绩
cout< for(i=0;i {
cout<<"第"< student[i].show();

}
//输出每门课程的不及格 及格 良好 优秀人数
cout<<"------------------------------------------------"< for(int j=0;j<3;j++)
{
int best=0,good=0,pass=0,fail=0;
for(i=0;i {
if(student[i].getscore(j)>0&&student[i].getscore(j)<60)fail++;
else if(student[i].getscore(j)>=60&&student[i].getscore(j)<80)pass++;
else if(student[i].getscore(j)>=80&&student[i].getscore(j)<90)good++;
else if(student[i].getscore(j)>=90&&student[i].getscore(j)<100)best++;
}
cout<<"第"< }

}

回答2:

#include
#include

class student
{
public:
int math;
int chinese;
int english;
public:
student();
void addmark(int m,int c,int e);
void editmark(std::string subject,int mark);
int getmark(std::string subject);
};

student::student()
{
}

void student::addmark(int m, int c, int e)
{
math=m;
chinese=c;
english=e;
}

void student::editmark(std::string subject, int mark)
{
if(subject=="math")
math=mark;
else if(subject=="chinese")
chinese=mark;
else if(subject=="english")
english=mark;
}

int student::getmark(std::string subject)
{
int mark;
if(subject=="math")
mark=math;
else if(subject=="chinese")
mark=chinese;
else if(subject=="english")
mark=english;
return mark;
}

int main()
{
int mp=0,cp=0,ep=0;
student stu[10];
for(int i=0;i<10;i++)
{
int m,c,e;
//std::cin>>stu[i].math< std::cin>>m>>c>>e;
stu[i].addmark(m,c,e);
}
for(int i=0;i<10;i++)
{
if(stu[i].math>=60)
mp++;
if(stu[i].chinese>=60)
cp++;
if(stu[i].english>=60)
ep++;
}
std::cout<<"mp:"< std::cout<<"cp:"< std::cout<<"ep:"< while(1)
getchar();
}
调试可成 比较烂了 根据实际情况 弄个学生类指针 随便多少学生都可以了

回答3:

#include
#include
using namespace std;
class student
{
private: char name[10];
char sex[5];
char number[7];
int age;
public: input();
print();
modify();
};
student a[3];
student::input()
{

cout<<"please input the student's name,sex,number and age:"< for(int i=0;i<3;i++)
{
cin>>a[i].name>>a[i].sex>>a[i].number>>a[i].age;
}
}
student::print()
{
cout<<"the student's message is:"< for(int i=0;i<3;i++)
cout<}
student::modify()
{
char str[5];
cout<<"please input the student's number who you want to modify:"< cin>>str;
for(int i=0;i<3;i++)
{
if(strcmp(str,a[i].number)==0)
{
cout<<"please input the new student's name,sex,number and age:"< cin>>a[i].name>>a[i].sex>>a[i].number>>a[i].age;
break;
}
}
}
void main()
{
student s;
s.input();
s.print();
s.modify();
s.print();
}

回答4:

这个是在这里说得清楚的?