用C++编写一个成绩管理系统

1.插入学生成绩。
2.查询成绩,按照输入的姓名或学号查询对应成绩。
3.显示所有成绩信息。
4.输入姓名,修改成绩信息。
5.给定姓名,删除该成绩信息。
6.统记人数,计算总分,平均分,最高分,最低分。
7.保存成绩到文件。
8.从文件读取成绩。
9.给成绩排序(可选排序字段及升,降序)。

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int id,num,xuehao,tmp;
double scoure,res,loww,highh;
string name;
map<string,double>map_str;
map<int,double>map_int;
vector<pair<string,int> >v;
bool cmp_str_high_first(pair<string,int> a,pair<string,int> b) {
    return a.first>b.first;
}
bool cmp_str_low_first(pair<string,int> a,pair<string,int> b) {
    return a.first<b.first;
}
bool cmp_int_high_first(pair<string,int> a,pair<string,int> b) {
    return a.second>b.second;
}
bool cmp_int_low_first(pair<string,int> a,pair<string,int> b) {
    return a.second<b.second;
}
bool cmp_scoure_high_first(pair<string,int> a,pair<string,int> b) {
    return map_int[a.second]>map_int[b.second];
}
bool cmp_scoure_low_first(pair<string,int> a,pair<string,int> b) {
    return map_int[a.second]<map_int[b.second];
}
int main() {
begin:
    puts("请问我能为您做什么?");
    puts("我的功能有:");
    puts(
        "1.插入学生成绩。\n"
        "2.查询成绩,按照输入的姓名或学号查询对应成绩。\n"
        "3.显示所有成绩信息。\n"
        "4.输入姓名,修改成绩信息。\n"
        "5.给定姓名,删除该成绩信息。\n"
        "6.统记人数,计算总分,平均分,最高分,最低分。\n"
        "7.保存成绩到文件并退出。\n"
        "8.从文件读取成绩。\n"
        "9.给成绩排序(可选排序字段及升,降序)。\n"
    );
    puts("请输入编号:");
    cin>>id;
    system("cls");
    switch(id) {
        case 1:
            puts("请输入学生数");
            cin>>num;
            for(int i=1; i<=num; i++) {
                puts("请输入学号");
                cin>>xuehao;
                puts("请输入名字");
                cin>>name;
                puts("请输入成绩");
                cin>>scoure;
                map_str[name]=map_int[xuehao]=scoure;
                v.push_back(make_pair(name,xuehao));
            }
            break;
        case 2:
            puts("请输入查询次数");
            cin>>num;
            for(int i=1; i<=num; i++) {
                puts("如需用学号查询,请输入1,用名字查询,请输入2");
                cin>>tmp;
                if(tmp==1) {
                    puts("请输入学号");
                    cin>>xuehao;
                    printf("%d号的成绩为%.2lf\n",xuehao,map_int[xuehao]);
                } else {
                    puts("请输入名字");
                    cin>>name;
                    printf("%s的成绩为%.2lf\n",name.c_str(),map_str[name]);
                }
            }
            break;
        case 3:
            if(v.empty())
                puts("还没有学生。");
            else
                for(vector<pair<string,int> >::iterator it=v.begin(); it!=v.end(); it++)
                    printf("%s,%d号,成绩为:%.2lf\n",it->first.c_str(),it->second,map_str[it->first]);
            break;
        case 4:
            puts("请输入您要修改的学生的姓名");
            cin>>name;
            tmp=0x7fffffff;
            for(unsigned i=0; i<v.size(); i++)
                if(v[i].first==name) {
                    tmp=i;
                    xuehao=v[i].second;
                    break;
                }
            if(tmp==0x7fffffff) {
                puts("学生不存在!");
                break;
            }
            printf("请输入%s的修改后分数:",name.c_str());
            cin>>scoure;
            map_str[name]=map_int[xuehao]=scoure;
            puts("修改完毕!");
            break;
        case 5:
            puts("请输入您要删除的学生的姓名");
            cin>>name;
            tmp=0x7fffffff;
            for(vector<pair<string,int> >::iterator it=v.begin(); it!=v.end(); it++)
                if(it->first==name) {
                    tmp=1;
                    v.erase(it);
                    xuehao=it->second;
                    break;
                }
            if(tmp==0x7fffffff) {
                puts("学生不存在!");
                break;
            }
            map_int[xuehao]=map_str[name]=0;
            puts("删除完毕!");
            break;
        case 6:
            res=0,highh=-1,loww=10000001;
            for(unsigned i=0; i<v.size(); i++)
                res+=map_int[v[i].second],highh=max(highh,map_int[v[i].second]),loww=min(loww,map_int[v[i].second]);
            if(v.size())
                printf("人数:%u,总分:%.2lf,平均分:%.3lf,最高分:%.2lf,最低分:%.2lf\n",unsigned(v.size()),res,double(res)/v.size(),highh,loww);
            else
                printf("没有学生!");
            break;
        case 7:
            puts("文件保存在目录下的'scoure.txt':");
            freopen("scoure.txt","w",stdout);
            for(unsigned i=0; i<v.size(); i++)
                printf("%s %d -> %.2lf\n",v[i].first.c_str(),v[i].second,map_int[v[i].second]);
            return 0;
        case 8:
            puts("正在读取'scoure.txt':");
            freopen("scoure.txt","r",stdin);
            while(cin>>name>>xuehao>>scoure) {
                v.push_back(make_pair(name,xuehao));
                map_int[xuehao]=map_str[name]=scoure;
            }
            puts("读取完毕!");
            break;
        case 9:
r:
            puts("排序:请输入关键字");
            puts("1:按名字降序排");
            puts("2:按名字升序排");
            puts("3:按学号降序排");
            puts("4:按学号升序排");
            puts("5:按成绩降序排");
            puts("6:按成绩升序排");
            cin>>tmp;
            if(v.size()) {
                if(tmp==1)
                    sort(v.begin(),v.end(),cmp_str_high_first);
                else if(tmp==2)
                    sort(v.begin(),v.end(),cmp_str_low_first);
                else if(tmp==3)
                    sort(v.begin(),v.end(),cmp_int_high_first);
                else if(tmp==4)
                    sort(v.begin(),v.end(),cmp_int_low_first);
                else if(tmp==5)
                    sort(v.begin(),v.end(),cmp_scoure_high_first);
                else if(tmp==6)
                    sort(v.begin(),v.end(),cmp_scoure_low_first);
                else {
                    puts("请重新输入");
                    system("cls");
                    goto r;
                }
            } else {
                printf("没有学生!");
            }
            break;
        default:
            puts("???");
            break;
    }
    puts("按任意键继续");
    getch();
    system("cls");
    goto begin;
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-01-11
#include <iostream>
#include<fstream>
using namespace std;
const int N=5
;
class student
{
char n_name[10];
char n_numble[5];
char n_sex[3];
float score[4];//score[0]英语成绩,score[1]数学成绩,score[2]语文成绩,score[3]平均分
public:
student();
student(char name[],char numble[],char sex[],float a,float b,float c);
student(student &s);
void input();//输入学生信息:姓名,学号,性别,英语,数学,语文
float pj();//获取平均分

void stu_delete(student stu[]);//删除
void stu_find(student stu[]);
void dispaly();//显示学生信息:姓名,学号,性别,英语,数学,语文,平均分
};
student::student()
{
strcpy(n_numble,"000");
strcpy(n_name,"noname");
strcpy(n_sex,"male");
score[0]=88;
score[1]=99;
score[2]=95;
score[3]=94;
}
student::student(char name[],char numble[],char sex[],float a,float b,float c)
{
strcpy(n_name,name);
strcpy(n_numble,numble);
strcpy(n_sex,sex);
score[0]=a;
score[1]=b;
score[2]=c;
score[3]=(score[0]+score[1]+score[2])/3.0;
}
student::student(student &s)
{
strcpy(n_name,s.n_name);
strcpy(n_numble,s.n_numble);
strcpy(n_sex,s.n_sex);
score[0]=s.score[0];
score[1]=s.score[1];
score[2]=s.score[2];
score[3]=(score[0]+score[1]+score[2])/3.0;

}
void student::input()
{
cout<<"输入学生信息:"<<endl;
cout<<"请依次输入姓名,学号,性别,英语,数学,语文"<<endl;
cin>>n_name>>n_numble>>n_sex>>score[0]>>score[1]>>score[2];
score[3]=(score[0]+score[1]+score[2])/3.0;
}
float student::pj()
{
return score[3];
}
void student::stu_delete(student stu[])
{
student t;
int i,k,j;
char d_numble[5];
cout<<"请输入要删除的学生学号:";
cin>>d_numble;
for(i=0;i<N+1;i++)
{
j=i;
k=strcmp(stu[i].n_numble,d_numble);
if(k==0&&i!=N)
{
for(;j<N;j++)
stu[j]=stu[j+1];
}
if(k==0&&i==N)
{
strcpy(stu[N].n_numble,"0000");
strcpy(stu[N].n_name,"noname");
strcpy(stu[N].n_sex,"男");
stu[N].score[0]=0;
stu[N].score[1]=0;
stu[N].score[2]=0;
stu[N].score[3]=0;
}
}
}
void student::stu_find(student stu[])
{
int i,k;
char d_numble[5];
cout<<"请输入要查询学生的学号:";
cin>>d_numble;
for(i=0;i<N+1;i++)
{
k=strcmp(stu[i].n_numble,d_numble);
if(k==0)
{
stu[i].dispaly();

}
}
}
void student::dispaly()
{
cout<<"姓名:"<<n_name<<" "<<"学号:"<<n_numble<<" "<<"性别:"<<n_sex<<" "<<"英语:"<<score[0]<<" "<<"数学:"<<score[1]<<" "<<"语文:"<<score[2]<<" "<<"平均分:"<<score[3]<<endl;
}
class manage_stu
{
//private:
student stu[N+1];//定义N+1名学生空间
public:
void input_stu();//输入N名学生信息

void delete_stu();//删除指定学号的学生信息
void find_stu();//查找指定学号的学生并显示
void total_stu();//分别求每个人的总分
void sort_stu();//按总分高低排序,排序后的结果显示并生成文件sortstu.txt
void display_stu();//显示N名学生信息
};
void manage_stu::input_stu()//输入N名学生信息
{
int i;
for(i=0;i<N;i++)
stu[i].input();
}
void manage_stu::delete_stu()//删除指定学号的学生信息
{
student r;
r.stu_delete(stu);
}
void manage_stu::find_stu()//查找指定学号的学生并显示
{
student t;
t.stu_find(stu);
}
void manage_stu::total_stu()//分别求每个人的总分
{
int i;
for(i=0;i<N+1;i++)
stu[i].pj();
}
void manage_stu::sort_stu()
{
int i,j;
student a;
for(i=0;i<N-1;i++)
{
for(j=i;j<N;j++)
{
if(stu[i].pj()<stu[j].pj())
{
a=stu[i];
stu[i]=stu[j];
stu[j]=a;
}
}
}
}
void manage_stu::display_stu()//显示N名学生信息
{
student t;
int i;
for(i=0;i<N;i++)
stu[i].dispaly();
}
void main()
{

int i=1,c;
manage_stu s;
while(i)
{
cout<<"学生管理系统:"<<endl;
cout<<"1、录入学生信息"<<N<<"名学生信息"<<endl;
cout<<"2、查找"<<endl;
cout<<"3、排序"<<endl;
cout<<"4、删除"<<endl;
cout<<"5、显示几名学生信息"<<endl;
cout<<"6、退出系统"<<endl;
cout<<"请选择(1--6):";
cin>>c;
switch(c)
{
case 1:s.input_stu();break;
case 2:s.find_stu();break;
case 3:s.sort_stu();break;
case 4:s.delete_stu();break;
case 5:s.display_stu();break;
case 6:i=0;
}
}
}本回答被网友采纳
第2个回答  2019-01-11

用我们的平台做可能简单的多,基于SaaS+后端云,直接拖拽设置就可以实现。但是如果你是要学校C++,还是自己练习吧。

第3个回答  2019-03-06
信息保存 : mysql (简单点可以用本地xml)
UI :MFC /QT
相似回答