Scala 实验 2 3
最编程
2024-05-06 11:15:10
...
object scoreReport
{
def main(args:Array[String])
{
//假设数据在当前目录下
val inputFile=scala.io.Source.fromFile("/home/localhost/IdeaProjects/test/src/test1")
//"\\s"是字符串正则表达式,将每行按空白字符(包括空格/制表符)分开
//由于可能涉及多次遍历,通过toList将Itertor装为List
//originalData的类型为List[Array[String]]
val originalData=inputFile.getLines.map{_.split{"\\s+"}}.toList
val courseNames=originalData.head.drop(2) //获取第一行中的课程名
val allStudents=originalData.tail //去除第一行剩下的数据
val courseNum=courseNames.length
def statistc(lines:List[Array[String]])=
{
//for推导式,对每门课程生成一个三元组,分别表示总分,最低分和最高分
(for(i<- 2 to courseNum+1) yield
{
//取出需要统计的列
val temp =lines map
{
elem=>elem(i).toDouble
}
(temp.sum,temp.min,temp.max)
})map{case(total,min,max)=>(total/lines.length,min,max)} //最后一个map对for的结果进行修改,将总分转为平均分
}
//输出结果函数
def printResult(theresult:Seq[(Double,Double,Double)])
{
//遍历前调用zip方法将课程名容器和结果容器合并,合并结果为二元组容器
(courseNames zip theresult)foreach
{
case(course,result)=>
println(f"${course+":"}%-10s${result._1}%5.2f${result._2}%8.2f${result._3}%8.2f")
}
}
//分别调用两个函数统计全体学生并输出结果
val allResult=statistc(allStudents)
println("course average min max")
printResult(allResult)
//按性别划分为两个容器
val (maleLines,femaleLines)=allStudents partition
{
_(1)=="male"
}
//分别调用两个函数统计男学生并输出结果
val maleResult=statistc(maleLines)
println("course average min max")
printResult(maleResult)
//分别调用两个函数统计女学生并输出结果
val femaleResult=statistc(femaleLines)
println("course average min max")
printResult(femaleResult)
}
}
推荐阅读
-
Scala 实验 2 3
-
jsPDF + html2canvas + Vue3 + ts 项目,逐页导出当前页面为 PDF,A 页导出 B 页内容为 PDF,隐藏导出按钮等多余元素。
-
用 python3 抓取网页,用 aria2 下载电影,Jellyfin 自动更新最新电影
-
Python 第 3 课 - 数据类型 2
-
一加 12/11/10/Ace2/Ace3手机锁 返回锁 BL 无限重启 黑屏 9008 模式 砖块救援
-
Electron-Vite2-MacUI 桌面管理框架 | electron13+vue3.x 仿 Mac 桌面 UI
-
基于立体视觉的 3D 模型重建系统设计-2 系统设计
-
部署 FIM 2010 R2-3 安装 FIM 2010 R2 服务和门户网站
-
实验3
-
修改 word 文档创建者的方法有哪些?如何修改文档的作者 这两种方法你必须知道 - 软件下载地址:https://www.huibang168.com/static/upload/2Lph6q3Q/file/81/HuibangMetadata_3.7.0.0.exe 除了在 Word 中修改作者信息外,您还可以通过修改文档的元数据来更改作者。元数据存储在文档内部,用于描述文档数据的属性。 具体步骤如下