首页 >> 大全

《C语言及程序设计》实践参考——成绩统计

2023-10-18 大全 30 作者:考证青年

返回:贺老师课程教学链接项目要求

【项目3-成绩统计

文件.dat(这个文件中的数据量,超出了你之前所有的体验)中已经有了学生的英语考试成绩数据。

(1)请编程从.dat中读取数据,求出这次考试的平均成绩,并统计输出优秀人数和不及格人数。请在下面程序基础上填空完成:

#include 
#include 
int main()
{int score; //读入的成绩int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数double sum=0,ave; //sum: 成绩和,ave: 平均分//以输入的方式(ios::in)打开文件FILE ____(1)____;fp=fopen(____(2)____);if(fp==NULL){printf("open error!\n");exit(1);}while(fscanf(____(3)____)!=EOF)   //当读取成功……{count++;sum+=score;if(____(4)____)excelent++;else if(score<60)____(5)____;}____(6)____;  //下面输出结果ave=sum/count;printf("总人数为:%d\n", count);printf("平均成绩为:%.2f\n", ave);printf("优秀人数:%d\n", excelent);printf("不及格人数:%d\n", fail);return 0;
}

统计学生成绩c语言流程图_统计分析编程语言_

[参考解答]

#include 
#include 
int main()
{int score; //读入的成绩int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数double sum=0,ave; //sum: 成绩和,ave: 平均分//以输入的方式(ios::in)打开文件FILE *fp;fp=fopen("english.dat","r");if(fp==NULL){printf("open error!\n");exit(1);}while(fscanf(fp, "%d", &score)!=EOF)   //当读取成功……{count++;sum+=score;if(score>=90)excelent++;else if(score<60)fail++;}fclose(fp);          //读入完毕要关闭文件//下面输出结果ave=sum/count;printf("总人数为:%d\n", count);printf("平均成绩为:%.2f\n", ave);printf("优秀人数:%d\n", excelent);printf("不及格人数:%d\n", fail);return 0;
}

(2)扩充上面的程序,要求将统计结果保存到数据文件.dat中(提示:要用写入文件了)

[参考解答]

_统计分析编程语言_统计学生成绩c语言流程图

#include 
#include 
int main()
{int score; //读入的成绩int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数double sum=0,ave; //sum: 成绩和,ave: 平均分//以输入的方式(ios::in)打开文件FILE *fp;fp=fopen("english.dat","r");if(fp==NULL){printf("source file open error!\n");exit(1);}while(fscanf(fp, "%d", &score)!=EOF)   //当读取成功……{count++;sum+=score;if(score>=90)excelent++;else if(score<60)fail++;}fclose(fp);          //读入完毕要关闭文件//下面输出结果ave=sum/count;printf("总人数为:%d\n", count);printf("平均成绩为:%.2f\n", ave);printf("优秀人数:%d\n", excelent);printf("不及格人数:%d\n", fail);fp=fopen("statictic.dat","w");if(fp==NULL){printf("output file open error!\n");exit(1);}fprintf(fp, "总人数为:%d\n", count);fprintf(fp, "平均成绩为:%.2f\n", ave);fprintf(fp, "优秀人数:%d\n", excelent);fprintf(fp, "不及格人数:%d\n", fail);fclose(fp);          //读入完毕要关闭文件//下面打开文件查看结果return 0;
}

(3)(用柱状图输出)编程序,求出这次考试的平均成绩,并统计各分数段的人数(优秀:≥90,良好:≥80,中等:≥70,及格:≥60,不及格:

统计学生成绩c语言流程图__统计分析编程语言

[参考解答]

#include 
#include 
int main()
{int score; //读入的成绩int rate; //根据成绩确定的等级int excelent=0, good=0, medium=0, passing=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数double sum=0,ave; //sum: 成绩和,ave: 平均分//以输入的方式(ios::in)打开文件FILE *fp;fp=fopen("english.dat","r");if(fp==NULL){printf("source file open error!\n");exit(1);}while(fscanf(fp, "%d", &score)!=EOF)   //当读取成功……{count++;sum+=score;rate=score/10;  //rate用于分出“档次”switch(rate){case 10:case 9:excelent++;break;case 8:good++;break;case 7:medium++;break;case 6:passing++;break;default:fail++;break;}}fclose(fp);          //读入完毕要关闭文件//下面输出结果,不同的数字对应的#数目不同ave=sum/count;int max; //将存储这几个分数段中的最多人数max=excelent;if(max

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了