以下是引用张耀元在2016-12-31 13:18:55的发言:
还是不行,成绩不出来。
试了一下,应该可以。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//学生信息
struct work
{
char no[10];//学生学号
char name[12];//学生姓名
char idno[20];//身份证号码
char mima[10];//密码
float chengji1;//成绩1
float chengji2;//成绩2
float chengji3;//成绩3
char powr[10];//权限
struct work *next;
};
//用链表将文件里的学生基本信息读出来
struct work *lianbiao()
{
struct work *head=NULL;
struct work *node=NULL;
FILE *fp=NULL;
if((fp=fopen("jiben.txt","rb"))==NULL)
{
printf("打开文件失败!");
return NULL;
}
else
{
while(!feof(fp))
{
node=(struct work *)malloc(sizeof(struct work));
if(fread(node,sizeof(struct work),1,fp)==1)
{
node->next=head;
head=node;
}
else
free(node);
}
}
fclose(fp);
return head;
}
//普通学生查询某门课程的得分
void chaxunmoumenke(struct work *head)
{
//system("CLS");
struct work *p=head;
char x;
while (1)
{
printf("请输入需要查询的课程(课程1/课程2/课程3)[请输入1,2,3,0]:\n");
while (((x=getch()) < '0') || (x > '3')) ;
if (x == '0')
break;
p = head;
while (p != NULL)
{
if (x=='1')
printf("%.2f\n", p->chengji1);
else if (x=='2')
printf("%.2f\n", p->chengji2);
else if (x=='3')
printf("%.2f\n", p->chengji3);
p = p->next;
}
}
}
void _list(struct work *head)
{
struct work *p=head;
while (p != NULL)
{
printf("学生学号....%s\n", p->no);
printf("学生姓名....%s\n", p->name);
printf("身份证号码..%s\n", p->idno);
printf("密码........%s\n", p->mima);
printf("成绩1.......%.2f\n", p->chengji1);
printf("成绩2.......%.2f\n", p->chengji2);
printf("成绩3.......%.2f\n", p->chengji3);
printf("权限........%s\n\n", p->powr);
p = p->next;
}
}
void _free(struct work *head)
{
struct work *p=head, *q;;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
}
main()
{
struct work *head=lianbiao();
_list(head);
chaxunmoumenke(head);
_free(head);
}