#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 SingleOut(Student e)
{
cout<<"学号、姓名、性别、班级、科目、成绩: ";
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 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<<"======================================\n";
cout<<" \n";
cout<<" Please select 1、 2、 3、 4、 5、 6: ";
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:
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 6:
flag=false;
break;
default:
cout<<endl<<"!! 选择错误,请重新选择 !!"<<endl;
Pause();
}
}
return 0;
}