回复 6楼 wp231957
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define num 10
//学生人数
typedef struct date
// 日期结构体
{
short year;
unsigned short month;
unsigned short day;
}DATE;
typedef struct fenshu
{
int chinese;
int math;
int C;
int english;
}FEN;
typedef struct student // 学生信息结构体
{
char name[10];
char studentID[20]; // 学号
unsigned short gender; // 性别
DATE birthday;
char address[100];
FEN Score; // 4门课程分数
}STUDENT;
typedef struct link
// 双向链表结构体
{
STUDENT nodeData; // 数据
struct link *last;
// 父节点指针
struct link *next;
// 子节点指针
}LINK;
void deletememory(LINK *head)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
LINK *p=NULL;
while(head!=NULL)
{
p=head->next;
free(head);
head=p;
}
}
void change(STUDENT *p1,STUDENT *p2)
{
int i;
strcpy(p1->name,p2->name);
strcpy(p1->studentID,p2->studentID);
p1->gender=p2->gender;
p1->birthday.year=p2->birthday.year;
p1->birthday.month=p2->birthday.month;
p1->birthday.day=p2->birthday.day;
strcpy(p1->address,p2->address);
p1->Score.C=p2->Score.C;
p1->Score.chinese=p2->Score.chinese;
p1->Score.english=p2->Score.english;
p1->Score.math=p2->Score.math;
}
void exchange(STUDENT *p1,STUDENT *p2)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
STUDENT n;
change(&n,p1);
change(p1,p2);
change(p2,&n);
}
void writefile(LINK *head)
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
int i;
FILE *p;
if ((p = fopen("neww.txt","a+")) == NULL)
//
{
printf("写数据文件失败。");
exit(0);
}
while(head!=NULL)
{
fwrite(&(head->nodeData),sizeof(STUDENT),1,p);
head=head->next;
}
fclose(p);
printf("写数据文件成功");
printf("\n");
getch();
}
LINK *AppendNode(LINK *head, STUDENT *b) //b指向的数据作为一个节点数据加到双向链表尾
{
int i;
LINK *s = NULL,*p=head;
s = (struct link*)malloc(sizeof(struct link));
//新建节点
if(s == NULL)
{
printf("No enough memory to allocate\n");
exit(0);
}
if(head == NULL)
{
head=s;
head->last=NULL;
}
else
{
while(p->next!=NULL)
p=p->next;
p->next=s;
s->last=p;
}
strcpy(s->nodeData.name,b->name);
strcpy(s->nodeData.studentID,b->studentID);
s->nodeData.gender=b->gender;
s->nodeData.birthday.year=b->birthday.year;
s->nodeData.birthday.month=b->birthday.month;
s->nodeData.birthday.day=b->birthday.day;
strcpy(s->nodeData.address,b->address);
s->nodeData.Score.C=b->Score.C;
s->nodeData.Score.chinese=b->Score.chinese;
s->nodeData.Score.english=b->Score.english;
s->nodeData.Score.math=b->Score.math;
s->next=NULL;
return head;
}
LINK *sort(LINK *head) //节点排序
{
LINK *p;
while(head!=NULL)
{
p=head->next;
while(p!=NULL)
{
if(strcmp(head->nodeData.name,p->nodeData.name)>0)
//按学号排序
{
exchange(&(head->nodeData),&(p->nodeData));
}
p=p->next;
}
head=head->next;
}
return head;
}
LINK *input(LINK *head)
//输入
{
STUDENT b;
char ch='y';
while(ch=='y'||ch=='Y')
{
fflush(stdin);
printf("输入姓名\n");
gets(b.name) ;
printf("输入学号\n");
gets(b.studentID);
printf("输入地址\n");
gets(b.address);
printf("输入出生年月日\n");
scanf("%d%d%d",&b.birthday.year,&b.birthday.month,&b.birthday.day);
printf("输入Chinese,math,C,english成绩\n");
fflush(stdin);
scanf("%d%d%d%d",&b.Score.chinese,&b.Score.math,&b.Score.C,&b.Score.english);
head=AppendNode(head,&b);
printf("\n 继续输入学生信息?(y/n)");
ch=getch();
printf("\n");
}
writefile(head);
return head;
}
LINK *DeleteNode(LINK *head)
//删除某一节
!!!!!!!!!!!!!!!!!!!!!!1
{
char name[20];
LINK *p=head;
fflush(stdin);
printf("请输入所要删除学生的");
gets(name);
while(p->next!=NULL&&strcmp(p->nodeData.name,name)!=0)
p=p->next;
if(strcmp(name,p->nodeData.name)==0)
{
if(p==head)
{
head=head->next;
head->last=NULL;
}
else if(p->next==NULL)
p->last->next=NULL;
else
{
p->last->next=p->next;
p->next->last=p->last;
}
free(p);
printf("删除成功") ;
}
writefile(head);
return head;
}
void search(LINK *head)
//查找学生信息
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
char name[30];
fflush(stdin);
printf("输入所查询学生的姓名");
gets(name);
while(head->next!=NULL&&strcmp(head->nodeData.name,name)!=0)
head=head->next;
if(strcmp(head->nodeData.name,name)==0)
{
printf("%s",head->nodeData.name);
printf("%s",head->nodeData.studentID);
printf("%s,%s,%d\n",head->nodeData.name,head->nodeData.studentID,head->nodeData.gender);
printf("%d,%d,%d,%d\n",head->nodeData.Score.C,head->nodeData.Score.chinese,head->nodeData.Score.english,head->nodeData.Score.math);
printf("%d,%d,%d\n",head->nodeData.birthday.day,head->nodeData.birthday.month,head->nodeData.birthday.year);
printf("%s\n",head->nodeData.address);
}
else
printf("无此人");
}
LINK *reviseinput(LINK *head)
//修改信息的输入 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
LINK *p=head;
char id[20],con;
fflush(stdin);
printf("输入学生id");
gets(id);
while(strcmp(head->nodeData.studentID,id)<0&&head->next!=NULL)
head=head->next;
if(strcmp(head->nodeData.studentID,id)==0)
{
printf("%s,%s",head->nodeData.studentID,head->nodeData.name);
}
printf("是否修改此人信息(y/n)");
con=getch();
if(con=='y')
{
printf("输入姓名");
gets(head->nodeData.name);
fflush(stdin);
printf("输入四门分数C,chinese,english,math");
scanf("%d%d%d%d",&head->nodeData.Score.C,&head->nodeData.Score.chinese,&head->nodeData.Score.english,&head->nodeData.Score.math);
}
writefile(p);
return head;
}
int INPUT()
//主界面
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
int n;
char ch='8';
while(ch<'0'||ch>'7')
{
printf(" 1查询成绩\n");
printf(" 2输入\n");
printf(" 3删除\n");
printf(" 4修改\n");
printf(" 5排序\n");
printf (" 6统计\n");
printf("0退出系统\n");
ch=getch();
}
n=(int)(ch-'0');
return n;
}
LINK *count(LINK *head)
//统计
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
LINK *b;
double average=0,pass=0,i=0,j=0,k=0,l=0,n=0;
//及格率,方差 ijkl为统计人数
int m=0,o=0,q,p;
double countc=0,countmath=0,countchinese=0,countenglish=0,nn=0;
double change=0;
char NAME[num][20];
double score[num][4],countadd[num];
double temp;
char swap[1][20];
for(b=head;b!=NULL;b=b->next)
//计算每门成绩平均分 ,一个人四科总分
{
countc=countc+b->nodeData.Score.C;
countmath=countmath+b->nodeData.Score.math;
countchinese=countchinese+b->nodeData.Score.chinese;
countenglish=countenglish+b->nodeData.Score.english;
countadd[o]=b->nodeData.Score.C+b->nodeData.Score.chinese+b->nodeData.Score.english+b->nodeData.Score.math;
if(b->nodeData.Score.C>60)
i++;
if(b->nodeData.Score.math>60)
j++;
if(b->nodeData.Score.chinese>60)
k++;
if(b->nodeData.Score.english>60)
l++;
score[m][0]=b->nodeData.Score.C;
score[m][1]=b->nodeData.Score.chinese;
score[m][2]=b->nodeData.Score.english;
score[m][3]=b->nodeData.Score.math;
strcpy(NAME[m],b->nodeData.name);
m++;
o++;
n++;
nn++;
}
printf("%lf,%lf,%lf,%lf\n",i/nn,j/nn,k/nn,l/nn);
for(p=0;p<nn-1;p++)
//总分
{
for(q=p+1;q<nn;q++)
{
if(countadd[q]>countadd[p])
{
change=countadd[q];
countadd[p]=countadd[q];
countadd[q]=change;
strcpy(swap[1],NAME[q]);
strcpy(NAME[q],NAME[p]);
strcpy(NAME[p],swap[1]);
}
}
}
for(p=0;p<nn;p++)
{
printf("总分第%d名为",(p+1));
puts(NAME[p]);
}
for(p=0;i<nn;i++)
//C分
for(q=1;j<nn;j++)
{
if(score[q][0]>score[p][0])
{
temp=score[q][0];
score[p][0]=score[q][0];
score[q][0]=temp;
strcpy(swap[1],NAME[q]);
strcpy(NAME[q],NAME[p]);
strcpy(NAME[p],swap[1]);
}
}
for(p=0;p<nn;p++)
{
printf("C第%d名为",(p+1));
puts(NAME[p]) ;
}
for(p=0;i<nn;i++)
//C分
for(q=1;j<nn;j++)
{
if(score[q][1]>score[p][1])
{
temp=score[q][1];
score[p][1]=score[q][1];
score[q][1]=temp;
strcpy(swap[1],NAME[q]);
strcpy(NAME[q],NAME[p]);
strcpy(NAME[p],swap[1]);
}
}
for(p=0;p<nn;p++)
{
printf("语文第%d名为",(p+1));
puts(NAME[p]) ;
}
for(p=0;i<nn;i++)
//C分
for(q=1;j<nn;j++)
{
if(score[q][2]>score[p][2])
{
temp=score[q][2];
score[p][2]=score[q][2];
score[q][2]=temp;
strcpy(swap[1],NAME[q]);
strcpy(NAME[q],NAME[p]);
strcpy(NAME[p],swap[1]);
}
}
for(p=0;p<nn;p++)
{
printf("英语第%d名为",(p+1));
puts(NAME[p]) ;
}
for(p=0;i<nn;i++)
//C分
for(q=1;j<nn;j++)
{
if(score[q][3]>score[p][3])
{
temp=score[q][3];
score[p][3]=score[q][3];
score[q][3]=temp;
strcpy(swap[1],NAME[q]);
strcpy(NAME[q],NAME[p]);
strcpy(NAME[p],swap[1]);
}
}
for(p=0;p<nn;p++)
{
printf("数学第%d名为",(p+1));
puts(NAME[p]) ;
}
getch();
return head;
}
LINK *ReadFile()
//读文件
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
~~~~~~~~~~~~~~~~~~~~~~~~
{
int i;
FILE *p;
LINK *head=NULL;
STUDENT s;
p = fopen("neww。txt","r");
if (p == NULL)
//
{
printf("读数据文件失败");
return head;
}
fgetc(p);
if(feof(p))
return head;
rewind(p);
while(!feof(p))
{
fread(&s,sizeof(STUDENT),1,p);
head=AppendNode(head,&s);
}
fclose(p);
printf("读取数据文件成功");
while(head->next!=NULL)
head=head->next;
head=head->last;
deletememory(head->next);
head->next=NULL;
while(head->last!=NULL)
head=head->last;
return head;
}
void sortname(LINK *head)
//排序功能
名字
{
LINK *p;
while(head!=NULL)
{
p=head->next;
while(p!=NULL)
{
if(strcmp(head->nodeData.name,p->nodeData.name)>0)
exchange(&(head->nodeData),&(p->nodeData));
p=p->next;
}
head=head->next;
}
}
void sortid(LINK *head)
//排序功能
学号
{
LINK *p;
while(head!=NULL)
{
p=head->next;
while(p!=NULL)
{
if(strcmp(head->nodeData.studentID,p->nodeData.studentID)>0)
exchange(&(head->nodeData),&(p->nodeData));
p=p->next;
}
head=head->next;
}
}
void DisplayLink(LINK *head)
{
if(head==NULL)
{
printf("没有记录!按任意键继续\n");
}
while(head!=NULL)
{
printf("名字%s\n",head->nodeData.name);
printf("学号%s\n", head->nodeData.studentID);
printf("性别%d\n",head->nodeData.gender);
printf("出生年%d,月%d,日%d\n", head->nodeData.birthday.year, head->nodeData.birthday.month,head->nodeData.birthday.day);
printf("地址%s\n", head->nodeData.address);
printf("成绩C%d
语文%d
英语%d
数学%d",head->nodeData.Score.C,head->nodeData.Score.chinese,head->nodeData.Score.english,head->nodeData.Score.math);
printf("\n");
head=head->next;
}
printf("已经显示完全部记录");
getch();
}
int check()
{
char mi[7]={};
int i;
char pass[10];
printf("输入6位密码:");
for(i=0;i<6;i++)
{
pass[i]=getch();
}
pass[6]='\0';
if(strcmp(pass,mi)!=0)
{
printf(" 密码错误 ");
getch();
return 0;
}
printf("\n\n\t 密码正确");
getch();
return 1;
}
void main()
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
{
int i=1,n;
char ch,chch,*q=NULL;
LINK *head = NULL;
// n=check();
//if(n==1)
head=ReadFile();
while(i!=0)
{
i=INPUT();
switch(i)
{
case 1:
printf("查询成绩");
search(head);
break;
case 2:
printf("输入信息\n");
head=input(head);
break;
case 3:
printf("删除");
head=DeleteNode(head);
break;
case 4:
printf("修改");
reviseinput(head);
break;
case 5:
printf("排序");
printf("按姓名排序输入1.按学号排序输入2") ;
printf("\n");
ch=getch();
if(ch==1)
sortname(head);
else
sortid(head);
break;
case 6:
printf("统计");
head=count(head);
break;
case 0:
printf("是否退出(y/n)");
chch=getch();
if(chch=='y')
i=0;
else
i=1;
}
}
sort(head);
DisplayLink(head);
//deletememory(head);
}
现在读文件不行,是怎么了,一读文件,直接说失败~~
txt和程序都是在同一个文件里的