#include
#include
#include
#include
#include
#include
#define LEN sizeof(struct student)
#define T "\t\t\t\t"
#define X 16
typedef struct student
{
char name[20];
char num[3];
float score[3];
struct student *next;
}STU;
STU *head=NULL;
//函数声明
void establish(); //建立链表
void maintain(); //信息维护菜单
void add_ask(); //添加查询
void add_new_student(); //添加学生
void del(); //删除菜单
void del_ask(int i); //删除询问
void del_student(int i); //删除操作
void modify(); //信息修改菜单
void modify_ask(int i); //修改询问
void modify_student(int i); //修改学生信息
void inquiry(); //信息查询菜单
void inquiry_student(int i); //按姓名查询
void inquiry_ask(int i); //按学号查询
void scan_all(); //浏览全部学生信息
void score_inquiry(); //成绩统计
void mainmenu(); //主菜单
//文件操作
void file_creat(); //文件创建
void file_open(); //文件内容写入链表
void file_repeat(STU *head); //重新写入信息
void file_modify(STU *p,long i); //修改信息写入文件
void main()
{
system ("color 14");
system ("title copyright zwsatan");
mainmenu();
}
void mainmenu() //主菜单
{
file_open();
char i;
do{
cout << "\n\n\n\n\n" << T << "--------------------" << endl;
cout << "\n" << T << "----成绩管理系统----" << endl;
cout << "\n" << T << "--------------------" << endl;
cout << "\n\n\n" << T << "1---------学生信息建立" << endl;
cout << "\n" << T << "2---------学生信息维护" << endl;
cout << "\n" << T << "3---------学生信息查询" << endl;
cout << "\n" << T << "4---------学生成绩查询" << endl;
cout << "\n" << T << "5---------退出程序" << endl;
cin >> i;
switch (i)
{
case '1':system("cls"); establish();
continue;
case '2':system("cls"); maintain();
continue;
case '3':system("cls"); inquiry();
continue;
case '4':system("cls"); score_inquiry();
continue;
case '5':
if (head!=NULL)
{
STU *pp=head,*tmp;
while (pp)
{
tmp=pp->next;
free(pp);
pp=tmp;
}
}
exit(0);
default :system("pause"); system("cls");
}
}while (1);
}
void establish() //构建链表
{
/*判断原先的链表是否为空,如果不是空,先清除链表,释放内存*/
if (head!=NULL)
{
STU *pp=head,*tmp;
while (pp)
{
tmp=pp->next;
free(pp);
pp=tmp;
}
}
STU *p1,*p2;
char name[20];
head=p2=(STU *)malloc(LEN);
cout << "\n\n" << "输入姓名(输入【q】键返回主菜单):";
cin >> name;
if (strcmp(name,"q")==0) //判断是否退出并构建结束
{
p2->next=NULL;
file_creat();
system("pause");
system("cls");
return;
}
strcpy (p2->name,name);
cout << "输入学号(“两位数”):";
cin >> p2->num;
cout << "输入语文成绩:";
cin >> p2->score[0];
cout << "输入数学成绩:";
cin >> p2->score[1];
cout << "输入英语成绩:";
cin >> p2->score[2];
while (1)
{
p1=(STU *)malloc(LEN);
cout << "\n" << "输入姓名(输入【q】键返回主菜单):";
cin >> name;
if (strcmp(name,"q")==0) //判断是否退出并构建结束
{
p2->next=NULL;
file_creat();
system("pause");
system("cls");
return;
}
strcpy (p1->name,name);
cout << "输入学号(“两位数”):";
cin >> p1->num;
cout << "输入语文成绩:";
cin >> p1->score[0];
cout << "输入数学成绩:";
cin >> p1->score[1];
cout << "输入英语成绩:";
cin >> p1->score[2];
p2->next=p1;
p2=p1;
}
}
void maintain() //维护菜单界面
{
char i;
do{
cout << "\n\n\n\n\n" << T << "--------------------" << endl;
cout << "\n" << T << "----学生信息维护----" << endl;
cout << "\n" << T << "--------------------" << endl;
cout << "\n\n\n" << T << "1-----------增加学生信息" << endl;
cout << "\n" << T << "2-----------删除学生信息" << endl;
cout << "\n" << T << "3-----------修改学生信息" << endl;
cout << "\n" << T << "4-----------返回主菜单" << endl;
cin >> i;
switch (i) //菜单内容选项
{
case '1':
system("cls"); add_ask();
continue;
case '2':
system("cls"); del();
continue;
case '3':
system("cls"); modify();
continue;
case '4':system("cls");
return;
default :system("pause"); system("cls");
}
}while (1);
}
void add_ask()
{
char flag;
add_new_student();
do
{
cout << "\n" << T << "是否继续添加新的学生信息(y/n):";
cin >> flag;
if (flag=='y'||flag=='Y') //重新调用继续查询
{
system("cls");
add_new_student();
}
else if (flag=='n'||flag=='N') //返回上级菜单
{
system("cls");
return;
}
else
cout << "\n" << T << "命令错误";
}while (1);
}
void add_new_student()
{
char name[20];
STU *temp,*p1=head,*p2=p1;
cout << "\n\n" << "输入要添加的学生姓名:";
cin >> name;
if (strcmp(name,"q")==0) //判断是否退出
{
system("cls");
return;
}
temp=(STU *)malloc(LEN);
strcpy (temp->name,name);
cout << "输入学号(“两位数”):";
cin >> temp->num;
cout << "输入语文成绩:";
cin >> temp->score[0];
cout << "输入数学成绩:";
cin >> temp->score[1];
cout << "输入英语成绩:";
cin >> temp->score[2];
if (head==NULL) //表头插入
{
head=temp;
temp->next=NULL;
}
else
{
while (p1->next!=NULL && strcmp(p1->num,temp->num)<0)
{
p2=p1;
p1=p1->next; //循环结束时p1指向插入位置
}
if (strcmp(p1->num,temp->num)==0)
{
char flag;
cout << "\n" << T << "该学号学生信息已存在,请问是否覆盖(y/n)" << endl;
cin >> flag;
if (flag=='y'||flag=='Y') //即删除p1结点,由新添结点取代
{
p2->next=temp;
temp->next=p1->next;
free(p1);
}
else
return; //返回上级菜单
}
else if (strcmp(p1->num,temp->num)>0)
{
if (head==p1)
head=temp; //结点插入到表头
else
p2->next=temp; //结点插入到p1前
temp->next=p1;
}
else
{
p1->next=temp; //结点插入到表尾
temp->next=NULL;
}
}
file_repeat(head);
}
void del() //删除菜单函数
{
char i;
do
{
cout << "\n\n\n" << T << "1:按学号删除 2:按姓名删除 0:上级主菜单" << endl;
cin >> i;
switch (i)
{
case '1':
case '2':system("cls"); del_ask(i-'1');
continue;
case '0':system("cls");
return;
default :system("pause"); system("cls");
}
}while (1);
}
void del_ask(int i)
{
char flag;
del_student(i);
file_repeat(head);
do
{
cout << "\n" << T << "是否继续删除其他学生信息(y/n):";
cin >> flag;
if (flag=='y'||flag=='Y') //调用删除函数
{
system("cls");
del_student(i);
(head);
}
else if (flag=='n'||flag=='N') //返回上级菜单
{
system("cls");
return;
}
else
cout << "\n" << T << "命令错误";
}while (1);
}
void del_student(int i)
{
char num[3],name[20],*tag1[2],tag2[2][10]={"学号","姓名"};
STU *p=head,*q=p;
tag1[0]=num;
tag1[1]=name;
cout << "\n\n" << T << "输入要删除学生的" << tag2[i] << ":";
cin >> tag1[i];
if (head==NULL)
cout << "\n\n\n" << T << "无此学生信息,删除失败" << endl;
else
{
while (p!=NULL) //进行查找并以尾指针作为找到结束的标志
if (i==0 && strcmp(p->num,num)!=0)
{
q=p;
p=p->next;
}
else if (i==1 && strcmp(p->name,name)!=0)
{
q=p;
p=p->next;
}
else
{
//进行删除
if(p==head)
{
head=p->next;
}
else
{
q->next=p->next;
}
free(p);
cout << "\n\n" << T << "删除成功" << endl;
return;
}
if (p==NULL)
cout << "\n\n\n" << T << "无此学生信息,删除失败" << endl;
}
}
void modify() //修改菜单函数
{
char i;
do
{
cout << "\n\n\n" << T << "1:按学号修改 2:按姓名修改 0:上级主菜单" << endl;
cin >> i;
switch (i)
{
case '1':
case '2':system("cls"); modify_ask(i-'1');
continue;
case '0':system("cls");
return;
default :system("pause"); system("cls");
}
}while (1);
}
void modify_ask(int i)
{
char flag;
modify_student(i);
do
{
cout << "\n" << T << "是否继续修改其他学生信息(y/n):";
cin >> flag;
if (flag=='y'||flag=='Y')
{
system("cls");
modify_student(i);
}
else if (flag=='n'||flag=='N') //返回上级菜单
{
system("cls");
return;
}
else
cout << "\n" << T << "命令错误";
}while (1);
}
void modify_student(int i)
{
char num[3],name[20],*tag1[2],tag2[2][10]={"学号","姓名"};
STU *p=head;
long pi=0L;
tag1[0]=num;
tag1[1]=name;
cout << "\n\n" << T << "输入要修改学生信息的" << tag2[i] << ":";
cin >> tag1[i];
if (head==NULL)
cout << "\n\n\n" << T << "无此学生信息,修改失败" << endl;
else
{
while (p!=NULL) //进行查找并以尾指针作为找到结束的标志
if (i==0 && strcmp(p->num,num)!=0)
{
p=p->next;
pi++;
}
else if (i==1 && strcmp(p->name,name)!=0)
{
p=p->next;
pi++;
}
else
{
cout << "\n\n" << T << "输入修改信息" << endl;
cout << "\n" << T << "姓名:"; cin >> p->name;
cout << "\n" << T << "学号:"; cin >>p->num;
cout << "\n" << T << "语文成绩:"; cin >> p->score[0];
cout << "\n" << T << "数学成绩:"; cin >> p->score[1];
cout << "\n" << T << "英语成绩:"; cin >> p->score[2];
break;
}
if (p==NULL)
cout << cout << "\n\n\n" << T << "无此学生信息,修改失败" << endl;
}
file_modify(p,pi);
}
void inquiry() //信息查询菜单
{
char i;
do{
cout << "\n\n\n\n\n" << T << "--------------------" << endl;
cout << "\n" << T << "----学生信息查询----" << endl;
cout << "\n" << T << "--------------------" << endl;
cout << "\n\n\n" << T << "1-----------按学号查询(单人信息查询)" << endl;
cout << "\n" << T << "2-----------按姓名查询(单人信息查询)" << endl;
cout << "\n" << T << "3-----------查询全部学生信息" << endl;
cout << "\n" << T << "0-----------返回主菜单" << endl;
cin >> i;
switch (i) //菜单选项
{
case '1':
case '2':system("cls"); inquiry_ask(i-'1');
continue;
case '3':system("cls"); scan_all(); system("pause"); system("cls");
continue;
case '0':system("cls");
return;
default :system("pause"); system("cls");
}
}while (1); //指令判断是否正确
}
void inquiry_ask(int i)
{
char flag; //flag用来判断是否继续查询
inquiry_student(i);
do
{
cout << "\n" << T << "是否继续查询其他学生信息(y/n):";
cin >> flag;
if (flag=='y'||flag=='Y')
{
system("cls");
inquiry_student(i);
}
else if (flag=='n'||flag=='N') //返回上级菜单
{
system("cls");
return;
}
else
cout << "\n" << T << "命令错误";
}while (1);
}
void inquiry_student(int i)
{
char num[3],name[20],*tag1[2],tag2[2][10]={"学号","姓名"};
STU *p=head;
tag1[0]=num;
tag1[1]=name;
cout << "\n\n" << "请输入要查询的" << tag2[i] << ":";
cin >> tag1[i];
if (head!=NULL) //判断链表是否为空
{
while (p!=NULL) //进行查找并以尾指针作为结束的标志
if (i==0 && strcmp(p->num,num)!=0)
p=p->next;
else if (i==1 && strcmp(p->name,name)!=0)
p=p->next;
else //表明找到并进行输出学生信息
{
cout << "\n\n" << T << "姓名:" << p->name << ",学号:" << p->num;
cout << "语文:" << p->score[0] << "数学:" << p->score[1] << "英语:" << p->score[2] << endl;
break;
}
if (p==NULL)
cout << T << "无此学生信息" << endl;
}
else
cout << T << "无此学生信息" << endl;
}
void score_inquiry() //成绩查询
{
int j,k,number=0; //number用来统计分数段人数
char i,q,flag,subject[3][5]={"语文","数学","外语"}; //q用来接收'-',flag用来接收任意键
STU *p=head;
do
{
cout << "\n\n\n" << T << "1:语文 2:数学 3:英语 0:返回主菜单" << endl;
cout << "\n" << T << "请选择科目:";
cin >> i;
switch (i)
{
case '1':
case '2':
case '3':
cout << "\n" << T << "请选择分数段(格式如70-80):";
cin >> j >> q >> k;
break;
case '0':system("cls");
return;
default :system("pause"); system("cls");
}
if (i=='1' || i=='2' || i=='3' || i=='0')
break;
}while (1);
for (int lp=1;p!=NULL;p=p->next) //查找并输出分数段内学生对应科目的成绩
if (p->score[i-'1']>=j && p->score[i-'1']<=k)
{
if (lp)
{
cout << "\n" << setw(X) << "姓名" << setw(X) << "学号" << setw(X) << subject[i-'1'] <
}
cout << setw(X) << p->name << setw(X) << p->num << setw(X) << p->score[i-'1'] <
}
cout << "\n" << T << "此分数段学生人数共计:" << number <
{
cout << "\n" << T << "按【q】键返回主菜单:";
cin >> flag;
if (flag=='q'||flag=='Q')
{
system("cls");
return;
}
else
cout << "\n" << T << "输入错误,请按照说明输入" << endl;
}while (flag!='q'||flag!='Q');
}
void scan_all()
{
STU *p=head;
int tag=1;
while (p)
{
if (tag)
{
cout << "\n" << setw(X) << "姓名" << setw(X) << "学号" << setw(X) << "语文" << setw(X) << "数学";
cout <
}
cout << setw(X) << p->name << setw(X) << p->num << setw(X) << p->score[0] << setw(X) << p->score[1];
cout << setw(X) << p->score[2] << endl;
p=p->next;
}
}
void file_creat() //文件创建
{
FILE *fp;
STU *phead=head;
if ((fp=fopen("score of student.dat","wb"))==NULL)
{
cout << "\n\n" << T << "文件建立失败,敲击任意键以退出" << endl;
getchar();
exit(0);
}
while (phead)
{
fwrite(phead,LEN,1,fp);
phead=phead->next;
}
if (fclose(fp))
cout << "\n\n" << T << "保存失败" << endl;
else
cout << "\n\n" << T << "保存成功" << endl;
}
void file_open() //将文件内容写入链表
{
FILE *fp;
STU *p1,*p2,tmp;
if ((fp=fopen("score of student.dat","rb"))==NULL)
{
cout << "\n\n" << T << "尚未建立学生信息,请先建立学生信息" << endl;
return;
}
head=p2=(STU *)malloc(LEN);
fread(p2,LEN,1,fp);
p2->next=NULL;
while (fread(&tmp,LEN,1,fp))
{
p1=(STU *)malloc(LEN);
memcpy(p1,&tmp,LEN);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
fclose(fp);
}
void file_repeat(STU *head) //重新写入学生信息
{
FILE *fp;
STU *phead=head;
if ((fp=fopen("score of student.dat","w"))==NULL)
{
cout << "\n\n" << T << "尚未建立学生信息,请先建立学生信息" << endl;
return;
}
while (phead!=NULL)
{
fwrite(phead,LEN,1,fp);
phead=phead->next;
}
fclose(fp);
}
void file_modify(STU *p,long i) //将修改信息添加入文件
{
FILE *fp;
if ((fp=fopen("score of student.dat","r+"))==NULL)
{
cout << "\n\n" << T << "尚未建立学生信息,请先建立学生信息" << endl;
return;
}
fseek(fp,i*LEN,SEEK_SET);
fwrite(p,LEN,1,fp);
fclose(fp);
}
我有c#的跟java的= =
这个我做过!
QQ :1258213365
h