学生成绩管理系统(数据结构之线性表实现)

这里写图片描述

AddrList.h

#include <iostream>
#include <iomanip>
#include <fstream>
#include <process.h>
#include <string.h>
using namespace std;

const int LIST_INIT_SIZE=10;
const int LISTINCREMENT=5;

//student,
typedef struct Contacts
{
    char id[10]; //学号
    char name[10];  //姓名
    char sex[10];  //性别
    char class_number[10];  //班级
    char class_name[30];  //科目
    char score[30];  //成绩
}Student;

//scorebook表
typedef struct scorebook
{
    Student *elem;
    int length;
    int listsize;
    int incrementsize;
}scorebook;

//错误运行
void ErrorMessage(char *s)
{
    cout<<s<<endl;
    exit(1); //非正常运行导致退出程序
}

//getchar()吸收换行来模拟暂停
void Pause()
{
    cout<<endl<<"按任意键继续......";
    getchar();getchar();
}

//初始化 length=0,获取 maxsize, incresize,new 长度为maxsize的student数组
void InitList_Sq(scorebook &L, int maxsize=LIST_INIT_SIZE, int incresize=LISTINCREMENT)
{
    L.elem=new Student [maxsize];
    L.length=0;
    L.listsize=maxsize;
    L.incrementsize=incresize;
}

//查找元素(按id学号查询)
int LocateElem_Sq(scorebook &L, Student e)
{
    for(int i=0;i<L.length;i++)
        if(strcmp(L.elem[i].id,e.id)==0) return i+1;
    return 0;
}

//获取元素
void GetElem_Sq(scorebook &L, int i, Student &e)
{
    e=L.elem[i-1];
}

//增加存储空间和长度
void increment(scorebook &L)
{
    Student  *p=new Student[L.listsize+L.incrementsize];
    for(int i=0;i<L.length;i++)
        p[i]=L.elem[i];
    delete [] L.elem;
    L.elem=p;
    L.listsize=L.listsize+L.incrementsize;
}

//增加元素
void ListInsert_Sq(scorebook &L, Student e)
{
    if(L.length>=L.listsize)
        increment(L);
    if(L.length ==0)
    {
        L.elem[0]=e;
        L.length++;
    }
    else
    {
        int i=L.length-1;
        while((strcmp(L.elem[i].id,e.id)>0)&&i>=0)
        {
            L.elem[i+1]=L.elem[i];
            i--;
        }
        L.elem[i+1]=e;
        ++L.length;
    }
 }

void ListInsert_Sq(scorebook &L, int i, Student e )
{
    if (i<1||i>L.length+1)   ErrorMessage(" i 值不合法");
    if (L.length>=L.listsize)  increment(L);
    Student *q = &(L.elem[i-1]);
    for (Student *p=&(L.elem[L.length-1]); p>=q;--p)
        *(p+1) = *p;
    *q = e;
    ++L.length;
}

//删除信息
void ListDelete_Sq(scorebook &L, int i,Student e)
{

    if ((i<1) || (i>L.length))  ErrorMessage("i值不合法");
    Student *p=&(L.elem[i-1]);
    e=*p;
    Student *q=L.elem+L.length-1;
    for(++p; p<=q;  ++p )
        *(p-1)=*p;
    --L.length;
}

//销毁表
void DestroyList_Sq(scorebook &L)
{
    delete [] L.elem;
    L.listsize=0;
    L.length=0;
}

//清空表
void  ClearList_Sq(scorebook &L)
{
    L.length=0;
}

bool ListEmpty_Sq(scorebook &L)
{
    if(L.length==0)
        return true;
    else return false;
}

//输出信息
void ListTraverse_Sq(scorebook &L)
{
    cout<<"\n========================  学生成绩信息表 ========================"<<endl<<endl;
   cout<<"学号、姓名、性别、班级、科目、成绩:\n";
    for(int i=0;i<L.length;i++)
        cout<<setiosflags(ios_base::left)<<setw(12)<<L.elem[i].id
            <<setw(12)<<L.elem[i].name<<setw(15)<<L.elem[i].sex<<setw(10)<<L.elem[i].class_number
            <<setw(12)<<L.elem[i].class_name<<setw(16)<<L.elem[i].score<<resetiosflags(ios_base::left)<<endl;
    Pause();
}

//输入
void input(char *tid,scorebook &L)
{
    int n;
    Student e;
    ofstream wfile;
    wfile.open(tid,ios_base::binary);
    cout<<"请输入信息表中的联系人人数:";
    cin>>n;
    cout<<"请输入学生的学号、姓名、性别、班级、科目、成绩:\n";
    for(int i=1 ;i<=n;i++)
    {
        cin>>e.id>>e.name>>e.sex>>e.class_number>>e.class_name>>e.score;
        ListInsert_Sq(L,e);
        wfile.write((char *)(&e),sizeof(Student));
    }
    wfile.close();
}

void load(char *tid,scorebook &L)
{
    Student e;
    ifstream rfile;
    rfile.open(tid,ios_base::binary);
    while(rfile.read((char *)(&e),sizeof(e)))
    {
        ListInsert_Sq(L,e);
    }
    rfile.close();
}


void save(char *tid,scorebook &L)
{
    ofstream save;
    save.open(tid,ios_base::binary);
    for(int i=0;i<L.length;i++)
        save.write((char *)(&L.elem[i]),sizeof(Student));
    save.close();
}

//排序(按照学号)
void Sort_id(scorebook &L)
{
    int i,j,k;
    Student e;
    for(i=0;i<L.length-1;i++)
    {
        k=i;
        for(j=i+1;j<L.length;j++)
            if(strcmp(L.elem[j].id,L.elem[k].id)<0) k=j;
        if(i!=k)
        {
            e=L.elem[i];
            L.elem[i]=L.elem[k];
            L.elem[k]=e;
        }
    }
}

//排序(按照成绩)
void Sort_class_number(scorebook &L)
{
    int i,j,k;
    Student e;
    for(i=0;i<L.length-1;i++)
    {
        k=i;
        for(j=i+1;j<L.length;j++)
            if(strcmp(L.elem[j].score,L.elem[k].score)<0)  k=j;
        if(i!=k)
        {
            e=L.elem[i];
            L.elem[i]=L.elem[k];
            L.elem[k]=e;
        }
    }
}

//单个输出信息
void SingleOut(Student e)
{
    cout<<"学号、姓名、性别、班级、科目、成绩:\n";
    cout<<setiosflags(ios_base::left)<<setw(12)<<e.id
        <<setw(12)<<e.name<<setw(15)<<e.sex<<setw(10)<<e.class_number
        <<setw(12)<<e.class_name<<setw(16)<<e.score<<resetiosflags(ios_base::left)<<endl;
}


int Reademe()
{
    cout<<"实现:数据结构之线性表"<<endl;
    cout<<endl;
    cout<<"内容:成绩管理系统"<<endl;
    cout<<endl;
    cout<<"时间:2016-11-08 "<<endl;
    cout<<endl;
    cout<<"HPioneer一直在追逐"<<endl;
}

AddrList.cpp

#include "AddrList.h"

int main()
{
    Student e;
    int i,n;
    bool flag=true;
    char yn;
    char addlist[30];
    scorebook L;
    InitList_Sq(L,LIST_INIT_SIZE,LISTINCREMENT);
    cout<<"请输入成绩表的存盘文件名:";
    cin>>addlist;
    fstream file;
    file.open(addlist,ios::in);
    if(!file)
    {
        cout<<"成绩表不存在,建立一个新的成绩表(Y/N)?";
        cin>>yn;
        if(yn=='Y'||yn=='y')
        {   file.close();
            input(addlist,L);
        }
    }
    else
    {
        file.close();
        load(addlist,L);
        ListTraverse_Sq(L);
    }

    while(flag)
    {
        system("cls");
        cout<<"\n         学生成绩管理系统主菜单   \n";
        cout<<"======================================\n";
        cout<<"          1 添加学生 \n"<<endl;
        cout<<"          2 删除学生 \n"<<endl;;
        cout<<"          3 按学号查找学生\n"<<endl;
        cout<<"          4 输出成绩表\n"<<endl;
        cout<<"          5 保存\n"<<endl;
        cout<<"          6 修改学生信息\n"<<endl;
        cout<<"          7 按学号排序\n"<<endl;
        cout<<"          8 按成绩排序(从小到大)\n"<<endl;
        cout<<"          9 关于作者\n"<<endl;
        cout<<"          0 退出\n"<<endl;

        cout<<"======================================\n";
        cout<<"                              \n";
        cout<<"  Please  select 1、 2、 3、 4、 5、 6、 7、 8、9、0:  ";
        cin>>n;
        switch (n)
        {
          case 1:
            cout<<"请输入该学生新的学号、姓名、性别、班级、科目、成绩:\n";
            cin>>e.id>>e.name>>e.sex>>e.class_number>>e.class_name>>e.score;
            ListInsert_Sq(L,e);
            break;
          case 2 :
            cout<<"请输入要删除的学生的学号:";
            cin>>e.id;
            char yn;
            i=LocateElem_Sq(L,e);
            if(i==0)
            {
                cout<<"学号为:"<<e.id<<" 的学生不存在,不能被删除!!\n";
                break ;
            }
            cout<<"你确实要删除么?";
            cin>>yn;
            if(yn=='Y'||yn=='y')
                ListDelete_Sq(L,i,e);
            else
            { cout <<"!!不删除!!"<<endl;}
            break;
          case 3:
            { cout<<"请输入要查找的学生学号:";
              cin>>e.id;
              i=LocateElem_Sq(L,e);
              if(i!=0)
              {     GetElem_Sq(L,i,e);
                    SingleOut(e);
              }
              else cout<<endl<<"!!学号为:"<<e.id<<" 的学生不存在!!\n";
            }
            Pause();
            break;
          case 4:
            ListTraverse_Sq(L);
            break;
          case 5:
            {
                save(addlist,L);
                break;
            }
          case 6:
              cout<<"请输入要修改的学生的学号:";
              cin>>e.id;
              i=LocateElem_Sq(L, e);
              if(i!=0)
              {     GetElem_Sq(L,i,e);
                    SingleOut(e);
                    cout<<"确实要修改么Y/N?";
                    cin>>yn;
                    if(yn=='y'||yn=='Y')
                    {
                        ListDelete_Sq(L,i,e);
                        cout<<"请输入该学生新的学号、姓名、性别、班级、科目、成绩:\n";
                        cin>>e.id>>e.name>>e.sex>>e.class_number>>e.class_name>>e.score;
                        ListInsert_Sq(L,e);
                    }
              }
              else cout<<endl<<"!!学号为 "<<e.id<<" 的学生不存在!!\n";
              Pause();
              break;
          case 7:
              Sort_id(L);
              ListTraverse_Sq(L);
              break;
          case 8:
              Sort_class_number(L);
              ListTraverse_Sq(L);
              break;
          case 9:
              Reademe();
              Pause();
              break;
          case 0:
              flag=false;
              break;
          default:
              cout<<endl<<"!! 选择错误,请重新选择 !!"<<endl;
              Pause();
      }
    }
    return 0;
}


因为没看课程要求就自己发挥了,在这里附上作业要求与按要求的写的代码:

题目:学生成绩管理系统

要求:
1. 基于顺序表或单链表实现学生成绩管理系统
2. 学生信息定义可参照如下结构体设计
typedef struct
{ long num;// 编号
char name[20]; //姓名
char sex; //性别
float english; //英语
float math; //高数
float computer; //计算机
float score; //总成绩
} ElemType;
3.设计头文件StuList.h,包含数据类型定义和成绩表的基本操作
4.设计程序文件StuList.cpp,以菜单形式实现系统控制
5.系统实现的功能:成绩的录入、查询、删除、修改、输出、保存、排序(以姓名、总成绩等作为关键字)、统计(如单科分数段统计)等功能。

(源代码+实验报告+作业要求)
链接:http://pan.baidu.com/s/1qYMt97m 密码:h7pv

posted @ 2016-11-08 21:47  H+  阅读(1841)  评论(0)    收藏  举报