学员成绩管理
/**
*程序:S1C语言项目实战:学员成绩管理
*作者:姜微
*日期:2007.10.19
*/
#include<stdio.h>
#include<string.h>
//声明结构体
struct student
{
int no; //学号
char name[15]; //姓名
float score[3]; //三门课程的成绩
double avg; //平均成绩
};
//函数的声明
struct student input();//录入信息函数
void display(struct student stud[],int count);//显示信息函数
void sort(struct student stud[],int count);//成绩排序函数
void sortName(struct student stud[],int count);//按人名排序
void insert(struct student stud[],int count);//插入函数
int del(struct student stud[],int count);//删除函数
void main()
{
struct student stu[50]; //声明一个结构数组变量
int count;//记录录入学员的个数
int flag; //标识是否删除数据
char ch; //判断是否继续录入
ch='y'; //赋初值'y'使循环允许录入
printf("请输入学员信息。");
printf("/n");
count=0; //从零开始计数
//while循环用于循环录入学员,并用count记录录入的个数
do
{
stu[count]=input(); //调用录入信息函数
count++;
printf("/n 是否继续?(y or n)");
scanf(" %c",&ch); //注意%c前空格的使用
} while ((ch=='y') || (ch=='Y')) ;
printf("/n排序前的学员信息如下:");
display(stu,count); //调用显示信息函数
sort(stu,count); //调用排序函数
printf("/n排序后的学员信息如下:");
display(stu,count); //调用显示信息函数
//插入新学员
printf("/n/n是否要插入新学员?(y or n)");
scanf(" %c",&ch);
if(ch=='y' || ch=='Y')
{
insert(stu,count); //调用插入信息函数
count++;
printf("/n插入新学员后的学员信息如下:");
display(stu,count);
}
//删除一条学员信息
printf("/n/n是否要删除某个学员?(y or n)");
scanf(" %c",&ch);
if(ch=='y' || ch=='Y')
{
flag = del(stu,count); //调用删除信息函数
if(flag==1) //如果有学员被删除则count--
{
count--;
}
printf("/n删除后学员的信息如下:");
display(stu,count);
}
}
struct student input() //录入信息函数
{
struct student studn;
int j;
float sum;
printf("/n学号:");
scanf("%d",&studn.no); //录入学号
printf("/n姓名:");
scanf("%s",studn.name);//录入学生姓名,因为name是字符数组,因此不用写&
printf("/n三门成绩:");
sum=0; //sum用于累加成绩总和
printf("/n");
//for循环用于录入三门课程的成绩
for(j=0;j<3;j++)
{
printf("成绩%d: ",j+1);
scanf("%f",&studn.score[j]);
sum+=studn.score[j]; //累加三门课程的成绩
}
studn.avg=sum/3.0; //求三门课程的平均值
return studn; //返回一名学员的录入信息
}
void display(struct student stud[],int count) //显示信息函数
{
int i;
printf("/n学号/t姓名/t/t平均成绩");
printf("/n");
//for循环用于显示结构数组中的所有元素的信息
for(i=0;i<count;i++)
{
printf("%-03d",stud[i].no);
printf("/t%-15s",stud[i].name);
/*添加用for循环显示每门课程的成绩
*/
printf("/t%-10.1f",stud[i].avg);
printf("/n");
}
}
//按平均成绩排序
void sort(struct student stud[],int count) //排序函数
{
/* 冒泡排序法*/
struct student t;//用于中介转换的载体
int i,j;
for(i=0;i<count;i++)
{
for(j=0;j<count-i-1;j++) //比较元素
{
if(stud[j].avg<stud[j+1].avg)
{//此处要特别注意交换的是结构体变量,而不是单一的结构体成员
t=stud[j];
stud[j]=stud[j+1];
stud[j+1]=t;
}
}
}
}
//按人名字母排序
void sortName(struct student stud[],int count)
{
struct student t;//用于中介转换的载体
int i,j;
for( i=0;i<count;i++)
{
for(j=0;j<count-i-1;j++) //比较元素
{
if(strcmp(stud[j].name,stud[j+1].name)>0)
{//此处要特别注意交换的是结构体变量,而不是单一的结构体成员
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循环用于把刚录入的学员的平均成绩与已经排好序的成绩进行比较
for(i=0;i<count;i++)
{
if(stud[i].avg<temp.avg) //当遇到比刚录入小的成绩时跳出循环,并记录下标i
break;
}
//for循环用于给新数据让出位置
for(j=count;j>i;j--)
{
stud[j]=stud[j-1];
}
stud[i]=temp;//把新数据添加到合适的位置
}
int del(struct student stud[],int count) //删除函数
{
int dno,i,j,flag=0;
printf("请输入要删除的学员的学号:");
scanf("%d",&dno);
//for循环用于查找是否有该学号
for(i=0;i<count;i++)
{
if(stud[i].no==dno)//如果有此学号,用下面的for循环覆盖被删除的学员数据
{
for(j=i;j<count-1;j++)
{
stud[j]=stud[j+1];
}
flag=1; //标志删除一名学员
break;
}
}
if(flag==0) //表示没有学员被删除
printf("没有此学号/n");
return flag;
}
下一篇:录入学员成绩,排序,输出.
