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

我的一个 C语言代码

2006-11-01 12:15 429 查看
#include<stdio.h>
struct student
{
int num;
char name[15];
int score[3];
double avr;
};
struct student stu[50];
struct student input();
void display(struct student stud[],int count);
void sort(struct student stud[],int count);
void insert(struct student stud[],int count);
void del(struct student stud[],int count);
void main()
{
int count=0;
char ch='y';
while((ch=='y')||(ch=='Y'))
{
stu[count]=input();
count++;
fflush(stdin);
printf("/n是否继续?(y or n)");
scanf("%c",&ch);
}
display(stu,count);
}
struct student input()
{
struct student studn;
int sum,j;
printf("/n学号:");
scanf("%d",&studn.num);
printf("/n姓名:");
scanf("%s",&studn.name);
printf("/n三门成绩:");
sum=0;
printf("/n");
for(j=0;j<3;j++)
{
printf("成绩%d:",j+1);
scanf("%d",&studn.score[j]);

sum+=studn.score[j];
}
studn.avr=sum/3.0;
return studn;
}
void display(struct student stud[],int count)
{
printf("/n学号/t姓名/t/t平均成绩/n");
for(int i=0;i<count;i++)
{
printf("%-0.3d",stud[i].num);
printf("/t%-15s",stud[i].name);
printf("/t%-10.1f",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[i];
stud[i]=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[i];
}
stud[i]=temp;
}
void del(struct student stud[],int count)
{
int dno,i;
struct student temp;
printf("/n请输入要删除学员的学号:");
scanf("%d",&dno);
for(i=0;i<count;i++)
{
if(stud[i].num==dno);
break;
}
for(int j=i;j<count-1;j++)
{
stud[j+1]=stud[i];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐