C语言课程设计

//用C语言设计一个简单的学生信息管理,读学生信息,并对其进行操作:查找,排序,并将结果写入另一个文件,求平均分和总分等

#include<stdio.h>
#include<malloc.h>
#include<string.h>
float sum(struct student *stu);
float average(struct student *stu);
struct student * search(struct student *stu, char s[], char nx);
int inputf(struct student **stu);
int outputf(struct student *stu);
void output(struct student *stu);
void paixu(struct student **stu);
/
struct student{
 char xuehao[12];
 char xingming[12];
 float yuwen,shuxue,yingyu,jisuanji;
 struct student *next;
};
/
float sum(struct student *stu) //求求一个学生成绩的和
{
return stu->yuwen+stu->shuxue+stu->yingyu+stu->jisuanji;
}
/
float average(struct student *stu)  //求一个学生成绩平均分
{
return sum(stu)/4;
}
/
struct student * search(struct student *stu, char s[], char nx) // 按姓名或学号查找,nx=n姓名,nx=x学号
{
struct student *p;
if(stu==NULL) {puts("stu is NULL!");exit(0);}
if(strcmp(s," ")==0) {puts("s is NULL!");exit(0);}
if(nx=='n')  //按姓名查找
 {p=stu;
 while(p!=NULL)
  {
  if(strcmp(p->xingming,s)==0)
   {return p;}  //查找成功,返回p
  p=p->next;
  } 
 }
else if(nx=='x') //按学号查找
 {p=stu;
 while(p!=NULL)
  {
  if(strcmp(p->xuehao,s)==0)
   {return p;}  //查找成功,返回q
  p=p->next;
  }
 }
printf("NO Find!/n");return p;  //返回空,查找失败*/
}
/
int inputf(struct student **stu) //读入文件,返回学生个数
{
int n=0; float temp[4];
struct student *p=NULL,*q=NULL;
FILE *fp;
fp=fopen("D://12.txt","r");
if(fp==NULL) {printf("can't open file 12.txt/n"); exit(0);}
while(!feof(fp))
 {
 p=malloc(sizeof(struct student));
 fscanf(fp,"%s %s",p->xuehao,p->xingming);
 fscanf(fp,"%f %f %f %f/n",&temp[0],&temp[1],&temp[2],&temp[3]);
 p->yuwen=temp[0];p->shuxue=temp[1];p->yingyu=temp[2];p->jisuanji=temp[3];
 if(*stu==NULL) *stu=p;
 else q->next=p;
 q=p;
 n++;
 }
fclose(fp);
q->next=NULL;
return n;
}
/
int outputf(struct student *stu) //写入文件,返回写入学生个数
{
int n=0; float temp[4];
struct student *p=NULL;
FILE *fp;
fp=fopen("D://13.txt","w");
if(fp==NULL) {printf("can't open file 13.txt/n"); exit(0);}
p=stu;
while(p!=NULL)
 {
 fprintf(fp,"%s %-12s",p->xuehao,p->xingming);
 temp[0]=p->yuwen;temp[1]=p->shuxue;temp[2]=p->yingyu;temp[3]=p->jisuanji;
 fprintf(fp,"%f %f %f %f/n",temp[0],temp[1],temp[2],temp[3]);
 p=p->next;
 n++;
 }
fclose(fp);
return n;
}
/
void output(struct student *stu)  //将结果打印出来
{
struct student *p=stu;
if(stu==NULL) exit(0);
printf("xuehao      xingming      yuwen       shuxue      yingyu       jisuanji/n");
while(p!=NULL)
 {
 printf("%-11s  %-11s  %f   %f   %f    %f/n",p->xuehao,p->xingming,p->yuwen,p->shuxue,p->yingyu,p->jisuanji);
 p=p->next;
 }
}
/
void paixu(struct student **stu) //按照总分从小到大排序
{
struct student *p1,*q1,*p,*q,*tep;
if(*stu==NULL) exit(0);
tep=malloc(sizeof(struct student)); //建立一个临时头接点
tep->next=*stu;
q1=tep;q=q1->next;
while(q->next!=NULL)
 {
 p1=q;p=p1->next;
 while(p!=NULL)
  {if(sum(q)>sum(p))
   if(p1==q)
    {q1->next=p;q->next=p->next;p->next=q;}
   else{q1->next=q->next;p1->next=p->next;//先删除p各q两个结点
    p->next=q1->next;q1->next=p;//在q1后插入p
    q->next=p1->next;p1->next=q;//在p1后插入q
    }
   p1=p1->next;p=p1->next;
  }
 q1=q1->next;q=q1->next;
 }
*stu=tep->next;
}
/
void main()
{
char xn=0,s[12],cd;
int n=0 ;
struct student *stud=NULL,*p,q;
do{
n=inputf(&stud);//读文件
printf("/nAll %d students./n",n);//显示文件内容
output(stud);
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/n");//构建菜单。a/A计算平均分,并显示
printf("**s/S:search.**p/P:sequenc.**z/Z:sum.**a/A:average.**q/Q:quit**/n");//s/S查找,并将结果显示;z/Z计算总分,并显示
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/n");//q/Q退出;p/P按总分排序,并将结果写入13.txt
printf("intput command:");
scanf("%c",&cd);  //输入命令
switch(cd){
case 'A':
case 'a':
 p=stud; //输出每个学生的平均成绩
 while(p!=NULL){printf("The average %s is %f./n",p->xingming,average(p));p=p->next;}
 getch();
 continue;
case 'p':
case 'P':
 paixu(&stud);
 printf("Write to 13.txt: %d/n",outputf(stud)); //将排序后的信息写入13.txt,并输出写入数目
 output(stud);
 getch();
 break;
case 'q':
case 'Q':
 exit(0);
case 's':
case 'S':
 printf("inputf search way:n/N ifs for xingming or x/X is for xuehao:/n");//查找
 do{scanf("%c",&xn);}while(xn!='n'&&xn!='x'&&xn!='N'&&xn!='X');
 if(xn<'a') xn+=32; //如果是大写字母就加上32
 printf("intput which one is searched of %c:/n",xn);
 scanf("%s",s);
 if((p=search(stud,s,xn))!=NULL)
  {q.next=NULL;
  strcpy(q.xingming,p->xingming);  strcpy(q.xuehao,p->xuehao);
  q.yuwen=p->yuwen;  q.shuxue=p->shuxue;
  q.yingyu=p->yingyu;  q.jisuanji=p->jisuanji;
  printf("search result:/n");  output(&q);}//查找
 getch();
 break;
case 'z':
case 'Z':
 p=stud; //输出每人各科总成绩
 while(p!=NULL){printf("The sum %s is %f./n",p->xingming,sum(p));p=p->next;}printf("/n");
 getch();
 break;
default:
 ;
 }
}while(1);

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值