#include<iostream>
#include<string>
using namespace std;
struct Student
{
string number;
string name;
double grade;
void functionZero()
{
int a;
string b;
string c;
for (int i = 0; i < 11; i++)
{
cout << "请输入第" << i << "个同学的学号" << endl;
cin >> b;
stu[i].number = b;
cout << "请输入第" << i << "个同学的姓名" << endl;
cin >> c;
stu[i].name = c;
cout << "请输入" << stu[i].number << stu[i].name << "的成绩" << endl;
cin >> a;
if (a >= 0)
{
stu[i].grade = a;
}
else
{
cout << "成绩不能为负数" << endl;
}
}
}
void functionOne()
{
int a = 0;
cout << "不及格学生名单" << endl;
for (int i = 0; i < 10; i++)
{
if (stu[i].grade < 60)
{
cout << "学号" << stu[i].number << "姓名" << stu[i].name << "成绩" << stu[i].grade << endl;
a++;
}
}
cout << "不及格学生人数:" << a << endl;
}
void functionTwo()
{
int student[6] = { 0 };
cout << "各分数段的学生人数名单" << endl;
for (int i = 0; i < 10; i++)
{
if (stu[i].grade < 60)
{
student[0]++;
}
else if (60<=stu[i].grade&& stu[i].grade<70)
{
student[1]++;
}
else if (70 <= stu[i].grade&&stu[i].grade < 80)
{
student[2]++;
}
else if (80 <= stu[i].grade&&stu[i].grade < 90)
{
student[3]++;
}
else if (90 <= stu[i].grade&&stu[i].grade < 99)
{
student[4]++;
}
else if (stu[i].grade == 100)
{
student[5]++;
}
}
cout << "第0段人数:" << student[0] << "所占百分比:" << student[0] << "0%" << endl;
cout << "第1段人数:" << student[1] << "所占百分比:" << student[1] << "0%" << endl;
cout << "第2段人数:" << student[2] << "所占百分比:" << student[2] << "0%" << endl;
cout << "第3段人数:" << student[3] << "所占百分比:" << student[3] << "0%" << endl;
cout << "第4段人数:" << student[4] << "所占百分比:" << student[4] << "0%" << endl;
cout << "第5段人数:" << student[5] << "所占百分比:" << student[5] << "0%" << endl;
}
}stu[10];
int main()
{
Student p;
struct Student stu[10];
p.functionZero();
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " 学生成绩统计 " << endl;
cout << "退出程序 使用请按0" << endl;
cout << "统计不及格人数并打印不及格学生名单 使用请按1 " << endl;
cout << "统计各分数段的学生人数及所占的百分比 使用请按2 " << endl;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
while (1)
{
int x;
cout << "请输入序号" << endl;
cin >> x;
if (x == 0)
{
return 0;
}
else if (x == 1)
{
p.functionOne();
}
else if (x == 2)
{
p.functionTwo();
}
else
{
cout << "输入有误" << endl;
}
return 0;
}
}
struct Student
{
改为
class Student
{
public:
再参考这个改下,
class Student
{
public:
void functionZero() {***}
改成
class Student
{
public:
void functionZero();
}stu[10];
void Student::functionZero()
{
***
}
struct可以直接改成class
struct 和 class的区别是,struct中的变量和方法默认都是public类型,而class的变量和方法默认为private类型