C语言题~有一个文件存了n个学生的成绩,要求读出成绩,计算平均成绩,统计众位数,最大值和最小值
答案:2 悬赏:0
解决时间 2021-11-26 09:58
- 提问者网友:娇妻失忆
- 2021-11-25 10:52
有一个文件student.dat中存了n个学生的成绩,要求读出成绩,计算平均成绩,统计众位数,最大值和最小值,大概题目是这样的~是有关C语言“文件”那章的内容~求回答啊~谢谢!
最佳答案
- 二级知识专家网友:浪女动了心
- 2021-11-25 12:02
要用链表吗?
//中位数,对长度为n的系列数据,根据数据大小排列得到的位于[(n+1)/2]位置上的数据,n为偶数时取中间2数的平均值
#include<stdio.h>#include<stdlib.h>
typedef struct scores//学生成绩链表
{
float score;
struct scores *next;
}SCORE;
void main( )
{
FILE *fp = NULL;//文件指针 int n = 0;//统计成绩个数
int i;
float temp;
float fenshu;
float sum;
SCORE *head = NULL,*p = NULL, *end =NULL;//链表头指针
if(!(fp=fopen("student.dat","r")))
{
printf("打开文件失败!\n");
exit(0);
}
head = (SCORE *)malloc(sizeof(SCORE)*1);//头节点
end = head;
fscanf(fp,"%f",&fenshu);
while(!feof(fp))//循环读取文件中成绩
{
if(fenshu == -1)
break;
if(n == 0)
{
end->score = fenshu;
end->next = NULL;
}
else
{
p = (SCORE *)malloc(sizeof(SCORE)*1);//重新申请节e69da5e6ba90e79fa5e9819331333332393430点
end->next = p;
end = p;
end ->score = fenshu;
end -> next = NULL;
}
fscanf(fp,"%f",&fenshu);
n++;//成绩计数
}
fclose(fp);
printf("总共有%d个成绩!\n",n);
//链表排序 加寻找众数
int j = 0;
float zshu = 0;
SCORE * np = NULL;
for(i = 0;i < n;i++)
{
p = head;
np = p->next;
for( j = 0;j < n -i-1;j++)
{
if( p->score > np->score )
{
temp = np -> score;
np -> score = p -> score;
p -> score = temp;
}
p = np; np = np ->next;
}
//求出众数
if( i == n/2)
{
if( (n%2 == 1) )//成绩为奇数个
{
zshu = p->score;
}
if( (n%2 == 0) )//成绩为偶数个
{
zshu = (p->score + np->score)/2;
}
}
}
p = head;
printf("成绩如下: \n");
do
{
printf("%f\n",p->score);
sum+=p->score;
p = p ->next;
}while( p != NULL);
printf("中位数是:%.2f\n",zshu);
printf("最小数是:%.2f,最大数是:%.2f,平均成绩是:%.2f!\n",head->score,end->score,sum/n);
}
//中位数,对长度为n的系列数据,根据数据大小排列得到的位于[(n+1)/2]位置上的数据,n为偶数时取中间2数的平均值
#include<stdio.h>#include<stdlib.h>
typedef struct scores//学生成绩链表
{
float score;
struct scores *next;
}SCORE;
void main( )
{
FILE *fp = NULL;//文件指针 int n = 0;//统计成绩个数
int i;
float temp;
float fenshu;
float sum;
SCORE *head = NULL,*p = NULL, *end =NULL;//链表头指针
if(!(fp=fopen("student.dat","r")))
{
printf("打开文件失败!\n");
exit(0);
}
head = (SCORE *)malloc(sizeof(SCORE)*1);//头节点
end = head;
fscanf(fp,"%f",&fenshu);
while(!feof(fp))//循环读取文件中成绩
{
if(fenshu == -1)
break;
if(n == 0)
{
end->score = fenshu;
end->next = NULL;
}
else
{
p = (SCORE *)malloc(sizeof(SCORE)*1);//重新申请节e69da5e6ba90e79fa5e9819331333332393430点
end->next = p;
end = p;
end ->score = fenshu;
end -> next = NULL;
}
fscanf(fp,"%f",&fenshu);
n++;//成绩计数
}
fclose(fp);
printf("总共有%d个成绩!\n",n);
//链表排序 加寻找众数
int j = 0;
float zshu = 0;
SCORE * np = NULL;
for(i = 0;i < n;i++)
{
p = head;
np = p->next;
for( j = 0;j < n -i-1;j++)
{
if( p->score > np->score )
{
temp = np -> score;
np -> score = p -> score;
p -> score = temp;
}
p = np; np = np ->next;
}
//求出众数
if( i == n/2)
{
if( (n%2 == 1) )//成绩为奇数个
{
zshu = p->score;
}
if( (n%2 == 0) )//成绩为偶数个
{
zshu = (p->score + np->score)/2;
}
}
}
p = head;
printf("成绩如下: \n");
do
{
printf("%f\n",p->score);
sum+=p->score;
p = p ->next;
}while( p != NULL);
printf("中位数是:%.2f\n",zshu);
printf("最小数是:%.2f,最大数是:%.2f,平均成绩是:%.2f!\n",head->score,end->score,sum/n);
}
全部回答
- 1楼网友:随心随缘不随便
- 2021-11-25 13:05
文件里数据的格式是什么?
再看看别人怎么说的。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯