其实你能找到的,昨天刚好有人问过。
另外还有个大大的代码 我去找给你
程序代码:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void input(struct student * , int, int );
void output(struct student *, int );
void sort(struct student *, int );
void avg(struct student *,int , int);
void rank(struct student *,int,int *);
struct student
{
float avg;
float score[10];
char name[10];
int rank;
};
int main (void)
{
struct student * pArr;
int len,subject;
int rankall=0;
printf("Please enter the number of students:");
scanf("%d", &len );
printf("Please enter the number of subject:");
scanf("%d", &subject);
pArr = (struct student *) malloc(len * sizeof(struct student));
input(pArr,len,subject);
avg(pArr,len,subject);
sort(pArr,len);
rank(pArr,len,&rankall);
output(pArr,len);
free(pArr);
system("pause");
return 0;
}
void input (struct student * stutemp , int len, int sub)
{
int i,j;
for (i=0; i<len ; ++i)
{
printf("please input the student name:");
scanf("%s",stutemp[i].name);
for (j=0; j<sub; ++j)
{
printf("please input the student score[s:%d]:", j);
scanf("%f", &stutemp[i].score[j]);
}
}
}
void output (struct student * stutemp , int len)
{
int i;
for (i=0; i<len; ++i)
{
printf("-------------------------\n");
printf("rank:%d\n", stutemp[i].rank);
printf("name:%s\n", stutemp[i].name);
printf("avg:%1f\n", stutemp[i].avg);
}
}
void sort (struct student * stutemp , int len)
{
struct student temp;
int i,k;
int j;
for (i=0; i<len-1; ++i)
{
for (k=0; k<len-1-i; ++k)
{
if (stutemp[k].avg > stutemp[k+1].avg && stutemp[k].avg != stutemp[k+1].avg)
{
temp = stutemp[k];
stutemp[k] = stutemp[k+1];
stutemp[k+1] = temp;
}
else
{
for (j=0; j<=10; j++)
if (stutemp[k].name[j] > stutemp[k+1].name[j] && stutemp[k].name[j] != stutemp[k+1].name[j])
{
temp = stutemp[k];
stutemp[k] = stutemp[k+1];
stutemp[k+1] = temp;
}
else ;
}
}
}
}
void avg(struct student * temp, int len , int subj)
{
int i,j;
float sum = 0;
for (i=0; i<len; ++i)
{
for (j=0; j<subj; ++j)
{
sum = sum + temp[i].score[j];
}
temp[i].avg = sum / subj;
sum = 0;
}
}
void rank(struct student * stutemp ,int len , int * rankall)
{
int i;
for (i=len-1; i>=0; --i)
{
if (stutemp[i].avg == stutemp[i-1].avg)
{
*rankall = *rankall +1;
stutemp[i].rank = *rankall;
stutemp[i-1].rank = *rankall;
printf("i = %d, len = %d , rank = %d , [%d].avg = [%d].avg True\n",i,len,*rankall,i,i-1);
}
else if ( -1 != i-1 ) //避免出现比较 [-1]的情况
{
*rankall = *rankall +1;
stutemp[i-1].rank = *rankall;
printf("i = %d, len = %d , rank = %d , [%d].avg = [%d].avg False\n",i,len,*rankall,i,i-1);
}
}
return ;
}
[
本帖最后由 随风飘荡 于 2011-10-30 10:25 编辑 ]