/*
* xjgl.h
*
* Created on: 2014-12-5
* Author: yao
*/
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
//#include"stu.c"
//#include"tea.c"
//学生结构体
typedef struct student
{
int id;
struct stuname
{
int passwd;
char class[20];
char name[20];
char sex;
int age;
}stuna;
float math;
float c;
float yuwen;
float ave;
struct student *next;
}students;
typedef struct teacher{ //教师结构体
int tid;
int tpasswd;
char tsex;
int tage;
char tname[20];
struct teacher *next;
}teachers;
students *listtofile_stu(students *phead);
students *filetolist_stu();
teachers *listtofile_tea(teachers *phead);
teachers *filetolist_tea();
struct student *create();
void serch_xh(students *phead);
void serch_xm(struct student *phead);
void chazhao_stu(struct student *phead);
struct student *add_stu(students *phead);
struct student *change_stu(students *phead);
void xianshi_stu(students *phead);
void del_stuxh(students *phead);
void del_stuxm(students *phead);
void del_stubj(students *phead);
void del_stu(students *phead);
void xs_stu_xh( students *phead );
void xs_stu_mc( students *phead );
void xs_stu_cj( students *phead );
struct teacher *tcreate();
struct teacher *add_tea(teachers *thead);
struct teacher *del_tea(teachers *thead);
void del_tea_gh(teachers *thead);
void del_teaxm(teachers *thead);
struct teacher *change_tea(teachers *thead);
void tprintall( teachers *head );
struct teacher *xianshi_tea(teachers *thead);
void xs_tea_gh( teachers *thead );
void manager(teachers *tphead,students *phead);
void caozuo_stu(students *phead);
void caozuo_tea(teachers *phead);
void print( students *head );
void tprint( teachers *head );
void chazhao_tea(teachers *thead);
void serch_gh(teachers *thead);
void serch_txm( teachers *thead);
teachers *teacher(teachers *tphead,students *phead);
void tea_change(teachers *phead);
void tea_change_tea(teachers *phead);
void tea_change_stu(students *phead);
void tea_changestu(students *phead);
students *student(students *phead);
void stu_change(students *phead);
========================================================
/*
*
*stu.c
* Created on: 2014-12-5
* Author: yaoshenjie
*/
#include"xjgl.h"
students *listtofile_stu(students *phead)
{
FILE *fp;
students *tmp;
if((fp=fopen("student","wb"))==NULL) /*打开文件*/
{ printf("cannot open file\n");
return 0;
}
for(tmp = phead;tmp != NULL;tmp = tmp->next) /*遍历链表*/
{
fwrite(tmp,sizeof(students),1,fp); /*把数据写入文件*/
}
fclose(fp);
return phead;
}
//从文件读入链表
students *filetolist_stu(){
FILE *fp=NULL;
students *p,*p1;
students *head;
head = NULL;
if((fp = fopen("student","rb"))==NULL)
{
printf("cannot open flie\n");
return 0;
}
char c;
c=fgetc(fp);
if(c==EOF){return NULL;}
else{rewind(fp);}
p=malloc(sizeof(students));
fread(p,sizeof(students),1,fp);
while(!feof(fp))
{
// p = malloc(sizeof(students));
// fread(p,sizeof(students),1,fp);
if(head==NULL) {
head=p;
p->next=NULL;
}
else{
p1=head;
while(p1->next!=NULL){ //求得尾指针p1
p1=p1->next;
}
p1->next = p;
p->next = NULL;
p = malloc(sizeof(students));
fread(p,sizeof(students),1,fp);
}
}
free(p);
fclose(fp);
return head;
}
//添加学生
struct student *add_stu(students *phead)
{
FILE *fp=NULL;
students *p;
students *s,*r;
p=phead;
int nid,flag=0;
int flag2=1;
/*while(p->next!=NULL){ //求得尾指针
p=p->next;
}
r=p;
p=phead;*/
fp=fopen("student","wb");
while(flag==0) //控制是否继续添加
{
p=phead;
printf("输入你要添加学生的学号\n");
scanf("%d",&nid);
while(p!=NULL){ //是否存在重学号
if(nid==p->id){
printf("用户已存在,重新输入学号\n");flag2=0;
break;
}else{
flag2=1;
}
p=p->next;
}
if(flag2==1){
int nid,npasswd,nage;
char nsex,nclass[20], nam[20];
float ma,cyuy,yw;
getchar();
fflush(stdin);
printf("请输入学生的学号,姓名,密码,性别,年龄,班级,"
"数学成绩,c语言成绩,语文成绩\n");
scanf("%d",&nid);
while(getchar()!='\n');
gets(nam);
scanf("%d",&npasswd);
while(getchar()!='\n');
nsex=getchar();
scanf("%d",&nage);
while(getchar()!='\n');
gets(nclass);
scanf("%f",&ma);
scanf("%f",&cyuy);
scanf("%f",&yw);
s=malloc(sizeof(students)); //动态创建内存
s->id=nid;
s->stuna.passwd=npasswd;
s->stuna.sex=nsex;
s->stuna.age=nage;
strcpy(s->stuna.class,nclass);
strcpy(s->stuna.name,nam);
s->math=ma;
s->c=cyuy;s->yuwen=yw;
// fwrite(s,sizeof(students),1,fp);
// s=m;
if(phead==NULL)
{
phead=s;
phead->next=NULL;
}
else
{
r=phead;
while(r->next!=NULL){ //求得尾指针
r=r->next;
}
r->next=s;
s->next=NULL;
}
// printf("是否保存信息y/n\n");
// if(getchar()=='y'||getchar()=='Y'){listtofile_stu(r);}
}
printf("是否继续插入学生信息y/n\n");
while(getchar()!='\n'); //防止下面getchar取得\n
if((getchar()=='n')||(getchar()=='N')){
flag=1;break;
}
}
listtofile_stu(s);
fclose(fp);
// stu_listtofile(phead->next,"student");
return phead;
}
//修改学生信息
students *change_stu(students *phead){
students *p;
students *r;
// FILE *fp=NULL;
// fp=fopen("student","wb");
int nid;
int flag=0;
int flag2=0;
while(flag==0){
printf("输入要修改学生学号\n");
scanf("%d",&nid);
//while(getchar()!='\n');
p = phead;
while(p!=NULL){ //是否存在此学生
// fread(p,sizeof(students),1,fp);
if(nid==p->id){
flag2=0;
break;
}else{
flag2=1; //不存在此学生
}
p=p->next;
}r=p; p=phead;
if(flag2==1){
printf("此学生不存在\n");
}
else{
int num;
int npasswd=0,nage=0;
char nsex,nclass[20],nam[20];
float ma=0,cyuy=0,yw=0;
printf("输入要修改学生的信息1.学号2.姓名,3.密码,4.性别,5.年龄,6.班级"
"7.数学成绩,8.c成绩,9.语文成绩\n");
scanf("%d",&num);
switch(num){
case 1:printf("输入新的学号\n");
scanf("%d",&nid);
r->id=nid;//free(r);
break;
case 2:printf("输入新的姓名\n");
scanf("%s",nam);
strcpy(r->stuna.name,nam);
break;
case 3:printf("输入新的密码\n");
scanf("%d",&npasswd);r->stuna.passwd=npasswd;
break;
case 4:printf("输入新的性别\n");
while(getchar()!='\n');
nsex=getchar();r->stuna.sex=nsex;
break;
case 5:printf("输入新的年龄\n");
scanf("%d",&nage);r->stuna.age=nage;
break;
case 6:printf("输入新的班级名\n");
scanf("%s",nclass);
strcpy(r->stuna.class,nclass);//free(r);
break;
case 7:printf("输入新的数学成绩\n");
scanf("%f",&ma);
r->math=ma;//free(r);
break;
case 8:printf("输入新的c语言成绩\n");
scanf("%f",&cyuy);
r->c=cyuy;//free(r);
break;
case 9:printf("输入新的语文成绩\n");
scanf("%f",&yw);
r->yuwen=yw;//free(r);
break;
default :printf("不做修改\n");break;
}
// fwrite(r,sizeof(students),1,fp);
//free(r);
// r=NULL;
} // getchar();
// getchar();
// fflush(stdin);
// listtofile_stu(r);
printf("修改成功\n");
printf("是否继续修改学生信息y/n\n");
while(getchar()!='\n'); //防止下面getchar取得\n
if((getchar()=='n')||(getchar()=='N')){
break;
}else{
flag=0;
}
}
//fclose(fp);
return phead;
}
//删除学生
void del_stu(students *phead){
students *p;
//students *r=NULL;
int num;
p=phead;
//FILE *fp=NULL;
//fp=fopen("student","wb");
//char nname[20],nclass[20];
* xjgl.h
*
* Created on: 2014-12-5
* Author: yao
*/
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
//#include"stu.c"
//#include"tea.c"
//学生结构体
typedef struct student
{
int id;
struct stuname
{
int passwd;
char class[20];
char name[20];
char sex;
int age;
}stuna;
float math;
float c;
float yuwen;
float ave;
struct student *next;
}students;
typedef struct teacher{ //教师结构体
int tid;
int tpasswd;
char tsex;
int tage;
char tname[20];
struct teacher *next;
}teachers;
students *listtofile_stu(students *phead);
students *filetolist_stu();
teachers *listtofile_tea(teachers *phead);
teachers *filetolist_tea();
struct student *create();
void serch_xh(students *phead);
void serch_xm(struct student *phead);
void chazhao_stu(struct student *phead);
struct student *add_stu(students *phead);
struct student *change_stu(students *phead);
void xianshi_stu(students *phead);
void del_stuxh(students *phead);
void del_stuxm(students *phead);
void del_stubj(students *phead);
void del_stu(students *phead);
void xs_stu_xh( students *phead );
void xs_stu_mc( students *phead );
void xs_stu_cj( students *phead );
struct teacher *tcreate();
struct teacher *add_tea(teachers *thead);
struct teacher *del_tea(teachers *thead);
void del_tea_gh(teachers *thead);
void del_teaxm(teachers *thead);
struct teacher *change_tea(teachers *thead);
void tprintall( teachers *head );
struct teacher *xianshi_tea(teachers *thead);
void xs_tea_gh( teachers *thead );
void manager(teachers *tphead,students *phead);
void caozuo_stu(students *phead);
void caozuo_tea(teachers *phead);
void print( students *head );
void tprint( teachers *head );
void chazhao_tea(teachers *thead);
void serch_gh(teachers *thead);
void serch_txm( teachers *thead);
teachers *teacher(teachers *tphead,students *phead);
void tea_change(teachers *phead);
void tea_change_tea(teachers *phead);
void tea_change_stu(students *phead);
void tea_changestu(students *phead);
students *student(students *phead);
void stu_change(students *phead);
========================================================
/*
*
*stu.c
* Created on: 2014-12-5
* Author: yaoshenjie
*/
#include"xjgl.h"
students *listtofile_stu(students *phead)
{
FILE *fp;
students *tmp;
if((fp=fopen("student","wb"))==NULL) /*打开文件*/
{ printf("cannot open file\n");
return 0;
}
for(tmp = phead;tmp != NULL;tmp = tmp->next) /*遍历链表*/
{
fwrite(tmp,sizeof(students),1,fp); /*把数据写入文件*/
}
fclose(fp);
return phead;
}
//从文件读入链表
students *filetolist_stu(){
FILE *fp=NULL;
students *p,*p1;
students *head;
head = NULL;
if((fp = fopen("student","rb"))==NULL)
{
printf("cannot open flie\n");
return 0;
}
char c;
c=fgetc(fp);
if(c==EOF){return NULL;}
else{rewind(fp);}
p=malloc(sizeof(students));
fread(p,sizeof(students),1,fp);
while(!feof(fp))
{
// p = malloc(sizeof(students));
// fread(p,sizeof(students),1,fp);
if(head==NULL) {
head=p;
p->next=NULL;
}
else{
p1=head;
while(p1->next!=NULL){ //求得尾指针p1
p1=p1->next;
}
p1->next = p;
p->next = NULL;
p = malloc(sizeof(students));
fread(p,sizeof(students),1,fp);
}
}
free(p);
fclose(fp);
return head;
}
//添加学生
struct student *add_stu(students *phead)
{
FILE *fp=NULL;
students *p;
students *s,*r;
p=phead;
int nid,flag=0;
int flag2=1;
/*while(p->next!=NULL){ //求得尾指针
p=p->next;
}
r=p;
p=phead;*/
fp=fopen("student","wb");
while(flag==0) //控制是否继续添加
{
p=phead;
printf("输入你要添加学生的学号\n");
scanf("%d",&nid);
while(p!=NULL){ //是否存在重学号
if(nid==p->id){
printf("用户已存在,重新输入学号\n");flag2=0;
break;
}else{
flag2=1;
}
p=p->next;
}
if(flag2==1){
int nid,npasswd,nage;
char nsex,nclass[20], nam[20];
float ma,cyuy,yw;
getchar();
fflush(stdin);
printf("请输入学生的学号,姓名,密码,性别,年龄,班级,"
"数学成绩,c语言成绩,语文成绩\n");
scanf("%d",&nid);
while(getchar()!='\n');
gets(nam);
scanf("%d",&npasswd);
while(getchar()!='\n');
nsex=getchar();
scanf("%d",&nage);
while(getchar()!='\n');
gets(nclass);
scanf("%f",&ma);
scanf("%f",&cyuy);
scanf("%f",&yw);
s=malloc(sizeof(students)); //动态创建内存
s->id=nid;
s->stuna.passwd=npasswd;
s->stuna.sex=nsex;
s->stuna.age=nage;
strcpy(s->stuna.class,nclass);
strcpy(s->stuna.name,nam);
s->math=ma;
s->c=cyuy;s->yuwen=yw;
// fwrite(s,sizeof(students),1,fp);
// s=m;
if(phead==NULL)
{
phead=s;
phead->next=NULL;
}
else
{
r=phead;
while(r->next!=NULL){ //求得尾指针
r=r->next;
}
r->next=s;
s->next=NULL;
}
// printf("是否保存信息y/n\n");
// if(getchar()=='y'||getchar()=='Y'){listtofile_stu(r);}
}
printf("是否继续插入学生信息y/n\n");
while(getchar()!='\n'); //防止下面getchar取得\n
if((getchar()=='n')||(getchar()=='N')){
flag=1;break;
}
}
listtofile_stu(s);
fclose(fp);
// stu_listtofile(phead->next,"student");
return phead;
}
//修改学生信息
students *change_stu(students *phead){
students *p;
students *r;
// FILE *fp=NULL;
// fp=fopen("student","wb");
int nid;
int flag=0;
int flag2=0;
while(flag==0){
printf("输入要修改学生学号\n");
scanf("%d",&nid);
//while(getchar()!='\n');
p = phead;
while(p!=NULL){ //是否存在此学生
// fread(p,sizeof(students),1,fp);
if(nid==p->id){
flag2=0;
break;
}else{
flag2=1; //不存在此学生
}
p=p->next;
}r=p; p=phead;
if(flag2==1){
printf("此学生不存在\n");
}
else{
int num;
int npasswd=0,nage=0;
char nsex,nclass[20],nam[20];
float ma=0,cyuy=0,yw=0;
printf("输入要修改学生的信息1.学号2.姓名,3.密码,4.性别,5.年龄,6.班级"
"7.数学成绩,8.c成绩,9.语文成绩\n");
scanf("%d",&num);
switch(num){
case 1:printf("输入新的学号\n");
scanf("%d",&nid);
r->id=nid;//free(r);
break;
case 2:printf("输入新的姓名\n");
scanf("%s",nam);
strcpy(r->stuna.name,nam);
break;
case 3:printf("输入新的密码\n");
scanf("%d",&npasswd);r->stuna.passwd=npasswd;
break;
case 4:printf("输入新的性别\n");
while(getchar()!='\n');
nsex=getchar();r->stuna.sex=nsex;
break;
case 5:printf("输入新的年龄\n");
scanf("%d",&nage);r->stuna.age=nage;
break;
case 6:printf("输入新的班级名\n");
scanf("%s",nclass);
strcpy(r->stuna.class,nclass);//free(r);
break;
case 7:printf("输入新的数学成绩\n");
scanf("%f",&ma);
r->math=ma;//free(r);
break;
case 8:printf("输入新的c语言成绩\n");
scanf("%f",&cyuy);
r->c=cyuy;//free(r);
break;
case 9:printf("输入新的语文成绩\n");
scanf("%f",&yw);
r->yuwen=yw;//free(r);
break;
default :printf("不做修改\n");break;
}
// fwrite(r,sizeof(students),1,fp);
//free(r);
// r=NULL;
} // getchar();
// getchar();
// fflush(stdin);
// listtofile_stu(r);
printf("修改成功\n");
printf("是否继续修改学生信息y/n\n");
while(getchar()!='\n'); //防止下面getchar取得\n
if((getchar()=='n')||(getchar()=='N')){
break;
}else{
flag=0;
}
}
//fclose(fp);
return phead;
}
//删除学生
void del_stu(students *phead){
students *p;
//students *r=NULL;
int num;
p=phead;
//FILE *fp=NULL;
//fp=fopen("student","wb");
//char nname[20],nclass[20];