第1个回答 推荐于2016-03-05
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//数据手工录入比较合理,这里为了方便,直接写在程序里了
struct student {
char *no;
int grade1;
int grade2;
double grade;
};
#define TOTAL_STUDENT 8
int cn[TOTAL_STUDENT] = {1,2,3,4,5,6,7,8};
int ps[TOTAL_STUDENT] = {92,95,98,96,93,91,92,96};
int fs[TOTAL_STUDENT] = {94,90,95,88,92,94,98,90};
main () {
struct student st[TOTAL_STUDENT],_st; //共8个数据
int i,j,flag;
double ave_s=0.0;
_st.no = (char*)malloc(2*sizeof(char));
//本for循环是为了读取数据
for (i=0;i<TOTAL_STUDENT;i++) {
st[i].no = (char*) malloc(2*sizeof(char));
sprintf(st[i].no,"%02d", cn[i]);
st[i].grade1 = ps[i];
st[i].grade2 = fs[i];
st[i].grade = (double)(st[i].grade1 + st[i].grade2)/2.0;
ave_s += st[i].grade;
}
ave_s /=TOTAL_STUDENT;
//输出结果
printf("考号\t笔试\t面试\t总分\n");
for (i=0;i<TOTAL_STUDENT;i++) {
printf("%s\t%d\t%d\t%4.2f\n",st[i].no, st[i].grade1,st[i].grade2, st[i].grade);
}
//为省事,使用冒泡法排序
for (i=0;i<TOTAL_STUDENT-1;i++) {
flag = 0;
for (j=i;j<TOTAL_STUDENT;j++) {
if (st[i].grade<st[j].grade) {
strncpy(_st.no, st[i].no,2);
_st.grade1 = st[i].grade1;
_st.grade2 = st[i].grade2;
_st.grade = st[i].grade;
strncpy(st[i].no, st[j].no,2);
st[i].grade1 = st[j].grade1;
st[i].grade2 = st[j].grade2;
st[i].grade = st[j].grade;
strncpy(st[j].no, _st.no,2);
st[j].grade1 = _st.grade1;
st[j].grade2 = _st.grade2;
st[j].grade = _st.grade;
flag = 1;
}
}
if (flag==0) break;
}
//输出结果:
printf("\n高于平均分%4.2f的考生:\n",ave_s);
for (i=0;i<TOTAL_STUDENT;i++) {
if (st[i].grade > ave_s)
printf("%s\t%d\t%d\t%4.2f\n",st[i].no, st[i].grade1,st[i].grade2, st[i].grade);
}
free(_st.no);
for (i=0;i<TOTAL_STUDENT;i++)
free(st[i].no);
return 0;
}本回答被提问者采纳