#include <stdio.h>
#include<iostream>
#include<iomanip>
#include <stdlib.h>
#include <fstream>
using namespace std;
const int defaultSize=100;
typedef struct student
{
char num[2];
char name[3];
char sex[1];
char birthday[6];
char health[4];
} stutype;
template <class Type> class SeqList {
Type *data; //顺序表存储数组
int MaxSize; //最大允许长度
int last; //当前最后元素下标
public:
SeqList ( int MaxSize = defaultSize );
~SeqList ( ) { delete [ ] data; }
int Find ( Type & x );
bool Insert(int i,Type & x );
bool Remove(int i,Type & x);
int Input();
int output();
int Delete();
int Search();
int count;
int intext();
int outtext();
};
template <class Type>
SeqList <Type> :: SeqList ( int sz ) {
if ( sz > 0 ) {
MaxSize = sz; last = -1;
data = new Type[MaxSize];
if ( data == NULL) {
MaxSize = 0; last = -1;
return;
}
count=0;
}
};
template <class Type>
int SeqList <Type> :: Find ( Type & x ) {
//搜索函数:在顺序表中从头查找结点值等于
//给定值x的结点
int i = 0;
while ( i<=count&&strcmp(data[i].num ,x.num)!=0)
i++;
if(count<i) cout<<"用户不存在!"<<endl;
return i;
};
template <class Type>
bool SeqList <Type> :: Insert(int i,Type & x ) {
//插入函数
if ( last==MaxSize-1 ) return false;
if (i<0||i>last+1) return false;
for(int j=last;j>=i;j--)
data[j+1]=data[j];
data[i]=x;
last++;
return true;
};
template <class Type>
bool SeqList <Type> :: Remove(int i, Type & x ) {
//删除函数
if ( last==-1 ) return false;
if (i<1||i>last+1) return false;
for(int j=i;j<=last;j++)
data[j-1]=data[j];
last--;
count--;
return true;
};
template <class Type>
int SeqList <Type> ::Delete()//删除函数
{
int c;
count--;
cout<<"输入学号:"<<endl;
cin>>data[20].num;
c=Find(data[20]);
Remove( c,data[20]);
cout<<"删除成功!请执行下一步(⊙o⊙)"<<endl;
return 0;
}
template <class Type>
int SeqList <Type> ::Search()//查询函数
{
int i;
cout<<"输入学号:"<<endl;
cin>>data[20].num;
i=Find(data[20]);
if(count>i)
{
cout<<" ------------------------------------------------------------------------------ "<<endl;
cout<<"|"<<setw(8)<<"学号"<<setw(8)<<"|"<<setw(8)<<"姓名"<<setw(8)<<"|"<<setw(8)<<"性别"<<setw(8)<<"|"<<setw(8)<<"出生年月"<<setw(8)<<"|"<<setw(8)<<"健康情况"<<setw(8)<<"|"<<setw(8)<<endl;
cout<<" ------------------------------------------------------------------------------- "<<endl;
cout<<"|"<<setw(8)<<data[i].num<<setw(8)<<"|"<<setw(8)<<data[i].name<<setw(8)<<"|"<<setw(8)<<data[i].sex<<setw(8)<<"|"<<setw(8)<<data[i].birthday<<setw(8)<<"|"<<setw(8)<<data[i].health<<setw(8)<<"|"<<setw(8)<<endl;
cout<<" ------------------------------------------------------------------------------- "<<endl;
}
cout<<"请执行下一步(⊙o⊙)"<<endl;
return 0;
}
template <class Type>
int SeqList <Type> ::Input()//输入学生信息
{
cout<<"输入学号:";
cin>>data[count].num;
cout<<"输入名字:";
cin>>data[count].name;
cout<<"输入性别(男或女):";
cin>>data[count].sex;
cout<<"输入出生年月:";
cin>>data[count].birthday;
cout<<"输入健康状况(good or bad):";
cin>>data[count].health;
count++;
cout<<"成功输入!请执行下一步(⊙o⊙)"<<endl;
return 0;
}
template <class Type>
int SeqList <Type> ::output()//输出学生信息到屏幕
{
cout<<" ------------------------------------------------------------------------------ "<<endl;
cout<<"|"<<setw(7)<<"学号"<<setw(7)<<"|"<<setw(7)<<"姓名"<<setw(7)<<"|"<<setw(7)<<"性别"<<setw(7)<<"|"<<setw(7)<<"出生年月"<<setw(7)<<"|"<<setw(7)<<"健康情况"<<setw(7)<<"|"<<setw(7)<<endl;
cout<<" ------------------------------------------------------------------------------- "<<endl;
for(int i=0;i<count;i++)
{
cout<<"|"<<setw(7)<<data[i].num<<setw(7)<<"|"<<setw(7)<<data[i].name<<setw(7)<<"|"<<setw(7)<<data[i].sex<<setw(7)<<"|"<<setw(7)<<data[i].birthday<<setw(7)<<"|"<<setw(7)<<data[i].health<<setw(7)<<"|"<<setw(7)<<endl;
cout<<" ------------------------------------------------------------------------------- "<<endl;
}
cout<<"请执行下一步(⊙o⊙)"<<endl;
return 0;
}
template <class Type>
int SeqList <Type> ::outtext()//把文本txt的信息输出
{
ifstream infile( "C:\ooread.txt", ios::in);
if(!infile)
{ofstream infile1( "C:\ooread.txt", ios::in);
for(int i=0;i<count;i++)
infile>>data[i].num>>data[i].name>>data[i].sex>>data[i].birthday>>data[i].health;
cout<<"写入成功!";
}
infile.close( );
cout<<"请执行下一步(⊙o⊙)"<<endl;
return 0;
}
template <class Type>
int SeqList <Type> ::intext()//把信息保存到文本中
{
ofstream outfile( "C:\ooread.txt", ios::app );
for(int i=0;i<count;i++)
outfile<<data[i].num<<data[i].name<<data[i].sex<<data[i].birthday<<data[i].health;
cout<<"写入成功!";
outfile.close( );
cout<<"请执行下一步(⊙o⊙)"<<endl;
return 0;
}
void print()//主界面
{
cout<<"-----学生管理系统---输入序号:"<<endl;
cout<<"--1、新建学生列表"<<endl;
cout<<"--2、向学生列表输入信息"<<endl;
cout<<"--3、在列表删除信息"<<endl;
cout<<"--4、将文件的信息读取"<<endl;
cout<<"--5、保存信息到文件中"<<endl;
cout<<"--6、输入学生学号进行查询"<<endl;
cout<<"--7、在屏幕上输出学生信息"<<endl;
cout<<"--8、清屏"<<endl;
cout<<"--9、退出"<<endl;
};
int main()//主函数
{
print();
int i,j=1;
SeqList <stutype> stu1(22);
while(cin>>i&&i!=9)
{
if(i<0||i>9)
cout<<"超出范围哦!请重新输入!";
switch(i)
{
case 1:cout<<"列表新建成功哇!"<<endl;break;
case 2:stu1.Input();break;
case 3:stu1.Delete();break;
case 4:stu1.intext();break;
case 5:stu1.outtext();break;
case 6:stu1.Search();break;
case 7:stu1.output();break;
case 8:system("cls");print();break;
case 9:exit(1);break;
};
}
return 0;}