粗糙的写了个简单的代码
输入文件名input.txt:其内容如下
6
60 80 50 40 90 70
40
结果输出文件名output.txt其内容如下:
5 排名第五。
#include<stdio.h>
#include <malloc.h>
typedef struct student
{
int totalnum;
float *elem;
float findscore;
}stu;
void main()
{
int i,j,n;
stu S;
FILE *fp;
if(!(fp =fopen("input.txt","r")))
{
printf("文件打开失败!请确定是否有input.txt文件。");
return ;
}
fscanf(fp,"%d",&n);
S.totalnum = n;
S.elem = (float*)malloc(n*sizeof(float));
for(i=0;i<n;i++)//读n个学生成绩
fscanf(fp,"%f",&S.elem[i]);
fscanf(fp,"%f",&S.findscore);
fclose(fp);
fp = fopen("output.txt","w+");
for(i=0;i<n-1;i++)
{
float temp;
for(j=i+1;j<n;j++)
{
if(S.elem[j]>S.elem[i])
{
temp = S.elem[j];
S.elem[j] = S.elem[i];
S.elem[i] = temp;
}
}
}
int ranknum;
for(i=0;i<n;i++)
{
if(S.elem[i] == S.findscore)
{
ranknum = i;break;
}
}
fprintf(fp,"%d",ranknum);
fclose(fp);
}
温馨提示:内容为网友见解,仅供参考