急!!请高手帮忙解决这个学生成绩管理程序中的错误!!!
老师让我们写一个学生成绩管系统 由于刚学没多久 不太会写 于是从网上参考了一些然后自己又改了一点 程序写成以后有许多问题 程序如下:程序主要功能是把学生记录按文件的形式存储,按学号ID、姓名来查找学生信息,按高分到低分将学生信息进行排序
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define DATA_FILE "cjfile" /*首先定义一个文件学生信息将写入此文件中*/
struct record /*定义一个学生结构*/
{
char Name[30 ];
char ID[30];
int Math;
int English;
int Compute;
int total;
} Student;
struct node /*构造一个学生链表结构 */
{
char Name[30];
char ID[30];
int Math;
int English;
int Compute;
int total;
struct node *next;
} *head;
int quit; /*定义一个全局变量在主程序中用来控制退出*/
/*主程序*/
main()
{
int cmd; /*接收命令选项的整型变量*/
struct node *makelist(); /*构造链表*/
quit=0;
while( !quit )
{
cmd=Menu(); /*显示菜单*/
switch( cmd )
{
case 1:
Print(); break;
case 2:
Add(); break;
case 3:
if((head=makelist())!=NULL) outputlist(head); break; /**输出排序后的信息/
case 4:
Find(4); break;
case 5:
Find(5); break;
case 6:
quit=1;
printf(" Thank you for your using .");
break;
default:
printf(" Please Input a number between\t1\tto\t6.\n");
}
if (quit!=1)
{
printf(" Press any key to Return Main Menu ....\n");
getch();
}
}
}
/*菜单选项函数*/
int Menu()
{
int cmd=0;
puts("1 List the Records in the data file");
puts("2 Add New Record");
puts("3 Sort by high to low");
puts("4 Find a Record from the ID");
puts("5 Find a Record from the Name");
puts("6 Quit");
printf("\n\n Input your choice : ");
scanf("%d", &cmd);
printf("\n");
return cmd;
}
/*输入信息函数*/
InputRec( struct node *stu)
{
if(stu==NULL)
return;
printf("\n Name = ");
scanf("%s",stu->Name);
printf("\n ID = ");
scanf("%s" , stu->ID);
printf("\n Math = ");
scanf("%d", &(stu->Math));
printf("\n English = ");
scanf("%d" ,&(stu->English));
printf("\n Compute = ");
scanf("%d" , &(stu->Compute));
printf("\n");
}
//输出函数//
OutputRec( struct node *stu)
{
printf("\n");
printf(" Name : %s", stu->Name);
printf("\n ID : %s\n",stu->ID );
printf("Math = %d\n" ,stu->Math);
printf("English = %d\n",stu->English);
printf("Compute = %d\n", stu->Compute);
printf("total= %d\n",stu->Math+stu->English+stu->Compute);
printf("\n");
}
//从文件读取信息//
int ReadData(FILE *fp, struct node *TextRec)
{
int r;
if(( fp==NULL ) || ( TextRec==NULL))
return 0;
r = fread(TextRec ,sizeof(Student) ,1 ,fp);
if(r != 1)
return 0;
return 1;
}
//向文件写入信息//
int WriteData(FILE *fp, struct node *ScrRec)
{
int r;
if((fp == NULL) || (ScrRec == NULL))
return 0;
r = fwrite(ScrRec , sizeof(Student) , 1, fp);
if(r != 1)
return 0;
return 1;
}
/*将文件内的学生信息显示到屏幕上*/
Print()
{
struct node *rec;
int i=0;
FILE *fp;
fp=fopen(DATA_FILE, "rb");
if(fp==NULL)
{
printf(" Can not open the data file : %s\n" , DATA_FILE);
}
while(ReadData(fp ,rec))
{
OutputRec(rec);
printf(" ------------------------------------------");
i++;
printf("\n Press any key to continue ... \n");
getch();
}
printf("\n The current data file have %d\trecord.\n" , i );
fclose(fp);
}
//添加学生信息//
Add()
{
struct node *rec;
FILE* fp;
InputRec(rec );
fp=fopen(DATA_FILE ,"ab");
if( fp==NULL)
{
printf(" Can not open the data file to write into ... \n");
}
if( WriteData(fp, rec)==1)
printf("\n\n Add New Record Success \n\n");
else
printf("\n\n Failed to Write New Record into the data file \n");
fclose(fp);
}
//按ID、Name查找学生信息//
Find(int n)
{
char buf[30];
struct node *rec;
int i=0;
FILE *fp;
fp = fopen(DATA_FILE ,"rb");
if( fp==NULL)
{
printf("\n Can not open the data file to search ...\n");
}
switch(n)
{
case 4:
printf("\n Please Input the ID : ");
scanf("%s",buf);
while(ReadData(fp , rec))
{
if(strcmp(rec->ID , buf)==0)
{
i++;
OutputRec(rec);
printf(" ------------------------------------------\n");
}
}
break;
case 5:
printf("\n Please Input the Name : ");
scanf("%s",buf);
while(ReadData(fp , rec))
{
if(strcmp(rec->Name , buf) == 0)
{
i++;
OutputRec(rec);
printf(" ------------------------------------------\n");
}
}
break;
default:
printf(" \nPlease type right choice ...\n");
}
if(i>0)
{
printf("\n Have find out\t%d\trecord\n" ,i);
}
else
printf("\n sorry !.\n failed to find out the one you want .\n");
fclose(fp);
}
//构造排序链表//
struct node *makelist()
{
struct node *p,*u,*v,*h;
FILE *fp;
if((fp=fopen(DATA_FILE,"rb"))==NULL)
{
printf("Can't open file %s.\n");
return NULL;
}
h=NULL;
p=(struct node *)malloc(sizeof(struct node));
while(ReadData(fp,p)!=NULL)
{
v=h;
while(v&&p->total<=v->total)
{
u=v;
v=v->next;
}
if(v==h)
h=p;
else
u->next=p;
p->next=v;
p=(struct node *)malloc(sizeof(struct node));
}
free(p);
fclose(fp);
return h;
}
//输出链表//
outputlist(struct node *h)
{
while(h!=NULL)
{
OutputRec(h);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
h=h->next;
}
return;
}
这个程序编译能够通过只是出现了许多象这样的警告“possible use of 'rec' before definition in function ****”(****在这个程序中代表编写的 Print 、Add、 Find函数 在上已用有色字体标出) 这个程序的最大的问题就是不能够对学生信息进行排序 按照这个程序编写的链表函数(已用有色字体标出) 显示出来的结果还是按输入顺序写入的信息 并没有从高分到低分显示!!!
请问该怎么解决?