C++使用链表编写的班级信息管理系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//链表方式实现班级基础信息管理
//QIUJUER开发
//转载请注明出处
#include
#include
using namespace std;
#include
#define NULL 0
#define N 20

//说明文档信息
void S_P()
{
cout<<"---------------学生基础信息管理系统---------------"<cout<<"版本:Student_For_MNG_Bite_1.0"<cout<<"说明:\n1.初始化/插入数据!\n2.输出程序缓存数据!\n3.查找程序缓存数据(修改、删除)!\n";
cout<<"4.数据按学号从小到大排序!\n5.数据按学号从大到小排序!\n6.读取“data”文件数据到程序!\n";
cout<<"7.输出“data”文件(覆盖模式)!\n8.清空(重置)数据缓存!\n9.说明文档信息!\n10退出程序!"<cout<<"\n\n\n"<}
//设计链表结构
struct student
{
char num[16];
char name[8];
char tell[11];
double score;
struct student *up;
struct student *next;
}*head,*over;

//数值字符转换为数值类型
double strint(char *str)
{
double NUM=0;
double L,J=0;
L=strlen(str);
for (int i = 0; i < L; i++)
{
if (str[i]=='.')
break;
J++; //取得小数点前的位数
}
for (int i=0; i{
if (str[i]=='.')
continue;
else
{
double x;
J--;
x=str[i]; //获取数字的ASSSC值!
x=x-48; //转换为其数值!
NUM=NUM+x*pow(10.00,J); //pow(x,y);x的y次方
}
}
return(NUM);
}
//初始化
student *S_default()
{
struct student *L;
L=new student;
L->next=NULL;
L->up=NULL;
return L;
}
//链表创建,输入,插入
void S_cin()
{
cout<<"TIPS:请按提示输入,退出输入请在学号处输入:end"<student *p;
char S_if[16];
while (true)
{
cout<<"请输入学生的学号:";
cin>>S_if;
if (strcmp(S_if,"end"))
{
p=S_default(); //初始化
strcpy(p->num,S_if);
cout<<"请输入姓名:";
cin>>p->name;
cout<<"请输入电话:";
cin>>p->tell;
cout<<"请输入成绩:";
cin>>p->score;
if(head==NULL)
{
head=over=p;
}
else
{
over->next=p;
p->up=over;
over=p;
}

}
else
break;
}
cout<<"OK(提示:您已退出输入!)\n"<}
//输出链表的所有数据
void S_cout()
{
if (head==NULL)
{
cout<<"ERROR:NO HAVE DATA!"<}
else
{
student *p;
p=head;
while (p!=NULL)
{
cout<<"学号:"<num<<"\t姓名:"<name<<"\t电话:"<tell<<"\t成绩:"<score<p=p->next;
}
}
}
//删除数据
student S_delete(struct student *p)
{
struct student *rp,*tp;
cout<<"学号:"<num<<"\t姓名:"<name<<"\t电话:"<tell<<"\t成绩:"<score<if (p==head)
{
//--------------------------------头部时:类转值------------------
head=rp=p->next;
head->up=NULL;

}
else
{
if (p==over)
{
//-----

---------------------------结尾时:类转值------------------
over=rp=p->up;
over->next=NULL;
}
else
{
//--------------------------------中间时:类转值------------------
rp=p->up;
rp->next=p->next;
tp=p->next;
tp->up=rp;
}
}
delete p;
cout<<"OK:HAVE ONE DATA DELETED! (成功删除一条记录!)\n"<return *rp;
}
//修改数据
void S_update(struct student *p)
{
cout<<"TIPS:请按提示输入修改数据:"<cout<<"请输入学生的学号:";
cin>>p->num;
cout<<"请输入姓名:";
cin>>p->name;
cout<<"请输入电话:";
cin>>p->tell;
cout<<"请输入成绩:";
cin>>p->score;
cout<<"UPDATE:学号:"<num<<"\t姓名:"<name<<"\t电话:"<tell<<"\t成绩:"<score<cout<<"OK:HAVE ONE DATA UPDATE! (成功修改一条记录!)\n"<}
//查询数据(查询上下,修改数据,删除数据)
void S_select()
{
char str[16];
int X=0;
cout<<"请输入查找的学号:";
cin>>str;
struct student *p;
p=head;
while (p!=NULL)
{
if(!(strcmp(p->num,str)))
{
X++;
cout<<"第"<cout<<"学号:"<num<<"\t姓名:"<name<<"\t电话:"<tell<<"\t成绩:"<score<int W=1;
struct student *lp;
lp=p;
while (W)
{
cout<<"ASK:\n1.查找上一条数据。\n2.查找下一条数据。\n3.修改本条数据。\n4.删除本条数据。\n5.退出次选择,继续执行程序。"<int cheng;
cout<<"\n请输入您的查询选择:";
cin>>cheng;
switch (cheng)
{
case 1:
if (lp->up==NULL)
cout<<"ERROR:抱歉前面已经没有数据了!"<else
{
lp=lp->up;
cout<<"UP:学号:"<num<<"\t姓名:"<name<<"\t电话:"<tell<<"\t成绩:"<score<}
break;
case 2:
if (lp->next==NULL)
cout<<"ERROR:抱歉后面已经没有数据了!"<else
{
lp=lp->next;
cout<<"DOWN:学号:"<num<<"\t姓名:"<name<<"\t电话:"<tell<<"\t成绩:"<score<}
break;
case 3:
char b;
cout<<"ASK:是否修改数据(Y/N):";
cin>>b;
if (b=='y'||b=='Y')
S_update(lp);
break;
case 4:
//-----------------------------------删除-------------
char a;
cout<<"ASK:是否删除数据(Y/N):";
cin>>a;
if (a=='y'||a=='Y')
*p=*lp=S_delete(lp);
//-----------------------------------删除-------------
break;
case 5:W=0;break;
default:cout<<"ERROR(没有此选项,刷新!)"<break;
}
}
cout<<"OK:已经退出本次选择循环。"<}
p=p->next;
}
if (X==0)
{
cout<<"ERROR:NO HAVE DATA!"<}
else
cout<<"OK:查找结束,返回主选择菜单。"<}
//交换两个链表中的值
void S_change(struct student *a,struct student *b)
{
struct student *p

;
p=S_default();
//a->p
strcpy(p->num,a->num);
strcpy(p->name,a->name);
strcpy(p->tell,a->tell);
p->score=a->score;
//b->a
strcpy(a->num,b->num);
strcpy(a->name,b->name);
strcpy(a->tell,b->tell);
a->score=b->score;
//p->b
strcpy(b->num,p->num);
strcpy(b->name,p->name);
strcpy(b->tell,p->tell);
b->score=p->score;
}
//排序
void S_sort()
{
int n=0;
struct student *p1,*p2;
p1=head;
while (p1!=over)
{
n++;
p1=p1->next;
}
for (int i = n; i>0; i--)
{
p1=head;
if (head!=over)
p2=head->next;
for (int j = 0; j < i; j++)
{
if ((strcmp(p1->num,p2->num))>0)
S_change(p1,p2);
p1=p1->next;
if (p2!=over)
p2=p2->next;
}
}
cout<<"OK(按学号(字符)从小到大排序完成。)\n"<}
//排序
void S_sort1()
{
int n=0;
struct student *p1,*p2;
p1=head;
while (p1!=over)
{
n++;
p1=p1->next;
}
for (int i = n; i>0; i--)
{
p1=head;
if (head!=over)
p2=head->next;
for (int j = 0; j < i; j++)
{
if ((strcmp(p1->num,p2->num))<0)
S_change(p1,p2);
p1=p1->next;
if (p2!=over)
p2=p2->next;
}
}
cout<<"OK(按学号(字符)从大到小排序完成。)\n"<}

//读取文件数据
void S_filein()
{
fstream infile("data.dat",ios::in);
if (!infile)
{
cerr<<"ERROR:NO HAVE DATA FILE!(打开文件错误!)"<infile.close();
}
else
{
if (infile.eof())
{
cout<<"ERROR:NO HAVE DATA!"<}
else
{
int X=0;
student *p;
char *str;
char *str1="BEGIN:DATA";
char *str2="END:DATA";
str=new char[N];
while (!infile.eof())
{
infile.getline(str,N);
if (!strcmp(str,str1))
{
p=S_default();
X=1;
cout<}
else
{
if (!strcmp(str,str2))
{
if(head==NULL)
{
head=over=p;
}
else
{
over->next=p;
p->up=over;
over=p;
}
//X=0;
cout<}
else
{
switch (X)
{
case 1:strcpy(p->num,str);X++;;break;
case 2:strcpy(p->name,str);X++;break;
case 3:strcpy(p->tell,str);X++;break;
case 4:p->score=strint(str);X++;break;
case 5:cout<<"OK:Read data is successfully!"<default:cout<<"ERROR:NO HAVE DATA!"<break;
}
}
}
}
}
infile.close();
}
}
//输出到文件。
void S_fileout()
{
fstream outfile("data.dat",ios::out);
if(!outfile)
{
cerr<<"ERROR:FAILED TO CREATE FILE!(创建文件失败!)"<outfile.close();
}
else
{
if (head==NULL)
{
cout<<"ERROR:NO HAVE DATA!"<}
else
{
char *str1="BEGIN:DATA";
char *str2="END:DATA";
student *p;
p=head;
while (p!=NULL)
{
outfile<outfile<num<outfile<name<outfile<tell<outfile<

score<outfile<p=p->next;
}
cout<<"OK!(输出完毕!)"<}
outfile.close();
}
}
//清除(重置)数据!
void S_clean()
{
if (head!=NULL)
{
student *p;
do
{
p=head;
head=head->next;
delete p;
} while (head==NULL);
head=over=NULL;
cout<<"OK(清空(重置)数据完成!)"<}
else
{
cout<<"ERROR:NO HAVE DATA!"<}
}

//主程序,main
void main()
{
head=over=NULL;
S_P();
int x=1;
int i;
while (x!=0)
{
cout<<"\n------------请输入您的选择:";
cin>>i;
switch(i)
{
case 1:S_cin();break;
case 2:S_cout();break;
case 3:S_select();break;
case 4:S_sort();break;
case 5:S_sort1();break;
case 6:S_filein();break;
case 7:S_fileout();break;
case 8:S_clean();break;
case 9:S_P();break;
case 10:x=0;break;
default:cout<<"ERROR(没有此选项,刷新!)"<break;
}
}
}

相关文档
最新文档