大三寒假生活3

Posted quyangzhangsiyuan

tags:

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

今天完成了spark实验二,花费的时间太多,没由大到预期的计划。另外打包过程中下载时候遇到链接打不开404是不影响下载的,完成打包的学习。 下边是今天的实验。

2, 模拟图形绘制

对于一个图形绘制程序,用下面的层次对各种实体进行抽象。定义一个 Drawable 的特质,其包括一个 draw 方法,默认实现为输出对象的字符串表示。定义一个 Point 类表示点,其混入了 Drawable 特质,并包含一个 shift 方法,用于移动点。所有图形实体的抽象类为Shape,其构造函数包括一个 Point 类型,表示图形的具体位置(具体意义对不同的具体图形不一样)。Shape 类有一个具体方法 moveTo 和一个抽象方法 zoom,其中 moveTo 将图形从当前位置移动到新的位置, 各种具体图形的 moveTo 可能会有不一样的地方。zoom 方法实现对图形的放缩,接受一个浮点型的放缩倍数参数,不同具体图形放缩实现不一样。继承Shape 类的具体图形类型包括直线类 Line 和圆类 Circle。Line 类的第一个参数表示其位置,第二个参数表示另一个端点,Line 放缩的时候,其中点位置不变,长度按倍数放缩(注意,缩放时,其两个端点信息也改变了),另外,Line 的 move 行为影响了另一个端点,需要对move 方法进行重载。Circle 类第一个参数表示其圆心,也是其位置,另一个参数表示其半径,Circle 缩放的时候,位置参数不变,半径按倍数缩放。另外直线类 Line 和圆类 Circle都混入了 Drawable 特质,要求对 draw 进行重载实现,其中类 Line 的 draw 输出的信息样式为“Line:第一个端点的坐标--第二个端点的坐标)”,类 Circle 的 draw 输出的信息样式为“Circle center:圆心坐标,R=半径”。如下的代码已经给出了 Drawable 和 Point 的定义,同时也给出了程序入口 main 函数的实现,请完成 Shape 类、Line 类和 Circle 类的定义。

 

技术图片
case class Point(var x:Double,var y:Double) extends Drawable{
    var Px:Double=x
    var Py:Double=y
     def shift(deltaX:Double,deltaY:Double){Px+=deltaX;Py+=deltaY}
}
trait Drawable{
    def draw(){println(this.toString)}
}

abstract class Shape extends Drawable{
    def moveTo(point:Point)
    def draw()
    def zoom(position:Double)
}

class Line(point1:Point,point2:Point)extends Shape{
    override def moveTo(point:Point){
        point1.shift(point.Px,point.Py)
           point2.shift(point.Px,point.Py)
    }
    
    override def zoom(position:Double){
        var lengthx = point2.Px-point1.Px
            var lengthy = point2.Py-point1.Py
            if(lengthx<0){
                  lengthx = -lengthx;
            }
            if(lengthy<0){
                  lengthy = -lengthy;
            }
            var newx = (lengthx/2)*(position-1);
            point1.Px = point1.Px-newx;
            point2.Px = point2.Px+newx;

        var newy =(lengthy/2)*(position-1);
        point1.Py = point1.Py-newy;
        point2.Py = point2.Py+newy;
    }
    
    override def draw(){
        println("Line:(" + point1.Px + "," + point1.Py + ")--(" + point2.Px + "," + point2.Py + ")")
    }

}

class Circle(point: Point, r: Double) extends Shape {
  var R = r

  override def moveTo(point: Point){
    this.point.Px = point.Px
    this.point.Py = point.Py
  }

  override def zoom(step: Double){
    R = R * step
  }

  override def draw(){
    println("Circle center:(" + point.Px + "," + point.Py + "),R=" + R);
  }
}

object MyDraw{
    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
     } 
}
View Code

 

 技术图片

3, 统计学生成绩学生的成绩清单格式如下所示,第一行为表头,各字段意思分别为学号、性别、课程名1、课程名 2 等,后面每一行代表一个学生的信息,各字段之间用空白符隔开

Id gender Math English Physics
301610 male 80 64 78
301611 female 65 87 58
...
给定任何一个如上格式的清单(不同清单里课程数量可能不一样),要求尽可能采用函数式编程,统计出各门课程的平均成绩,最低成绩,和最高成绩;另外还需按男女同学分开,
分别统计各门课程的平均成绩,最低成绩,和最高成绩。测试样例 1 如下:
Id gender Math English Physics
301610 male 80 64 78
301611 female 65 87 58
301612 female 44 71 77
301613 female 66 71 91
301614 female 70 71 100
301615 male 72 77 72
301616 female 73 81 75
301617 female 69 77 75
301618 male 73 61 65
301619 male 74 69 68
301620 male 76 62 76
301621 male 73 69 91
301622 male 55 69 61
301623 male 50 58 75
301624 female 63 83 93
301625 male 72 54 100
301626 male 76 66 73
301627 male 82 87 79
301628 female 62 80 54
301629 male 89 77 72
样例 1 的统计结果输出为:
course average min max
Math: 69.20 44.00 89.00
English: 71.70 54.00 87.00
Physics: 76.65 54.00 100.00
course average min max (males)
Math: 72.67 50.00 89.00
English: 67.75 54.00 87.00
Physics: 75.83 61.00 100.00
course average min max (females)
Math: 64.00 44.00 73.00
English: 77.63 71.00 87.00
Physics: 77.88 54.00 100.00

 

 

测试样例 2
Id
gender Math English Physics Science
301610 male 72 39 74 93
301611 male 75 85 93 26
301612 female 85 79 91 57
301613 female 63 89 61 62
301614 male 72 63 58 64
301615 male 99 82 70 31
301616 female 100 81 63 72
301617 male 74 100 81 59
301618 female 68 72 63 100
301619 male 63 39 59 87
301620 female 84 88 48 48
301621 male 71 88 92 46
301622 male 82 49 66 78
301623 male 63 80 83 88
301624 female 86 80 56 69
301625 male 76 69 86 49
301626 male 91 59 93 51
301627 female 92 76 79 100
301628 male 79 89 78 57
301629 male 85 74 78 80
样例 2 的统计结果为:
course average min max
Math: 79.00 63.00 100.00
English: 74.05 39.00 100.00
Physics: 73.60 48.00 93.00
Science: 65.85 26.00 100.00
course average min max
Math: 77.08 63.00 99.00
English: 70.46 39.00 100.00
Physics: 77.77 58.00 93.00
Science: 62.23 26.00 93.00
course average min max
Math: 82.57 63.00 100.00
English: 80.71 72.00 89.00
Physics: 65.86 48.00 91.00
Science: 72.57 48.00 100.00 

这个我写了两种方法,第一种容易理解比较笨重。第二种在网上借鉴的别人的,方便快捷,可以很轻松的改变录入的样本,但还有些东西没搞懂,接下来会搞懂。

第一种方法:

object Score{
    def main(args:Array[String]){
        var students:ArrayBuffer[Student] = new ArrayBuffer[Student]()
        students.append(new Student(301610, "male", 80, 64, 78))
        students.append(new Student(301611, "female", 65, 87, 58))
        students.append(new Student(301612, "female", 44, 71, 77))
        students.append(new Student(301613, "female", 66, 71, 91))
        students.append(new Student(301614, "female", 70, 71, 100))
        students.append(new Student(301615, "male", 72, 77, 72))
        students.append(new Student(301616, "female", 73, 81, 75))
        students.append(new Student(301617, "female", 69, 77, 75))
        students.append(new Student(301618, "male", 73, 61, 65))
        students.append(new Student(301619, "male", 74, 69, 68))
        students.append(new Student(301620, "male", 76, 62, 76))
        students.append(new Student(301621, "male", 73, 69, 91))
        students.append(new Student(301622, "male", 55, 69, 61))
        students.append(new Student(301623, "male", 50, 58, 75))
        students.append(new Student(301624, "female", 63, 83, 93))
        students.append(new Student(301625, "male", 72, 54, 100))
        students.append(new Student(301626, "male", 76, 66, 73))
        students.append(new Student(301627, "male", 82, 87, 79))
        students.append(new Student(301628, "female", 62, 80, 54))
        students.append(new Student(301629, "male", 89, 77, 72))
        println(students.length)
        println(students(1).toString)

        getData(students)
        getDataMale(students)
        getDataFeMale(students)
    }

    def getData(value: ArrayBuffer[Student]){
        var Mathaverage:Double=0;
        var MathSum:Double = 0;
        var MathMin:Double = value(0).Math;
        var MathMax:Double = 0;
    
    var Englishaverage:Double=0;
        var EnglishSum:Double = 0;
        var EnglishMin:Double = value(0).English;
        var EnglishMax:Double = 0;

        var Physicsaverage:Double=0;
        var PhysicsSum:Double = 0;
        var PhysicsMin:Double = value(0).Physics;
        var PhysicsMax:Double = 0;

        var number = value.length;
        for (i <- 0 until value.length){
            MathSum = value(i).Math+MathSum
              EnglishSum = value(i).English+EnglishSum
              PhysicsSum = value(i).Physics+PhysicsSum

              if(value(i).Math>MathMax){
            MathMax=value(i).Math;
              }
            if(value(i).English>EnglishMax){
            EnglishMax=value(i).English;
            }
            if(value(i).Physics>PhysicsMax){
            PhysicsMax=value(i).Physics;
            }
            if(value(i).Math<MathMin){
            MathMin=value(i).Math;
            }
              if(value(i).English<EnglishMin){
             EnglishMin=value(i).English;
            }
            if(value(i).Physics<PhysicsMin){
            PhysicsMin=value(i).Physics;
            }
        }

        Mathaverage=MathSum/number
        Englishaverage=EnglishSum/number
        Physicsaverage=PhysicsSum/number
        println("course	 average	 min	 max 	")
        println("Math:	   "+Mathaverage+"		"+MathMin+"	 "+MathMax)
        println("English:	"+Englishaverage+"	"+EnglishMin+"	 "+EnglishMax)
        println("Physics:	"+Physicsaverage+"	"+PhysicsMin+"	 "+PhysicsMax)
  }

  def getDataMale(value: ArrayBuffer[Student]){
        var Mathaverage: Double = 0;
        var MathSum: Double = 0;
        var MathMin: Double = value(0).Math;
        var MathMax: Double = 0;

        var Englishaverage: Double = 0;
        var EnglishSum: Double = 0;
       var EnglishMin: Double = value(0).English;
        var EnglishMax: Double = 0;

        var Physicsaverage: Double = 0;
        var PhysicsSum: Double = 0;
        var PhysicsMin: Double = value(0).Physics;
        var PhysicsMax: Double = 0;

        var number = 0;

        for (i <- 0 until value.length) {
              if (value(i).gender.equals("male")) {
            number = number + 1
 
            MathSum = value(i).Math + MathSum
            EnglishSum = value(i).English + EnglishSum
            PhysicsSum = value(i).Physics + PhysicsSum

            if (value(i).Math > MathMax) {
                  MathMax = value(i).Math;
            }
            if (value(i).English > EnglishMax) {
                    EnglishMax = value(i).English;
            }
            if (value(i).Physics > PhysicsMax) {
                  PhysicsMax = value(i).Physics;
            }
            if (value(i).Math < MathMin) {
                  MathMin = value(i).Math;
            }
            if (value(i).English < EnglishMin) {
                  EnglishMin = value(i).English;
            }
            if (value(i).Physics < PhysicsMin) {
                  PhysicsMin = value(i).Physics;
            }
              }

       }

        Mathaverage=MathSum/number
        Englishaverage=EnglishSum/number
        Physicsaverage=PhysicsSum/number
        println("course	 average	 min	 max 	(Male)")
        Mathaverage=Mathaverage.toInt
        Englishaverage=Englishaverage.toInt
        Physicsaverage=Physicsaverage.toInt
        println("Math:	   "+Mathaverage+"		"+MathMin+"	 "+MathMax)
        println("English:	   "+Englishaverage+"		"+EnglishMin+"	 "+EnglishMax)
        println("Physics:	   "+Physicsaverage+"		"+PhysicsMin+"	 "+PhysicsMax)
  }

  def getDataFeMale(value: ArrayBuffer[Student]){
        var Mathaverage:Double=0;
        var MathSum:Double = 0;
        var MathMin:Double = value(1).Math;
        var MathMax:Double = 0;

        var Englishaverage:Double=0;
        var EnglishSum:Double = 0;
        var EnglishMin:Double = value(1).English;
        var EnglishMax:Double = 0;

        var Physicsaverage:Double=0;
        var PhysicsSum:Double = 0;
        var PhysicsMin:Double = value(1).Physics;
        var PhysicsMax:Double = 0;

        var number = 0;

        for (i <- 0 until value.length){
              if(value(i).gender.equals("female")){
               number=number+1
  
            MathSum = value(i).Math+MathSum
            EnglishSum = value(i).English+EnglishSum
            PhysicsSum = value(i).Physics+PhysicsSum

            if(value(i).Math>MathMax){
                 MathMax=value(i).Math;
            }
            if(value(i).English>EnglishMax){
                  EnglishMax=value(i).English;
            }
            if(value(i).Physics>PhysicsMax){
                      PhysicsMax=value(i).Physics;
            }
            if(value(i).Math<MathMin){
                  MathMin=value(i).Math;
            }
            if(value(i).English<EnglishMin){
                  EnglishMin=value(i).English;
            }
            if(value(i).Physics<PhysicsMin){
                  PhysicsMin=value(i).Physics;
            }
              }
        }

        Mathaverage=MathSum/number
        Englishaverage=EnglishSum/number
        Physicsaverage=PhysicsSum/number
        println("course	 average	 min	 max 	(FeMale)")
        Mathaverage=Mathaverage.toInt
        Englishaverage=Englishaverage.toInt
        Physicsaverage=Physicsaverage.toInt
        println("Math:	   "+Mathaverage+"		"+MathMin+"	 "+MathMax)
        println("English:	   "+Englishaverage+"		"+EnglishMin+"	 "+EnglishMax)
        println("Physics:	   "+Physicsaverage+"		"+PhysicsMin+"	 "+PhysicsMax)
      }
}
class Student(id1:Int,gender1:String,Math1:Double,English1:Double,Physics1:Double){
  var id:Int = id1
  var gender:String = gender1
  var Math:Double = Math1
  var English:Double = English1
  var Physics:Double = Physics1

  override def toString = s"Student($id, $gender, $Math, $English, $Physics)"
}

技术图片

第一种方法想要改变样本得重新一个一个录入不方便。

 

第二种方法:

技术图片
object scoreReport
 {
     def main(args:Array[String])
     {
         //假设数据在当前目录下
         val inputFile=scala.io.Source.fromFile("test1.txt")
         
         //"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)
     
     }
     
 }
View Code

样本一:

技术图片

 

样本二:

技术图片

第二种方法采用从文件读取的方法,比较方便快捷,容易改变录入的信息。

以上是关于大三寒假生活3的主要内容,如果未能解决你的问题,请参考以下文章

大三寒假生活5

大三寒假生活指导

大三寒假生活13

大三寒假生活2

大三寒假生活4

记录下一个软件工程专业大三学生的生活片段