#include
#include
#define ARR_SIZE 30
int Menu(void);
int ReadScore(long num[], float score[]);
float GetAver(float score[], int n);
float GetSum(float score[], int n);
void Dsortbyscore(float score[],long num[],int n,int (*compare)(int a,int b));
void Asortbynum(float score[],long num[], int n,int (*compare)(int a,int b));
void Print(long num[],float score[],int n);
int Search(long num[], float score[], int n, long x);
void GetDetail(float score[], int n);
void swap1(long *x,long *y);
void swap2(float *x,float *y);
int ascending(int a,int b);
int descending(int a,int b);
int main() {
int item,n,flag;
float score[ARR_SIZE],Aver,Sum;
long num[ARR_SIZE],xnum;
int order;
while(1) {
item=Menu();
switch(item) {
case 1:
n = ReadScore(num, score);
printf("学生总数为:%d\n", n);
Print(num,score,n);
break;
case 2:
Sum = GetSum(score, n);
printf("总成绩= %.2f\n", Sum);
Aver = GetAver(score, n);
printf("平均值=%.2f\n", Aver);
break;
case 3:
printf("请选择排序方式,值为1表示升序排序,值为2表示降序排序:");
scanf("%d",&order);
if(order==1) {
Dsortbyscore(score,num,n,ascending);
} else {
Dsortbyscore(score,num,n,descending);
}
printf("排序后的结果为 :\n");
Print(num,score,n);
break;
case 4:
printf("请选择排序方式,值为1表示升序排序,值为2表示降序排序:");
scanf("%d",&order);
if(order==1) {
Asortbynum(score,num,n,ascending);
} else {
Asortbynum(score,num,n,descending);
}
printf("排序后的结果为 ::\n");
Print(num,score,n);
break;
case 5:
printf("请输入要查找的学号:");
scanf("%ld",&xnum);
flag=Search(num, score, n, xnum);
if(flag<0) printf("Not found!\n");
break;
case 6:
GetDetail(score, n);
break;
case 7:
Asortbynum(score,num,n,descending);
Print(num,score,n);
Sum = GetSum(score, n);
printf("总成绩= %.2f\n", Sum);
Aver = GetAver(score, n);
printf("平均值=%.2f\n", Aver);
break;
case 0:
printf("退出程序! \n");
exit(0);
default:
printf("Ierror! \n");
}
}
return 0;
}
int Menu(void) {
int item;
printf("\nManagement for student scores \n");
printf("1. Input record \n");
printf("2. Caculate total and average of course \n");
printf("3. Sort in descending order by score \n");
printf("4. Sort in ascending order by number \n");
printf("5. Search by number \n");
printf("6. Statistic analysis \n");
printf("7. List record \n");
printf("0. Exit \n");
printf("Please enter your choice(0-7): ");
scanf(" %d", &item);
return(item);
}
int ReadScore(long num[], float score[]) {
int i=-1;
printf("请输入学生的学号和成绩(成绩为负值时停止):\n");
do {
i++;
scanf("%ld",&num[i]);
scanf("%f",&score[i]);
} while(score[i]>=0);
return i;
}
float GetSum(float score[], int n) {
int i=0;
float sum=0;
for(i=0; i<n; i++) {
sum+=score[i];
}
return sum;
}
float GetAver(float score[], int n) {
float average=0.0;
int i=0,sum=0;
for(i=0; i<n; i++) {
sum+=score[i];
}
average=(float)sum/n;
return average;
}
void Dsortbyscore(float score[],long num[],int n,int (*compare)(int a,int b)) {
int i=0,j=0,k=0;
for(i=0; i<n-1; i++) {
k=i;
for(j=i+1; j<n; j++) {
if((*compare)(score[j],score[k]))
k=j;
}
if(k!=i) {
swap2(&score[k],&score[i]);
swap1(&num[k],&num[i]);
}
}
}
void Asortbynum(float score[],long num[], int n,int (*compare)(int a,int b)) {
int i=0,j=0,k=0;
for(i=0; i<n-1; i++) {
k=i;
for(j=i+1; j<n; j++) {
if((*compare)(num[j],num[k]))
k=j;
}
if(k!=i) {
swap2(&score[k],&score[i]);
swap1(&num[k],&num[i]);
}
}
}
int Search(long num[], float score[], int n, long x) {
int i;
for(i=0; i<n; i++) {
if(x==num[i]) {
printf("该学生的学号是%d\n",num[i]);
printf("该学生的成绩是%f\n",score[i]);
printf("该学生的排名是%d\n",(i+1));
return i;
break;
}
}
if(i>n) {
return (-1);
}
}
void GetDetail(float score[], int n) {
int i=0,a=0,b=0,c=0,d=0,e=0;
for(i=0; i<n; i++) {
if(score[i]<=100&&score[i]>=90) {
a++;
}
if(score[i]<=89&&score[i]>=80) {
b++;
}
if(score[i]<=79&&score[i]>=70) {
c++;
}
if(score[i]<=69&&score[i]>=60) {
d++;
}
if(score[i]<=60&&score[i]>=0) {
e++;
}
}
printf("优秀人数:%d,占总人数:%.2f%%\n",a,(float)(a*100)/n);
printf("良好人数:%d,占总人数:%.2f%%\n",b,(float)(b*100)/n);
printf("中等人数:%d,占总人数:%.2f%%\n",c,(float)(c*100)/n);
printf("及格人数:%d,占总人数:%.2f%%\n",d,(float)(d*100)/n);
printf("不及格人数:%d,占总人数:%.2f%%\n",e,(float)(e*100)/n);
}
void Print(long num[], float score[], int n) {
int i=0;
for(i=0; i<n; i++) {
printf("学生的学号是%d\n 学生的成绩是%.2f:\n",num[i],score[i]);
}
}
void swap1(long *x,long *y) {
int pTemp;
pTemp=*x;
*x=*y;
*y=pTemp;
}
void swap2(float *x,float *y) {
float pTemp;
pTemp=*x;
*x=*y;
*y=pTemp;
}
int ascending(int a,int b) {
return a<b;
}
int descending(int a,int b) {
return a>b;
}