您的位置:首页 > 编程语言 > C语言/C++

我的编程生涯的入门语言 - C语言之学员成绩管理

2012-03-10 17:48 274 查看
至今还保存着当初学 C语言 时的代码,现在看来已经有点生疏,毕竟好久没玩了。

往事不堪回首啊!

直接贴代码了:

#include <stdio.h>
struct student
{
int num;
char name[15];
float score[3];
double avr;
};
struct student stu[50];
struct student input()
{
struct student studn;
int i,sum=0;
printf ("请输入学号:");
scanf ("%d",&studn.num);
printf ("请输入姓名:");
scanf("%s",&studn.name);

for(i=0;i<3;i++)
{
printf ("第%d 个成绩:",i+1);
scanf ("%f",&studn.score[i]);
sum+=studn.score[i];
}
studn.avr=sum/3.0;
return studn;
}
void display(struct student stud[],int count)
{
printf ("\n学号\t姓名\t平均成绩\n");
for (int i=0;i<count;i++)
{
printf ("%d",stud[i].num);
printf ("\t%s",stud[i].name);
printf ("\t%7.2f",stud[i].avr);
printf ("\n");
}
}
void sort (struct student stud[],int count)
{
struct student t;
for (int i=0;i<count;i++)
{
for (int j=0;j<count-i-1;j++)
{
if ((stud[j].avr)<(stud[j+1].avr))
{
t=stud[j];
stud[j]=stud[j+1];
stud[j+1]=t;
}
}
}
}

void insert (struct student stud[],int count)
{
int i,j;
struct student temp;
printf ("请输入要插入学员的信息\n");
temp = input();
for(i=0;i<count;i++)
{
if((stud[i].avr)<(temp.avr))
break;
}
for (j=count;j>=i;j--)
{
stud[j+1]=stud[j];
}
stud[i]=temp;
}
void display2(struct student stud[],int count)
{
printf ("\n学号\t姓名\t平均成绩\n");
for (int i=0;i<(count+1);i++)
{
printf ("%d",stud[i].num);
printf ("\t%s",stud[i].name);
printf ("\t%7.2f",stud[i].avr);
printf ("\n");
}
}
void dele (struct student stud[],int count )
{
int i,j,temp;
//struct student temp;
printf ("请输入要删除的学员的学号:\n");
scanf ("%d",&temp);
// temp=input();
for (i=0;i<count;i++)
{
if (stud[i].num==temp)
{
for (j=i;j<count;j++)
{
stud[j]=stud[j+1];
}
}
}
}

void main()
{
int count=0;
char ch='Y';
while((ch=='y')||(ch=='Y'))
{
stu[count]=input();
count++;
printf ("是否要继续(y/n):\n");

fflush(stdin);
scanf ("%c",&ch);
}
printf ("\n排序前的学员成绩序列如下:");
display(stu,count);
sort (stu,count);
printf ("排序后的学员成绩序列如下:");
display (stu,count);
insert(stu,count);
printf ("插入后的学员成绩序列如下:");
display2 (stu,count);
dele(stu,count+1);
display (stu,count);
}


  

谢谢浏览!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: