Spark-寒假-实验2

Posted 。往心。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark-寒假-实验2相关的知识,希望对你有一定的参考价值。

1. 计算级数

  代码:

    

    

import scala.io.StdIn
object jishu

  def main(args:Array[String])
  
    var Sum=0.0
    println("请输入q的值")
    var q:Int=StdIn.readInt()
    var i=1.0
    while(Sum<q)
    
      Sum=Sum+(i+1)/i
      i=i+1
    
    println("-------------------")
    printf("级数的前n项和为:%f \\n",Sum)
  

  测试截图:

  

 

2. 模拟图形绘制

  代码:

    

    

case class Point(var x:Double,var y:Double) extends Drawable

  def shift(deltaX:Double,deltaY:Double)
  
    x+=deltaX;
    y+=deltaY;
  

trait Drawable

  def draw()
  
    println(this.toString)
  

abstract class Shape(var point :Point)

  var r =4.0
  def moveTo(point2:Point):Unit=
    point=point2
  
  def zoom(b:Double):Unit
  def this(cpoint:Point,cr:Double)
  
    this(cpoint:Point)
    this.r=cr
  
  def this(lpoint:Point,lpoint2:Point)
  
    this(lpoint:Point)
  

//圆形
class Circle(point: Point,R:Double) extends Shape(point: Point) with Drawable
  r=R
  //重写,对图形放大,半径放大
  override def zoom(b:Double): Unit = 
    r = r * b
  
  //重写,打印
  override def draw(): Unit =
    var toString="Circle center:("+point.x+","+point.y+")\\t"+"R="+r
    println(toString)
  
  //重写,移动
  override def moveTo(point2: Point): Unit =
    point.x=point2.x
    point.y=point2.y
  

//直线
class Line(point: Point, point1: Point)extends Shape(point: Point) with Drawable
  //重写,对图形放大
  override def zoom(b:Double): Unit = 
    var xmid=(point1.x+point.x)/2   //寻找中点坐标
    var ymid=(point1.y+point.y)/2
    point.x=xmid-(xmid-point.x)*b
    point.y=ymid-(ymid-point.y)*b
    point1.x=xmid+(point1.x-xmid)*b
    point1.y=ymid+(point1.y-ymid)*b
  
  //重写,打印
  override def draw(): Unit =
    var toString="Line:("+point.x+","+point.y+")--"+"("+point1.x+","+point1.y+")"
    println(toString)
  
  //重写,移动
  override def moveTo(point2: Point): Unit =
    point1.x=point1.x+point2.x-point.x
    point1.y=point1.y+point2.y-point.y
    point.x=point2.x
    point.y=point2.y
  

object  moni 
  def main(args: Array[String]) 
    val p=new Point(10,30)
    p.draw;

    val line1 = new Line(Point(0,0),Point(20,20))
    line1.draw
    line1.moveTo(Point(5,5)) //移动到一个新的点
    line1.draw
    line1.zoom(2) //放大两倍
    line1.draw

    val cir= new Circle(Point(10,10),5)
    cir.draw
    cir.moveTo(Point(30,20))
    cir.draw
    cir.zoom(0.5)
    cir.draw
  

 

  测试结果:

    

 

3. 统计学生成绩

  代码:

    

    

object scoreReport
def main(args: Array[String]) 
 // 假设数据文件在当前目录下,自行更改文件名
 val inputFile = scala.io.Source.fromFile("ceshi1.txt")
 //”\\s+“是字符串正则表达式,将每行按空白字符(包括空格/制表符)分开
// 由于可能涉及多次遍历,同 toList 将 Iterator 装为 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
// 统计函数,参数为需要常用统计的行
//用到了外部变量 courseNum,属于闭包函数
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$r
esult._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)
  

  测试截图:

    

    

 

    

 

以上是关于Spark-寒假-实验2的主要内容,如果未能解决你的问题,请参考以下文章

Spark-寒假-实验1

寒假学习报告13

2020年寒假学习进度第十天

寒假第三天

寒假第九天

大三寒假生活13