关闭

----------学生信息管理系统-------

149人阅读 评论(0) 收藏 举报
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct student_message
{
    int student_num;
    char student_name[15];
    char student_sex[15];
    double sub_a,sub_b,sub_c,sub_d,sub_e;
    double sub_ave;
    double sub_sum;
    int mingci;
} data[100];
int ji=0,n,x,tmp=1;
void del(int m)
{
    for(int i=1; i<=n+ji-1; i++)
    {
        if(data[i].student_num==m)
        {
            for(int j=i; j<=n+ji-1; j++)
            {
                data[j]=data[j+1];
            }
            ji--;
            break;
        }
    }
    if(data[n+ji].student_num==m)
        ji--;
}
void xiugai(int m)
{
    for(int i=1; i<=n+ji; i++)
    {
        if(data[i].student_num==m)
        {
            char name[15],sex[15];
            double a,b,c,d,e;
            cout<<"名字"<<endl;
            cin>>name;
            strcpy(data[i].student_name,name);
            cout<<"性别"<<endl;
            cin>>sex;
            strcpy(data[i].student_sex,sex);
            cout<<"成绩"<<endl;
            cin>>a>>b>>c>>d>>e;
            data[i].sub_a=a;
            data[i].sub_b=b;
            data[i].sub_c=c;
            data[i].sub_d=d;
            data[i].sub_e=e;
            data[i].sub_sum=data[i].sub_a+data[i].sub_b+data[i].sub_c+data[i].sub_d+data[i].sub_e;
            data[i].sub_ave=data[i].sub_sum/5;
            break;
        }
    }
}
void showall()
{
    for(int i=1; i<=ji+n; i++)
    {
        cout<<data[i].student_num<<" "<<data[i].student_name<<" "<<data[i].student_sex<<" ";
        cout<<data[i].sub_a<<" "<<data[i].sub_b<<" "<<data[i].sub_c<<" "<<data[i].sub_d<<" "<<data[i].sub_e<<" ";
        cout<<data[i].sub_ave<<" "<<data[i].sub_sum<<" "<<data[i].mingci<<endl;
    }
}
void show(int m)
{
    for(int i=1; i<=n+ji; i++)
    {
        if(data[i].student_num==m)
        {
            cout<<data[i].student_num<<" "<<data[i].student_name<<" "<<data[i].student_sex<<" ";
            cout<<data[i].sub_a<<" "<<data[i].sub_b<<" "<<data[i].sub_c<<" "<<data[i].sub_d<<" "<<data[i].sub_e<<" ";
            cout<<data[i].sub_ave<<" "<<data[i].sub_sum<<" "<<data[i].mingci<<endl;
            break;
        }
    }
}
int cmp(const student_message &a,const student_message &b)
{
    return a.sub_ave>b.sub_ave;
}
void paimingci()
{
    for(int i=1; i<=n+ji; i++)
    {
        //cout<<"Aaaaaaaaaaaaaa"<<endl;
        data[i].mingci=i;
    }
}
void save(char *filename)
{
    FILE *fp=fopen(filename,"at");
    for(int i=tmp; i<=ji+n; i++)
    {
        fprintf(fp,"%d %s %s %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %.2lf %d\n",data[i].student_num,data[i].student_name,data[i].student_sex,data[i].sub_a,data[i].sub_b,data[i].sub_c,data[i].sub_d,data[i].sub_e,data[i].sub_ave,data[i].sub_sum,data[i].mingci);
    }
    fclose(fp);
    tmp=ji+n;
    return ;
}
void read(char *filename)
{
    char temp;
    FILE *fp;
    fp=fopen(filename,"r");
    while((temp=fgetc(fp))!=EOF)
    {
        cout<<temp;
    }
    fclose(fp);
    return;
}
void build()
{
    cout<<"请输入新建文件名称: ";
}
int main()
{
    cout<<"1、增加学生信息 "<<"  "<<"6、对学生信息进行排序 "<<endl;
    cout<<"2、修改学生信息 "<<"  "<<"7、保存学生信息至记录文件 "<<endl;
    cout<<"3、显示学生信息 "<<"  "<<"8、从记录文件读取学生信息 "<<endl;
    cout<<"4、查询学生信息 "<<"  "<<"9、新建学生信息文件 "<<endl;
    cout<<"5、删除学生信息 "<<"  "<<"0、运行结束 "<<endl;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>data[i].student_num>>data[i].student_name>>data[i].student_sex;
        cin>>data[i].sub_a>>data[i].sub_b>>data[i].sub_c>>data[i].sub_d>>data[i].sub_e;
        data[i].sub_sum=data[i].sub_a+data[i].sub_b+data[i].sub_c+data[i].sub_d+data[i].sub_e;
        data[i].sub_ave=data[i].sub_sum/5;
        paimingci();
    }
    int chs;
    cin>>chs;
    while(1)
    {
        switch(chs)
        {
        case 1:
            cout<<"输入学生信息"<<endl;
            ji++;
            cin>>data[n+ji].student_num>>data[ji+n].student_name>>data[ji+n].student_sex;
            cin>>data[ji+n].sub_a>>data[ji+n].sub_b>>data[ji+n].sub_c>>data[ji+n].sub_d>>data[ji+n].sub_e;
            data[ji+n].sub_sum=data[ji+n].sub_a+data[ji+n].sub_b+data[ji+n].sub_c+data[ji+n].sub_d+data[ji+n].sub_e;
            data[ji+n].sub_ave=data[ji+n].sub_sum/5;
            paimingci();
            break;
        case 2:
            cout<<"输入所要修改的学生学号"<<endl;
            cin>>x;
            xiugai(x);
            cout<<"修改完成"<<endl;
            break;
        case 3:
            showall();
            break;
        case 4:
            cout<<"输入所要显示的学生学号"<<endl;
            cin>>x;
            show(x);
            break;
        case 5:
            cout<<"输入所要删除的学生学号"<<endl;
            cin>>x;
            del(x);
            paimingci();
            break;
        case 6:
            sort(data+1,data+ji+n+1,cmp);
            paimingci();
            showall();
            break;
        case 7:
            save("学生信息管理系统.txt");
            break;
        case 8:
            read("学生信息管理系统.txt");
            break;
        case 9:
            build();
            break;
        case 0:
            return 0;
        default:
            cout<<"您输入的编号不在操作步骤里面,请重新选择谢谢"<<endl;
            break;
        }
        cin>>chs;
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12758次
    • 积分:1005
    • 等级:
    • 排名:千里之外
    • 原创:93篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条