大一上学期C语言课程设计
总结:程序中还存在很多的bug,也有很多功能没完善,但这是人生中第一个课设,故发表出来纪念一下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct student)
struct student
{
char num[15];
char name[10];
int clas;
int Math;
int English;
int Physic;
int sum;
int rank;
struct student *next;
} ;
void create(void) //录入信息
{
struct student *head,*p1,*p2;
FILE *fp;
int n=0;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
printf("please enter the schoolnumber(such as 5116),name,class(such as 11),and the score of Math,English and Physic:\n");
scanf("%s",p1->num);
getchar();
while(p1->num[0]!='@') //录入信息以@结束
{
scanf("%s%d%d%d%d",p1->name,&p1->clas,&p1->Math,&p1->English,&p1->Physic); //出错,因没加&!!!
p1->rank=0;
p1->sum=0;
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s",p1->num);
getchar();
}
p2->next=NULL;
free(p1);
printf("record is OK!\n");
if((fp=fopen("student.txt","w"))==NULL)
{
printf("cannot open file!\n");
//fclose(fp);
exit(0);
}
p1=head;
while(p1!=NULL) //数据存放再文件中
{
fprintf(fp,"%s %s %d %d %d %d %d %d\n",p1->num,p1->name,p1->clas,p1->Math,p1->English,p1->Physic,p1->sum,p1->rank);
p1=p1->next;
}
fclose(fp);
}
/***********
void record(struct student *p1)//讲链表中的数据放在文件student.txt中
{
FILE *fp;
struct student *p;
//char filename[10];
p=p1;
//printf("...........\n");
//scanf("%s",filename);
if((fp=fopen("student.txt","w"))==NULL)
{
printf("cannot open file!\n");
//fclose(fp);
exit(0);
}
while(p!=NULL) //数据存放再文件中
{
fprintf(fp,"%s %s %d %d %d %d %d %d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
p=p->next;
}
free(p); //释放内存 // 出错! 因把p1内存释放导致出错
fclose(fp);
}
*********/
void print(struct student *head) //输出全部数据函数
{
struct student *p;
p=head;
printf("---------------------------STUDENT--------------------------------------\n");
printf("|num |name |clas |Math |English |Physic |sum |rank |\n");
printf("------------------------------------------------------------------------\n");
while(p!=NULL)
{
printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
p=p->next;
}
}
void searchbyscore(struct student *head) //按成绩段查询信息
{
struct student *p;
char s[10];
int a,b,k;
p=head;
printf("please enter the range of score,such as 60--70:\n");
scanf("%d--%d",&a,&b);
if(a>b||a==b)
printf("Your enter is wrong !\n");
else
{
printf("please choose the course ,Math,English or Physic:\n");
scanf("%s",s); //存放要查询的成绩
if(strcmp(s,"Math")==0)
{
k=0;
while(p!=NULL)
{
if(p->Math>=a&&p->Math<=b)
{
k++;
if(k==1)
{
printf("---------------------------STUDENT--------------------------------------\n");
printf("|num |name |clas |Math |English |Physic |sum |rank |\n");
printf("------------------------------------------------------------------------\n");
}
printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
}
p=p->next;
}
if(k==0)
printf("not found the statistic !\n");
}
else if(strcmp(s,"English")==0)
{
k=0;
while(p!=NULL)
{
if(p->English>=a&&p->English<=b)
{
k++;
if(k==1)
{
printf("---------------------------STUDENT--------------------------------------\n");
printf("|num |name |clas |Math |English |Physic |sum |rank |\n");
printf("------------------------------------------------------------------------\n");
}
printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
}
p=p->next;
}
if(k==0)
printf("not found the statistic !\n");
}
else if(strcmp(s,"Physic")==0)
{
k=0;
while(p!=NULL)
{
if(p->Physic>=a&&p->Physic<=b)
{
k++;
if(k==1)
{
printf("---------------------------STUDENT--------------------------------------\n");
printf("|num |name |clas |Math |English |Physic |sum |rank |\n");
pri

最低0.47元/天 解锁文章
825

被折叠的 条评论
为什么被折叠?



