这个源程序怎么写啊,完全不会

1.输入所有同学同一门课程的成绩
2.找出成绩的最高分
3.找出成绩的最低分
4.求出成绩的平均分
5.统计成绩中任意一个分数段的学生人数
6.将最低分放在所有成绩的第一位(换成最高分也可以)
7.输出所有成绩
要求:(1)编写的程序要体现出函数、数组的应用。
(2)程序演示要体现上述功能。

img


代码如下:

#include<stdio.h>
#define MAX 100
int FindMax(int a[],int n)  //输入参数为数组及其长度
{
    int max = a[0];
    for (int i = 0; i < n; i++)
        if (a[i] > max)
            max = a[i];
    return max;           //函数返回值为数组最大值
}
int FindMin(int a[], int n)  //输入参数为数组及其长度
{
    int min = a[0];
    for (int i = 0; i < n; i++)
        if (a[i] < min)
            min = a[i];
    return min;           //函数返回值为数组最小值
}
double average(int a[], int n)
{
    int sum=0;
    double ave;
    for (int i = 0; i < n; i++)
        sum += a[i];
    ave = (double)sum / n;
    return ave;//函数返回值为数组平均值
}
int sta_num(int a[], int n)
{
    int sta_min, sta_max,count=0;
    printf("请输入要统计的分数段(用空格隔开,如:20 90 表示统计20到90分数段):");
    scanf_s("%d %d", &sta_min, &sta_max);
    for (int i = 0; i < n; i++)
        if (a[i] >= sta_min && a[i] <= sta_max)
            count++;
    return count;
}
void PutMinFirst(int a[], int n)
{
    for(int i=0;i<n-1;i++)        //冒泡排序将分数从小到大排序
        for(int j=0;j<n-1-i;j++)
            if (a[j] > a[j + 1])
            {
                int temp;
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
}
void Print(int a[], int n)
{
    for (int i = 0; i < n; i++)
        printf("%d\t", a[i]);
}
int main()
{
    int n;//同学人数
    int a[MAX];//成绩数组
    printf("请输入学生人数:");
    scanf_s("%d", &n);
    printf("请输入%d个学生成绩:",n);
    for (int i = 0; i < n; i++)
        scanf_s("%d", &a[i]);//输入n个同学的成绩
    printf("max=%d\n", FindMax(a, n));           //求最大数并输出
    printf("min=%d\n", FindMin(a, n));            //求最小数并输出
    printf("average=%lf\n", average(a, n));            //求平均数并输出
    printf("该分数段人数=%d\n", sta_num(a, n));            //求某分数段并输出其人数
    PutMinFirst(a, n);                            //冒泡排序将分数从小到大排序
    Print(a, n);                                 //求某分数段并输出其人数
    return 0;
}

参考一下


#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<Windows.h>
using namespace std;
class Student        //学生类
{
public:
    Student( string name, int id, string sex)
    {
        this->name = name;
        this->id = id;
        this->sex = sex;
    }
    string name;
    int id;
    string sex;
};

class Grade            //课程类
{
public:
    string English;
    string Math;
    string C_plus;
    string Analog_circuit;
    string Sports;
    string Matlab;
    string Data_structure;
    string Physical;
};

class Score : public Grade,virtual public Student    //成绩类
{
public:
    Score(string name, int id, string sex, float English_score, float Math_score, float C_plus_score, float Analog_circuit_score, float Sports_score, float Matlab_score, float Data_structure_score, float Physical_score) :Student(name, id, sex)
    {
        this->English_score = English_score;
        this->Math_score = Math_score;
        this->C_plus_score = C_plus_score;
        this->Analog_circuit_score = Analog_circuit_score;
        this->Sports_score = Sports_score;
        this->Matlab_score = Matlab_score;
        this->Data_structure_score = Data_structure_score;
        this->Physical_score = Physical_score;
        GPA = (English_score + Math_score + C_plus_score + Analog_circuit_score + Sports_score + Matlab_score + Data_structure_score + Physical_score) / 8.0;
    }
    float English_score;
    float Math_score;
    float C_plus_score;
    float Analog_circuit_score;
    float Sports_score;
    float Matlab_score;
    float Data_structure_score;
    float Physical_score;
    float GPA;


};
class stusystem
{
public:
    stusystem()    //打开文件
    {
        ifstream ifs("学生成绩.txt", ios::in);
        if (ifs.is_open() != NULL)
        {
            string name, sex;
            int id;
            float English_score;
            float Math_score;
            float C_plus_score;
            float Analog_circuit_score;
            float Sports_score;
            float Matlab_score;
            float Data_structure_score;
            float Physical_score;
            float GPA;    //说实话你设的这变量名真的长
            while (ifs >> name >> id >> sex >> English_score >> Math_score >> C_plus_score >> Analog_circuit_score >> Sports_score >> Matlab_score >> Data_structure_score >> Physical_score)
            {
                Score s(name , id , sex , English_score , Math_score , C_plus_score , Analog_circuit_score , Sports_score , Matlab_score , Data_structure_score , Physical_score);
                a.push_back(s);
            }
        }
        else
        {
            cout << "不存在该文件" << endl;

        }
    }
    vector<Score>a;
    void Save();       //保存
    void Add();        //添加
    void AddAgain();   //再度添加
    void Del();        //删除
    void Find();       //查找菜单
    void Findname();   //查找名字
    void Findid();     //查找学号
    void Sort();       //排序
    void Remake();     //重新编辑
    void Print();      //打印
    void mainmenu()    //主菜单
    {
        system("cls");
        cout << "____________________________________________" << endl;
        cout << "|                                          |" << endl;
        cout << "|    添加学生信息                     1    |" << endl;
        cout << "|    查找学生信息                     2    |" << endl;
        cout << "|    GPA排名                          3    |" << endl;
        cout << "|    修改学生信息                     4    |" << endl;
        cout << "|    删除学生信息                     5    |" << endl;
        cout << "|    打印学生信息                     6    |" << endl;
        cout << "|    退出系统                         0    |" << endl;
        cout << "|                                          |" << endl;
        cout << "--------------------------------------------" << endl;
        cout << "请输入你的选择:";
        int x;
        cin >> x;
        switch (x)
        {
        case 1:Add(); break;
        case 2:Find(); break;
        case 3:Sort(); break;
        case 4:Remake(); break;
        case 5:Del(); break;
        case 6:Print(); break;
        case 0:Save(); break;
        default:cout << "输入错误,请重新输入\n"; Sleep(1000); mainmenu(); break;
        }
    }
};

void stusystem::Save()  //保存文件
{
    ofstream ofs("学生成绩.txt", ios::out);
    for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
    {
        ofs << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t" << (*i).English_score << "\t" <<
            (*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
            << "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t" << (*i).GPA;
    }
    ofs.close();
}

void stusystem::Add()  //添加
{
    system("cls");
    int flag;
    
    cout << "请输入学生信息:" << endl;
    string name, sex;
    int id;
    float eng, math, cplus, mndl, ty, matlab, sjjg, dw;
    while (1)
    {
        cout << "姓名:";
        cin >> name;
        
        cout << "学号:";
        cin >> id;
        for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
        {
            if ((*i).id == id)
            {
                cout << "此同学信息已经存在,请重新输入";
                Sleep(700);
                Add();
            }
        }
        cout << "性别:";
        cin >> sex;
        do {
            flag = 0;
            cout << "英语学分:";
            cin >> eng;
            if (eng > 5.0 || eng < 0) {
                cout << " 对不起,请输入0-5.0之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "高数学分:";
            cin >> math;
            if (math > 5.00 || math < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "C++学分:";
            cin >> cplus;
            if (cplus > 5.00 || cplus < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "模拟电路学分:";
            cin >> mndl;
            if (mndl > 5.00 || mndl < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "体育学分:";
            cin >> ty;
            if (ty > 5.00 || ty < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "Matlab学分:";
            cin >> matlab;
            if (matlab > 5.00 || matlab < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "数据结构学分:";
            cin >> sjjg;
            if (sjjg > 5.00 || sjjg < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        do {
            flag = 0;
            cout << "大学物理学分:";
            cin >> dw;
            if (dw > 5.00 || dw < 0) {
                cout << " 对不起,请输入0-5.00之间的数字!!\n";
            }
            else {
                flag = 1;
            }
        } while (flag == 0);
        Score s(name, id, sex, eng, math, cplus, mndl, ty, matlab, sjjg, dw);
        a.push_back(s);  //vector头文件中的push_back操作符,意思是插入到表尾
        Save();
        cout << "添加成功" << endl;
        AddAgain();    
    }
}
void stusystem::AddAgain()
{
        cout << "是否继续添加学生成绩信息?(y/n)" << endl;
        char sign = '0';
        cin >> sign;
        switch (sign) {
        case'y':
        case'Y':
            Add(); break;
        case'n':
        case'N':
            mainmenu(); break;
        default:cout << "输入错误,请重新输入\n"; Sleep(1000); AddAgain(); break;
        }
}
void stusystem::Find()
{
    system("cls");
    cout << "查找学生成绩:" << endl;
    cout << "请选择查询方式:" << endl;
    cout << "1-姓名查询" << endl << "2-学号查询" << endl;
    int x = 0;    
    cin >> x;
    switch (x)
    {
    case 1:
        Findname(); break;
    case 2:
        Findid(); break;
    }
}
void stusystem::Findname()
{
    
    string s; 
    bool flag = false;         //异常解决方案
    cout << "请输入需查找的学生姓名:";
    cin >> s;
    for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
    {
        if ((*i).name == s)
        {
            flag = true;
            cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
                << "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
                << "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
            cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
                (*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
                << "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
            cout << "\n按任意键返回主菜单"; char ch;
            cin >> ch; mainmenu();
        }
        if (flag == false)
        {
            cout << "查无此人!" << endl;
            Findname();
        }
    }
}
void stusystem::Findid()
{
    int y=0;
    bool flag = false;         //异常解决方案
    cout << "请输入需查找的学生学号:";
    cin >> y;
    for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
    {
        if ((*i).id == y)
        {
            flag = true;
            cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
                << "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
                << "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
            cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
                (*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
                << "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
            cout << "\n按任意键返回主菜单"; char ch;
            cin >> ch; mainmenu();
        }
        if (flag == false)
        {
            cout << "查无此人!" << endl;
            Findid();
        }
    }
}

void stusystem::Del()   //删除
{
    cout << "请输入学生的学号" << endl;
    int id;
    cin >> id;
    for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
    {
        if ((*i).id == id)
        {
            int select;
            cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
                << "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
                << "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
            cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
                (*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
                << "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
            cout << "是否删除?" << endl;
            cout << "1.是" << "   " << "2.否" << endl;
            cin >> select;
            switch (select)
            {
            case 1:
                a.erase(i);  //以string.h为头文件的删除函数
                cout << "删除成功" << endl; Sleep(100); mainmenu(); break;
            case 2:
                cout << "您未删除" << endl; Sleep(100); mainmenu(); break;
            default:cout << "输入错误,将返回主菜单\n"; Sleep(1000); mainmenu(); break;
            }
            if ((*i).id != id)
            {
                cout << "查无此人" << endl;
            }

        }
    }
    Save();
    cout << "\n按任意键返回主菜单"; char ch;
    cin >> ch; Sleep(500); return;
}
void stusystem::Remake()
{
    system("cls");
    cout << "修改学生成绩:" << endl;
    cout << "请输入要修改的学生学号:";
    int id;
    cin >> id;
    for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
    {
        if ((*i).id == id)
        {
            cout << "已找到该学生,请重新输入该学生的信息" << endl;
            int ID;
            string name, sex;
            float eng, math, cplus, mndl, ty, matlab, sjjg, dw;
            cout << "姓名:"; cin >> name;
            cout << "学号:"; cin >> id;
            cout << "性别:"; cin >> sex;
            cout << "英语:"; cin >>eng;
            cout << "高数:"; cin >> math;
            cout << "C++:"; cin >>cplus;
            cout << "模拟电路:"; cin >> mndl;
            cout << "体育:"; cin >> ty;
            cout << "Matlab:"; cin >> matlab;
            cout << "数据结构:"; cin >> sjjg;
            cout << "大学物理:"; cin >> dw;
            char c;
            cout << "是否保存修改?(y/n)";
            cin >> c;
            switch (c) {
            case'y':
            case'Y':
                (*i).name = name; (*i).id = id; (*i).sex = sex; (*i).English_score = eng; (*i).Math_score = math;
                (*i).C_plus_score = cplus; (*i).Analog_circuit_score = mndl; (*i).Sports_score = ty; (*i).Physical_score = dw; break;
            case'n':
            case'N':
                 break;
            default:cout << "输入错误,将返回主菜单\n"; Sleep(1000); mainmenu(); break;

            }
        }
    }
    Save();
    cout << "\n按任意键返回主菜单"; char ch;
    cin >> ch; Sleep(500); return;
}
bool mysort(Score a,Score b)//自定义排序规则
{
    if (a.GPA == b.GPA) {
        return a.id > b.id;         //若gpa相同则按学号从小到大排列
    }
    else {
        return a.GPA > b.GPA;       //gpa从大到小排
    }
}
void stusystem::Sort()
{
    system("cls");
    if (a.size() == 0)
        cout << "文件不存在" << endl;
    else {
        sort(a.begin(), a.end(), mysort);   //排序函数从向量表头至表尾使用自定义mysort规则
        cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
            << "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
            << "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
        for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
        {
            cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
                (*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
                << "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
        }
        cout << "\n按任意键返回主菜单"; char ch;
        cin >> ch; Sleep(500); return;
    }
}
void stusystem::Print()
{
    system("cls");
    if (a.size() == 0)
        cout << "文件不存在" << endl;
    else {
        cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "  " << "各科学分" << "  " << "英语" << "  " << "高等数学"
            << "  " << "C++" << "  " << "模拟电路" << "  " << "体育" << "  " << "Matlab"
            << "  " << "数据结构" << "  " << "大学物理" << "   " << "GPA" << endl;
        for (vector<Score>::iterator i = a.begin(); i != a.end(); i++)
        {
            cout << (*i).name << "\t" << (*i).id << "\t" << (*i).sex << "\t    \t" << (*i).English_score << "\t" <<
                (*i).Math_score << "\t" << (*i).C_plus_score << "\t" << (*i).Analog_circuit_score << "\t" << (*i).Sports_score
                << "\t" << (*i).Matlab_score << "\t" << (*i).Data_structure_score << "\t" << (*i).Physical_score << "\t  " << (*i).GPA << endl;
        }
    }
    cout << "\n按任意键返回主菜单"; char ch;
    cin >> ch; Sleep(500); return;
}
int main()
{
    stusystem stu;
    while (1)
    {
        stu.mainmenu();
    }
    return 0;
}

针对每一个问题,找出输入——处理——输出,三个过程

1.输入所有同学同一门课程的成绩(对输入的数据利用数组存储)。 输入为键盘或者文件,处理为输入变成输出的过程,输出为数组存储所输入的数据,下面类似
2.找出成绩的最高分 (使用函数计算数组的最高分——查找算法)
3.找出成绩的最低分(使用函数计算数组的最低分——查找算法)
4.求出成绩的平均分(使用函数计算数组的平均分——遍历每个数组元素,求和,然后求平均值)
5.统计成绩中任意一个分数段的学生人数(使用函数计算分数段的学生人数——遍历每个数组元素,找到符合条件的人就把结果数值加1,即可得到最终结果)
6.将最低分放在所有成绩的第一位(换成最高分也可以)——对数组元素进行排序,如果按照从小到大顺序,排序之后逆序输出,第一个为最大,正序输出,结果为最小
7.输出所有成绩 ——遍历数组元素,对每个元素进行输出