├── 01.pdf ├── 02.pdf ├── 03.pdf ├── 05.pdf ├── 06.pdf ├── 07.pdf ├── 08.pdf ├── 09.pdf ├── 10.pdf ├── LICENSE ├── README.md ├── build.sbt ├── lx01a ├── Makefile ├── simple.c ├── simple.s └── simple.scala ├── lx14 ├── build.sbt ├── project │ └── Build.scala └── src │ ├── actor1.scala │ ├── actor2.scala │ ├── actorf.scala │ ├── clicks.scala │ ├── thread1.scala │ └── thread2.scala ├── src ├── ex04a-power.scala ├── ex04b-binary.scala ├── lx00-a.scala ├── lx02a-leapyear.scala ├── lx02b-puzzle.scala ├── lx03a-ioverflow.scala ├── lx03b-overload.scala ├── lx03c-format.scala ├── lx03d-fpoverflow.scala ├── lx03e-fpunderflow.scala └── lx04-ioverflow.scala ├── test ├── lx02a-leapyear.scala └── lx02b-puzzle.scala └── workbook └── workbook05.pdf /01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/01.pdf -------------------------------------------------------------------------------- /02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/02.pdf -------------------------------------------------------------------------------- /03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/03.pdf -------------------------------------------------------------------------------- /05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/05.pdf -------------------------------------------------------------------------------- /06.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/06.pdf -------------------------------------------------------------------------------- /07.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/07.pdf -------------------------------------------------------------------------------- /08.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/08.pdf -------------------------------------------------------------------------------- /09.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/09.pdf -------------------------------------------------------------------------------- /10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/10.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | 117 | 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 2015年度後期 計算機科学第一 2 | 3 | - 情報科学科(講義: 2,実験: 1) 4 | - 講義コード 5527 5 | - 担当者 [脇田 建](https://github.com/wakita),[森立平](https://github.com/ryuhei-mori) 6 | - ティーチングアシスタント [藤田 健太](https://github.com/nikeeshi),[芦田 亮](https://github.com/RyoAshida) 7 | 8 | [講義のページ](https://titech-is-cs115.github.io/lecture/)へ, 9 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | // 重要な注意:各設定の間に必ず空行を挿入して下さい。これをしないと sbt が起動しません。 2 | 3 | name := "lecture" // プロジェクトの名称 4 | 5 | version := "0.1.0" // プロジェクトのバージョン番号 6 | 7 | scalaVersion := "2.11.7" // コンパイルに使う scalac のバージョン 8 | 9 | scalacOptions ++= // scalac に与えるオプション 10 | Seq("-optimize", 11 | "-feature", 12 | "-unchecked", 13 | "-deprecation") 14 | 15 | javaOptions in run ++= // 仮想機械に与えるオプション 16 | Seq( "-Xmx2G", "-verbose:gc") 17 | 18 | // プロジェクトで使う非標準 Scala ライブラリ 19 | 20 | libraryDependencies += // テストのために使う ScalaTest ライブラリ 21 | "org.scalatest" % "scalatest_2.11" % "2.2.5" % "test" 22 | 23 | libraryDependencies += // データ生成テストのために使う ScalaCheck ライブラリ 24 | "org.scalacheck" %% "scalacheck" % "1.12.5" % "test" 25 | 26 | libraryDependencies += // JSONデータを扱うためのライブラリ 27 | "net.liftweb" % "lift-json_2.11" % "3.0-M6" 28 | 29 | // sbt の挙動の設定 30 | 31 | fork := true 32 | 33 | connectInput := true 34 | 35 | // ソースコードの在処を非標準の場所に設定 36 | 37 | scalaSource in Compile := baseDirectory.value / "src" 38 | 39 | scalaSource in Test := baseDirectory.value / "test" 40 | 41 | // コンパイル結果を非標準の場所に設定 42 | 43 | target := Path.userHome / "tmp" / "cs1f" / name.value 44 | -------------------------------------------------------------------------------- /lx01a/Makefile: -------------------------------------------------------------------------------- 1 | all: simple simple.s Simple.class 2 | 3 | simple: simple.c 4 | clang -o simple simple.c 5 | 6 | simple.s: simple.c 7 | clang -O0 -S simple.c 8 | 9 | Simple.class: simple.scala 10 | scalac simple.scala 11 | 12 | clean: 13 | rm -f simple simple.s *.class 14 | -------------------------------------------------------------------------------- /lx01a/simple.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int simple(int a, int n) { 4 | for (int i = 1; i <= n; i++) { 5 | a = a + i; 6 | } 7 | return a; 8 | } 9 | 10 | int main() { 11 | printf("1 + 2 + ... + 10 = %d\n", simple(0, 10)); 12 | } 13 | -------------------------------------------------------------------------------- /lx01a/simple.s: -------------------------------------------------------------------------------- 1 | .section __TEXT,__text,regular,pure_instructions 2 | .macosx_version_min 10, 10 3 | .globl _simple 4 | .align 4, 0x90 5 | _simple: ## @simple 6 | .cfi_startproc 7 | ## BB#0: 8 | pushq %rbp 9 | Ltmp0: 10 | .cfi_def_cfa_offset 16 11 | Ltmp1: 12 | .cfi_offset %rbp, -16 13 | movq %rsp, %rbp 14 | Ltmp2: 15 | .cfi_def_cfa_register %rbp 16 | movl %edi, -4(%rbp) 17 | movl %esi, -8(%rbp) 18 | movl $1, -12(%rbp) 19 | LBB0_1: ## =>This Inner Loop Header: Depth=1 20 | movl -12(%rbp), %eax 21 | cmpl -8(%rbp), %eax 22 | jg LBB0_4 23 | ## BB#2: ## in Loop: Header=BB0_1 Depth=1 24 | movl -4(%rbp), %eax 25 | addl -12(%rbp), %eax 26 | movl %eax, -4(%rbp) 27 | ## BB#3: ## in Loop: Header=BB0_1 Depth=1 28 | movl -12(%rbp), %eax 29 | addl $1, %eax 30 | movl %eax, -12(%rbp) 31 | jmp LBB0_1 32 | LBB0_4: 33 | movl -4(%rbp), %eax 34 | popq %rbp 35 | retq 36 | .cfi_endproc 37 | 38 | .globl _main 39 | .align 4, 0x90 40 | _main: ## @main 41 | .cfi_startproc 42 | ## BB#0: 43 | pushq %rbp 44 | Ltmp3: 45 | .cfi_def_cfa_offset 16 46 | Ltmp4: 47 | .cfi_offset %rbp, -16 48 | movq %rsp, %rbp 49 | Ltmp5: 50 | .cfi_def_cfa_register %rbp 51 | subq $16, %rsp 52 | xorl %edi, %edi 53 | movl $10, %esi 54 | callq _simple 55 | leaq L_.str(%rip), %rdi 56 | movl %eax, %esi 57 | movb $0, %al 58 | callq _printf 59 | xorl %esi, %esi 60 | movl %eax, -4(%rbp) ## 4-byte Spill 61 | movl %esi, %eax 62 | addq $16, %rsp 63 | popq %rbp 64 | retq 65 | .cfi_endproc 66 | 67 | .section __TEXT,__cstring,cstring_literals 68 | L_.str: ## @.str 69 | .asciz "1 + 2 + ... + 10 = %d\n" 70 | 71 | 72 | .subsections_via_symbols 73 | -------------------------------------------------------------------------------- /lx01a/simple.scala: -------------------------------------------------------------------------------- 1 | object Simple { 2 | def simple(a: Int, n: Int): Int = { 3 | def aux(a: Int, i: Int): Int = { 4 | if (i > n) a else aux(a + i, i + 1) 5 | } 6 | aux(a, 1); 7 | } 8 | 9 | def main(arguments: Array[String]) { 10 | println("1 + 2 + ... + 10 = " + simple(0, 10)) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /lx14/build.sbt: -------------------------------------------------------------------------------- 1 | // 重要な注意:各設定の間に必ず空行を挿入して下さい。これをしないと sbt が起動しません。 2 | 3 | name := "lx14" // プロジェクトの名称 4 | 5 | version := "0.1.0" // プロジェクトのバージョン番号 6 | 7 | scalaVersion := "2.11.7" // コンパイルに使う scalac のバージョン 8 | 9 | scalacOptions ++= // scalac に与えるオプション 10 | Seq("-optimize", 11 | "-feature", 12 | "-unchecked", 13 | "-deprecation") 14 | 15 | javaOptions in run ++= // 仮想機械に与えるオプション 16 | Seq( "-Xmx2G", "-verbose:gc") 17 | 18 | libraryDependencies += // テストのために使う ScalaTest ライブラリ 19 | "org.scalatest" % "scalatest_2.11" % "2.2.5" % "test" 20 | 21 | libraryDependencies += 22 | "com.typesafe.akka" % "akka-actor_2.11" % "2.4.1" 23 | 24 | // sbt の挙動の設定 25 | 26 | fork := true 27 | 28 | connectInput := true 29 | 30 | // ソースコードの在処を非標準の場所に設定 31 | 32 | scalaSource in Compile := baseDirectory.value / "src" 33 | 34 | scalaSource in Test := baseDirectory.value / "test" 35 | 36 | // コンパイル結果を非標準の場所に設定 37 | 38 | target := Path.userHome / "tmp" / "cs1f" / name.value 39 | -------------------------------------------------------------------------------- /lx14/project/Build.scala: -------------------------------------------------------------------------------- 1 | import sbt._ 2 | import Keys._ 3 | 4 | object BuildSettings { 5 | val javaVersion = sys.props("java.specification.version") 6 | 7 | assert(javaVersion == "1.8" || javaVersion == "1.7", 8 | f"ScalaFX supports Java versions 1.7 and higher. Current version is (${javaVersion}). Please upgrade the version of your Java Development Kit") 9 | 10 | // ScalaFX の設定.Java 8 に依存するため注意を要する 11 | val scalafxSettings = 12 | if (javaVersion == "1.8") // Java 8 用の設定 13 | Seq(libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.60-R9") 14 | else // Java 7 用の設定 15 | Seq(libraryDependencies += "org.scalafx" %% "scalafx" % "2.2.76-R11", 16 | unmanagedJars in Compile += Attributed.blank(file(scala.util.Properties.javaHome) / "/lib/jfxrt.jar")) 17 | val buildSettings = Defaults.defaultSettings ++ scalafxSettings 18 | } 19 | 20 | object MyBuild extends Build { 21 | import BuildSettings._ 22 | lazy val root: Project = Project("root", file("."), settings = buildSettings) 23 | } 24 | -------------------------------------------------------------------------------- /lx14/src/actor1.scala: -------------------------------------------------------------------------------- 1 | package lx14 2 | 3 | import akka.actor.{Actor, Props, ActorSystem} 4 | 5 | class PrintActor extends Actor { 6 | def receive = { 7 | case x => println(x) 8 | } 9 | } 10 | 11 | object ActorSample1 extends App { 12 | val system = ActorSystem("sample1") 13 | val printer = system.actorOf(Props[PrintActor], "printer") 14 | 15 | printer ! "Hello world!" 16 | 17 | system.terminate() 18 | } 19 | -------------------------------------------------------------------------------- /lx14/src/actor2.scala: -------------------------------------------------------------------------------- 1 | package lx14 2 | 3 | import akka.actor.{Actor, Props, ActorSystem} 4 | 5 | case class AnswerS(s: String) 6 | 7 | class Master extends Actor { 8 | 9 | val doubler = context.actorOf(Props[Doubler]) 10 | 11 | def receive = { 12 | case i: Int => doubler ! i 13 | case AnswerS(msg) => println(msg); ActorSample2.stop = true 14 | } 15 | } 16 | 17 | class Doubler extends Actor { 18 | def receive = { 19 | case i: Int => sender ! AnswerS(f"$i * 2 = ${i * 2}") 20 | } 21 | } 22 | 23 | object ActorSample2 extends App { 24 | val system = ActorSystem("sample2") 25 | val master = system.actorOf(Props[Master], "master") 26 | 27 | var stop = false 28 | master ! 100 29 | 30 | while (!stop) Thread.sleep(500) 31 | system.terminate() 32 | } 33 | -------------------------------------------------------------------------------- /lx14/src/actorf.scala: -------------------------------------------------------------------------------- 1 | package lx14 2 | 3 | import akka.actor.{Actor, Props, ActorSystem} 4 | 5 | /* 6 | case class AnswerS(s: String) 7 | 8 | class Fibonacci extends Actor { 9 | 10 | val doubler = context.actorOf(Props[Doubler]) 11 | 12 | def receive = { 13 | case (i: Int, adder: Adder) => doubler ! i 14 | } 15 | } 16 | 17 | class Adder extends Actor { 18 | var nAnswers = 0 19 | 20 | def receive = { 21 | case i: Int => sender ! AnswerS(f"$i * 2 = ${i * 2}") 22 | } 23 | } 24 | 25 | object ActorSampleF extends App { 26 | val system = ActorSystem("sample2") 27 | val master = system.actorOf(Props[Master], "master") 28 | 29 | var stop = false 30 | master ! 100 31 | 32 | while (!stop) Thread.sleep(500) 33 | system.terminate() 34 | } 35 | */ 36 | -------------------------------------------------------------------------------- /lx14/src/clicks.scala: -------------------------------------------------------------------------------- 1 | package lx14 2 | 3 | import scala.compat.Platform 4 | 5 | import scalafx.Includes._ 6 | import scalafx.application.JFXApp 7 | import scalafx.application.JFXApp.PrimaryStage 8 | import scalafx.scene.Scene 9 | import scalafx.scene.input.{MouseEvent} 10 | import scalafx.scene.layout.{Pane} 11 | 12 | object Clicks extends JFXApp { 13 | 14 | val canvas = new Pane { } 15 | 16 | // 連続クリックにおけるクリック間に許容する最大の時間(ミリ秒単位) 17 | val DOUBLE_CLICK_WAIT_MS = 300; 18 | 19 | var clickedAt = Long.MinValue 20 | canvas.onMousePressed = { (ev: MouseEvent) => 21 | 22 | // すべてのスレッドから見えている変数にそのスレッドが担当するクリックの時刻を 23 | // 記録する.ここには最後のクリックに対応するクリック時刻が刻まれる. 24 | clickedAt = Platform.currentTime 25 | 26 | new Thread { // 新しいスレッド(並行実行単位)を生む 27 | 28 | override def run { 29 | 30 | // このスレッドが担当するクリックの時刻 31 | val clicked = clickedAt 32 | 33 | // 自分が担当するクリックが連続クリックの最後なのか判断するため, 34 | // 少し寝て後続のクリックの有無を確認する. 35 | Thread.sleep(DOUBLE_CLICK_WAIT_MS) 36 | 37 | // 起きたところで,後続のクリックの有無を確認する. 38 | // もしも後続のクリックがいたとしたら,最後に生まれたスレッドの誕生時刻 39 | // (clickedAt)に,自分の誕生時刻(clicked)よりも遅い時刻が記録されるはずである. 40 | if (clicked == clickedAt) { 41 | 42 | // 最後のクリック時刻(clickedAt)と自分のクリック時刻(clicked)が一致した. 43 | // ということは,自分が寝ているあいだに後続のクリックはなかったことになる. 44 | // したがって,このクリックが連続クリックの最後のクリックである. 45 | val clicks = ev.clickCount match { 46 | case 1 => "シングル" case 2 => "ダブル" case 3 => "トリプル" 47 | case n => ev.clickCount + "-" 48 | } 49 | println(f"${clicks}クリックを検知しました.") 50 | 51 | } else 52 | // クリックのあと寝ている間に後続のクリックがあった.連続クリックの処理は 53 | // 後続のクリックを処理するスレッドに任せる. 54 | println(f" ${ev.clickCount}番目のクリックを無視しました.") 55 | } 56 | }.start 57 | } 58 | 59 | stage = new PrimaryStage { 60 | title = "連続クリックのテスト" 61 | scene = new Scene(600, 400) { 62 | root = canvas 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /lx14/src/thread1.scala: -------------------------------------------------------------------------------- 1 | package lx14 2 | 3 | object ThreadSample1 { 4 | def main(args: Array[String]) { 5 | val names = List("イチゴ", "ミカン", "メロン", "リンゴ") 6 | for (i <- 0 to 3) { 7 | new Thread { 8 | val name = names(i) 9 | override def run () { 10 | for (i <- 0 to 3) { 11 | println(f"$name ($i)") 12 | } 13 | } 14 | }.start () 15 | } 16 | println(f"main: ${names.length}個のスレッドを走らせています") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /lx14/src/thread2.scala: -------------------------------------------------------------------------------- 1 | package lx14 2 | 3 | import scala.util.Random.nextInt 4 | 5 | import scalafx.Includes._ 6 | import scalafx.application.JFXApp 7 | import scalafx.application.JFXApp.PrimaryStage 8 | import scalafx.scene.{Group, Scene} 9 | import scalafx.scene.canvas.{Canvas,GraphicsContext} 10 | import scalafx.scene.paint.Color 11 | import scalafx.scene.paint.Color._ 12 | import scalafx.scene.shape.ArcType 13 | 14 | object ThreadSample2 extends JFXApp { 15 | val W = 1200; val H = 600 16 | val canvas = new Canvas(W, H) 17 | val gc = canvas.graphicsContext2D 18 | 19 | var finish = false 20 | val lock = new AnyRef 21 | 22 | // 雨 23 | new Thread { 24 | override def run() { 25 | while (!finish) { 26 | lock.synchronized { 27 | for (i <- 0 to 3) { 28 | val x = nextInt(W) 29 | val y = nextInt(H) 30 | val r = nextInt(20) 31 | gc.fill = LightBlue 32 | gc.fillOval(x - r/2, y - r/2, r, r) 33 | } 34 | } 35 | Thread.sleep(30) 36 | } 37 | } 38 | }.start () 39 | 40 | // ワイパー 41 | new Thread { 42 | var angle = 0 43 | var dir = 1 44 | val speed = 180 / 30 45 | override def run() { 46 | while (!finish) { 47 | lock.synchronized { 48 | gc.fill = White 49 | gc.fillArc(0, 0, W, H * 2, angle, speed * dir, ArcType.Round) 50 | } 51 | angle = angle + speed * dir 52 | if (angle < 0) { angle = 0; dir = 1 } 53 | if (angle > 180) { angle = 180; dir = -1 } 54 | Thread.sleep(50) 55 | } 56 | } 57 | }.start () 58 | 59 | stage = new PrimaryStage { 60 | title = "Thread Sample 2" 61 | width = W 62 | height = H 63 | scene = new Scene { root = new Group(canvas) } 64 | } 65 | 66 | override def stopApp() { finish = true } 67 | } 68 | -------------------------------------------------------------------------------- /src/ex04a-power.scala: -------------------------------------------------------------------------------- 1 | package wakita.lx04 2 | 3 | object powerOf2 { 4 | 5 | // Int型のnに関して2^nを計算する関数 6 | def powerOf2(n: Int): Int = { 7 | def aux(i: Int, p: Int): Int = 8 | if (i == n) p 9 | else aux(i+1, p+p) 10 | aux(0, 1) 11 | } 12 | 13 | /* 14 | def main(arguments: Array[String]) { 15 | // println(f"Int: ${powerI(5)}") 16 | 17 | println(f"Byte: ${powerOf2[Byte] (5, 1, (p) => (p+p).toByte)}") 18 | println(f"Short: ${powerOf2[Short](5, 1, (p) => (p+p).toShort)}") 19 | println(f"Int: ${powerOf2[Int] (5, 1, (p) => (p+p))}") 20 | println(f"Long: ${powerOf2[Long] (5, 1, (p) => (p+p))}") 21 | } 22 | */ 23 | } 24 | -------------------------------------------------------------------------------- /src/ex04b-binary.scala: -------------------------------------------------------------------------------- 1 | // package wakita.ex04b 2 | 3 | object binary { 4 | def toBinary(n: Long, size: Int): String = { 5 | def aux(n: Long, i: Int, bits: List[String]): String = { 6 | if (i == 0) bits.mkString("") 7 | else aux(n >>> 1, i-1, (if ((n&1L) == 0L) "0" else "1") :: bits) 8 | } 9 | aux(n, size, List()) 10 | } 11 | 12 | def toBinary(n: Byte) :String = toBinary(n, 8) 13 | def toBinary(n: Short):String = toBinary(n, 16) 14 | def toBinary(n: Int) :String = toBinary(n, 32) 15 | def toBinary(n: Long) :String = toBinary(n, 64) 16 | 17 | def main(arguments: Array[String]) { 18 | println(f"5.toByte => ${toBinary(5.toByte)}") 19 | println(f"5.toShort => ${toBinary(5.toShort)}") 20 | println(f"5 => ${toBinary(5)}") 21 | println(f"5L => ${toBinary(5L)}") 22 | println(f"-5L => ${toBinary(-5L)}") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/lx00-a.scala: -------------------------------------------------------------------------------- 1 | //import org.scala_tools.time.Imports._ 2 | import net.liftweb.json._ 3 | 4 | object StudentInformation { 5 | val info = """{ 6 | "family": "あなたの姓に書き換えて下さい", 7 | "name": "あなたの名に書き換えて下さい", 8 | "id": "あなたの学籍番号に書き換えて下さい", 9 | "login": "あなたのログインIDに書き換えて下さい", 10 | "email": "あなたの東工大メールのアドレスの@から左の部分に書き換えて下さい" }""" 11 | 12 | def main(arguments: Array[String]) { 13 | implicit val format = DefaultFormats 14 | println(Serialization.write(parse(info))) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/lx02a-leapyear.scala: -------------------------------------------------------------------------------- 1 | package cs1.lx02a 2 | 3 | object LX02A { 4 | def leapyear(y: Int) = { 5 | true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/lx02b-puzzle.scala: -------------------------------------------------------------------------------- 1 | package cs1.lx02b 2 | 3 | object LX02B { 4 | } 5 | -------------------------------------------------------------------------------- /src/lx03a-ioverflow.scala: -------------------------------------------------------------------------------- 1 | package cs1 2 | 3 | // run-main cs1.LX03A 4 | 5 | object LX03A { 6 | 7 | // 2^nをByte上で計算する関数 8 | def powerOf2B(n: Int): Byte = { 9 | def aux(i: Int, p: Byte): Byte = { 10 | println(f"2^${i} = ${p}\n") 11 | if (i == n) p 12 | else aux(i+1, (p+p).toByte) 13 | } 14 | println("\n2^n[Byte]\n") 15 | aux(0, 1: Byte) 16 | } 17 | 18 | // 2^nをShort上で計算する関数 19 | def powerOf2S(n: Int): Short = { 20 | def aux(i: Int, p: Short): Short = { 21 | println(f"2^${i} = ${p}\n") 22 | if (i == n) p 23 | else aux(i+1, (p+p).toShort) 24 | } 25 | println("\n2^n[Short]") 26 | aux(0, 1: Short) 27 | } 28 | 29 | // 2^nをInt上で計算する関数 30 | def powerOf2I(n: Int): Int = { 31 | def aux(i: Int, p: Int): Int = { 32 | println(f"2^${i} = ${p}\n") 33 | if (i == n) p 34 | else aux(i+1, p+p) 35 | } 36 | println("\n2^n[Int]") 37 | aux(0, 1) 38 | } 39 | 40 | // 2^nをLong上で計算する関数 41 | def powerOf2L(n: Int): Long = { 42 | def aux(i: Int, p: Long): Long = { 43 | println(f"2^${i} = ${p}\n") 44 | if (i == n) p 45 | else aux(i+1, p+p) 46 | } 47 | println("\n2^n[Long]") 48 | aux(0, 1L) 49 | } 50 | 51 | def main(arguments: Array[String]) { 52 | powerOf2B(8); 53 | powerOf2S(16); 54 | powerOf2I(32); 55 | powerOf2L(64); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/lx03b-overload.scala: -------------------------------------------------------------------------------- 1 | package cs1 2 | 3 | // run-main cs1.LX03A 4 | 5 | object LX03B { 6 | 7 | // 2^nをByte上で計算する関数 8 | def powerOf2(n: Int, one: Byte): Byte = { 9 | def aux(i: Int, p: Byte): Byte = { 10 | println(f"2^${i} = ${p}\n") 11 | if (i == n) p 12 | else aux(i+1, (p+p).toByte) 13 | } 14 | println("\n2^n[Byte]\n") 15 | aux(0, one) 16 | } 17 | 18 | // 2^nをShort上で計算する関数 19 | def powerOf2(n: Int, one: Short): Short = { 20 | def aux(i: Int, p: Short): Short = { 21 | println(f"2^${i} = ${p}\n") 22 | if (i == n) p 23 | else aux(i+1, (p+p).toShort) 24 | } 25 | println("\n2^n[Short]") 26 | aux(0, one) 27 | } 28 | 29 | // 2^nをInt上で計算する関数 30 | def powerOf2(n: Int, one: Int): Int = { 31 | def aux(i: Int, p: Int): Int = { 32 | println(f"2^${i} = ${p}\n") 33 | if (i == n) p 34 | else aux(i+1, p+p) 35 | } 36 | println("\n2^n[Int]") 37 | aux(0, one) 38 | } 39 | 40 | // 2^nをLong上で計算する関数 41 | def powerOf2(n: Int, one: Long): Long = { 42 | def aux(i: Int, p: Long): Long = { 43 | println(f"2^${i} = ${p}\n") 44 | if (i == n) p 45 | else aux(i+1, p+p) 46 | } 47 | println("\n2^n[Long]") 48 | aux(0, one) 49 | } 50 | 51 | def main(arguments: Array[String]) { 52 | powerOf2(8, 1.toByte); 53 | powerOf2(16, 1.toShort); 54 | powerOf2(32, 1); 55 | powerOf2(64, 1L); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/lx03c-format.scala: -------------------------------------------------------------------------------- 1 | package cs1 2 | 3 | import scala.math._ 4 | 5 | // run-main cs1.LX03C: 書式つき文字列の例 6 | 7 | object LX03C { 8 | def fact(n: Int): Int = { 9 | if (n == 0) 1 10 | else n * fact(n - 1) 11 | } 12 | 13 | def main(arguments: Array[String]) { 14 | // ${...} の内側に書かれた Scala の式の評価結果が文字列に埋め込まれる 15 | println(f"fact(10) = ${fact(10)}") 16 | println(f"fact(10) は偶数か? ${fact(10) % 2 == 0}") 17 | println(f"fact(10) は偶数か? ${if (fact(10) % 2 == 0) "はい" else "いいえ"}") 18 | println(f"Int.MaxValue = ${Int.MaxValue}") 19 | 20 | // 変数の値を文字列に埋め込むときは { } を省略できる. 21 | val lmax = Long.MaxValue 22 | println(f"Long.MaxValue = $lmax") 23 | 24 | // %d, %x, %f, %gなどを使ってさらに細かく書式を制御できる 25 | // %8x: 8桁の16進表記を埋める 26 | // %08x: 8桁とするが桁が埋まらない高位の桁は0で埋める 27 | println(f"100> ${100}, 100> ${100}%8x, 100> ${100}%08x") 28 | // %f: 実数表記を埋める 29 | // %1.5f: 小数点以上は1桁で,小数点以下は5桁という表記を埋める. 30 | // %e: 指数表現を埋める. 31 | println(f"100の16進表現 = ${100}%x, π = ${Pi}%1.5f, e^30 = ${pow(E, 30)}%g") 32 | 33 | // もっと詳しいことは Java の [Formatter クラス](http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax). 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/lx03d-fpoverflow.scala: -------------------------------------------------------------------------------- 1 | package cs1 2 | 3 | // run-main cs1.LX03D: 浮動小数点数計算の誤差とオーバーフローの観察 4 | 5 | object LX03D { 6 | def main(arguments: Array[String]) { 7 | def addMany(delta: Float, n: Int, v: Float): Float = 8 | if (n == 0) v else addMany(delta, n-1, v + delta) 9 | 10 | println(f"0.01 * 100 = ${addMany(0.01f, 100, 0f)}") 11 | 12 | val N = 10000000 13 | val s0 = 0f 14 | 15 | val s1 = addMany(1f, N, s0) 16 | println(f"1.0 * $N = $s1\n") 17 | 18 | val s2 = addMany(1f, N, s1) 19 | println(f"1.0 * $N = $s2\n") 20 | 21 | val s3 = addMany(1f, N, s2) 22 | println(f"1.0 * $N = $s3\n") 23 | 24 | val s4 = addMany(1f, N, s3) 25 | println(f"1.0 * $N = $s4\n") 26 | 27 | val s5 = addMany(1f, N, s4) 28 | println(f"1.0 * $N = $s5\n") 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/lx03e-fpunderflow.scala: -------------------------------------------------------------------------------- 1 | package cs1 2 | 3 | // run-main cs1.LX03E: 浮動小数点数演算におけるアンダーフローの観察 4 | 5 | object LX03E { 6 | def fpUnderflow(n: Int, f: Double) { 7 | def aux(i: Int, x: Double) { 8 | if (i != n) { 9 | println(f"$f * 2^-$i = $x%g") 10 | aux(i+1, x/2) 11 | } 12 | } 13 | aux(0, f) 14 | } 15 | 16 | def main(arguments: Array[String]) { 17 | fpUnderflow((2<<9) + 52, 1.0) 18 | } 19 | // さて,fpUnderflowの第一引数の式はあまりに恣意的だ. 20 | // どうやってこのコードの著者は実行の最初に 0.00000 が出現するところでプログラムを終了できたのだろう.IEEE 754標準との関連で考えるとこの謎が解ける. 21 | } 22 | -------------------------------------------------------------------------------- /src/lx04-ioverflow.scala: -------------------------------------------------------------------------------- 1 | package cs1.lx04 2 | 3 | // run-main cs1.lx03.Main 4 | 5 | object LX04A { 6 | def powerOf2[T](n: Int, i: Int, square: T => T, p: T) { 7 | if (i <= n) { 8 | if (i <= 3 || i >= n - 3) println(f"2^$i = $p") 9 | if (i == 3) println("...") 10 | powerOf2(n, i+1, square, square(p)) 11 | } 12 | } 13 | 14 | def main(arguments: Array[String]) { 15 | println("\n2^n[Byte]") 16 | powerOf2[Byte] ( 8, 0, (x: Byte) => (x + x).toByte, 1) 17 | println("\n2^n[Short]") 18 | powerOf2[Short](16, 0, (x: Short) => (x + x).toShort, 1) 19 | println("\n2^n[Int]") 20 | powerOf2[Int] (32, 0, (x: Int) => x + x, 1) 21 | println("\n2^n[Long]") 22 | powerOf2[Long] (64, 0, (x: Long) => x + x, 1) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /test/lx02a-leapyear.scala: -------------------------------------------------------------------------------- 1 | package cs1.lx02a 2 | 3 | import org.scalatest._ 4 | import LX02A._ 5 | 6 | class Test extends FlatSpec with Matchers { 7 | 8 | "leapyear" should "be true for 4で割り切れる年" in { 9 | leapyear(2004) should be (true) 10 | leapyear(2008) should be (true) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/lx02b-puzzle.scala: -------------------------------------------------------------------------------- 1 | package cs1.lx02b 2 | 3 | import org.scalatest._ 4 | import LX02B._ 5 | 6 | class Test extends FlatSpec with Matchers { 7 | } 8 | -------------------------------------------------------------------------------- /workbook/workbook05.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titech-is-cs115/lecture/f96c460d3b90da399175a59f6f95892520caf381/workbook/workbook05.pdf --------------------------------------------------------------------------------