ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++学生成绩管理系统

2019-12-15 18:01:47  阅读:198  来源: 互联网

标签:std return name puts 管理系统 int C++ 成绩 string


#include <map>
#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
class Courses
{
private:
    std::map<std::string, int> c;   //课程名字到编号的映射,编号从0开始
    std::vector<std::string> names; //课程编号到名字的映射
public:
    int getNum() { return c.size(); } //课程总数
    int addCourse(std::string name)   //返回新ID
    {
        int temp = c.size();
        c[name] = temp;
        names.push_back(name);
        return temp;
    }
    int findCourseId(std::string name) //课程名字寻找课程ID,找不到返回-1
    {
        if (c.find(name) != c.end())
            return c[name];
        else
            return -1;
    }
    std::string queryName(int id) //询问课程名称
    {
        if (id < 0 || id >= c.size())
            return std::string("");
        return names[id];
    }
    void display() //输出表头
    {
        printf("\t");
        for (auto i : names)
            printf("%s\t", i.c_str());
        printf("总分\t平均分\t排名\n");
    }
} courses;

class Student
{
private:
    std::string name;
    std::map<int, double> score;
    double totScore = 0;
public:
    bool operator<(const Student &x) const
    { //输出时候除以课程总数就行了courses.getNum()
        return totScore > x.totScore;
    }
    friend class Students;
};

class Students
{
private:
    std::map<std::string, int> mp; //名字到下标的映射
    std::vector<Student> s;        //下标到名字的映射
public:
    int getNum(){return s.size();}
    int findName(std::string name)
    {
        std::map<std::string, int>::iterator it = mp.find(name);
        if (it == mp.end()) return 0;
        return it->second;
    }
    void addStudent(std::string name) //录入学生成绩
    {
        Student temp;
        temp.name = name;
        int totCourse = courses.getNum();
        for(int i=0;i<totCourse;i++)
        {
            std::cout<<"请输入"<<courses.queryName(i)<<"成绩:"<<std::endl;
            double ss;
            std::cin>>ss;
            temp.score[i]=ss;
            temp.totScore+=ss;
        }
        s.push_back(temp);
        mp[name] = mp.size();
        return; //成功
    }
    double queScore(std::string name,int courseId)
    {
        std::map<std::string, int>::iterator it = mp.find(name);
        if (it == mp.end())
            return -1.0;
        int sid=it->second;
        return s[sid].score[courseId];
    }
    bool changeScore(std::string name, int courseId, double newscore) //返回是否成功
    {
        std::map<std::string, int>::iterator it = mp.find(name);
        if (it == mp.end())
            return false;
        int sid = it->second;
        int temp = s[sid].score[courseId];
        s[sid].score[courseId] = newscore;
        s[sid].totScore += newscore - temp;
        return true;
    }
    void displayStudent(std::string name) //输出一个学生的成绩,不输出表头
    {
        int sid = mp[name];
        displayStudent(sid);
    }
    void displayStudent(int sid)     //输出一个学生的成绩,不输出表头
    {
        std::cout<<s[sid].name<<"\t";
        for (int i = 0; i < courses.getNum(); i++)
        {
            printf("%.1f\t", s[sid].score[i]);
        }
        printf("%.1f\t%.1f\t%d/%u\n", s[sid].totScore, s[sid].totScore / courses.getNum(), sid + 1, mp.size());
    }
    
    void sort()
    {
        std::sort(s.begin(), s.end());
        int temp = 0;
        for (auto i : s)
        {
            mp[i.name] = temp++; //重排mp
        }
    }
} students;

class FrontPage{
private:
    inline void read(int &x)
    {
        int s = 0, w = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9')
        {
            if (ch == '-')
                w = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9')
            s = s * 10 + ch - '0', ch = getchar();
        x = s * w;
    }
    int temp;
public:
    void run()
    {
        printf("启动中");
        for (int i = 1; i <= 5; i++)
            system("sleep 0.1"), printf(".");
        system("cls");
        printf("欢迎使用成绩管理系统!\n请输入科目数量:\n");
        read(temp);
        puts("请输入每个科目的名称,用回车键分隔");
        for(int i=0;i<temp;i++)
        {
            std::string name;
            std::cin>>name;
            courses.addCourse(name);
        }
        puts("请输入学生数量");
        read(temp);
        puts("开始录入成绩");
        for(int i=1;i<=temp;i++)
        {
            std::string name;
            puts("输入姓名:");
            std::cin>>name;
            students.addStudent(name);
            puts("添加成功");
        }
        students.sort();
        while (1)
        {
            puts("添加学生请输入1\n修改学生成绩请输入2\n查询某学生成绩请输入3\n按排名输出成绩单请输入4");
            int type = 0;
            read(type); //提高容错性
            if (type == 1)
            {
                std::string name;
                puts("输入姓名:");
                std::cin>>name;
                students.addStudent(name);
                puts("添加成功");
                students.sort();
                system("sleep 2");
                system("cls");
                continue;
            }
            if (type == 2)
            {
                std::string name;
                puts("姓名:");
                std::cin >> name;
                if(!students.findName(name))
                {
                    puts("查无此人");
                    system("sleep 2");
                    system("cls");
                    continue;
                }
                puts("要修改的科目名称:");
                std::string cour;
                int courid;
                while (1)
                {
                    std::cin>>cour;
                    if(cour==std::string("@$#")) goto cao;
                    courid = courses.findCourseId(cour);
                    if(courid==-1)
                    {
                        puts("查无此科目,请重新输入。返回上层请输入@$#");
                    }
                    else break;
                }
                std::cout<< "原先成绩为" << students.queScore(name, courid)<<",请输入新的成绩:" << std::endl;
                double newsc;
                std::cin>>newsc;
                students.changeScore(name,courid,newsc);
                puts("修改成功");
                students.sort();
                system("sleep 2");
                cao:
                system("cls");
                continue;
            }
            if(type==3)
            {
                std::string name;
                puts("输入姓名:");
                std::cin>>name;
                courses.display();
                students.displayStudent(name);
                continue;
            }
            if(type==4)
            {
                courses.display();
                for(int i=0,sz=students.getNum();i<sz;i++)
                {
                    students.displayStudent(i);
                }
                continue;
            }
            puts("输入错误");
            system("sleep 2");
        }
    }
};

int main()
{
    FrontPage p;
    p.run();
    return 0;
}

标签:std,return,name,puts,管理系统,int,C++,成绩,string
来源: https://www.cnblogs.com/wawcac-blog/p/12045257.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有