Institute of Computer Science
  1. Courses
  2. 2019/20 fall
  3. Programming Languages (MTAT.03.006)
ET
Log in

Programming Languages 2019/20 fall

  • Info
  • Õppekava
  • Moodle
  • Loengud & Praksid
  • Lisamaterjalid
  • Küsi abi! (Fleep)

Teine Scala kodutöö

Implementeerida Poola kuju (mitte RPN) kalkulaator. Tulemus peab olema selline, et kutsudes käsurealt välja argumendiga '+ * 2 3 4' vastab programm "Vastus: 10\n".

a) Kirjuta meetodid loe, liida, lahuta, korruta ja jaga. Kõik meetodid, mis tuleb kirjtada, on kujul

def f(xs: Seq[String]): (Double, Seq[String]) = ???

Sisse võetakse järjestus Seq[String] kujul ja väljastatakse paar mille esimene kompnent on väärtus ja teine komponent ülejäänud avaldis.

Meetod loe: vaatab järjestuse esimest sümbolit, kui see on "+", "-", "*" või "/" siis kutsub välja vastava meetodi --- argumendiks anda järjestue saba. Kui tegemist pole operaatoriga, eeldame et see on ujukoma arvu kujul --- tagastame selle paaris koos järjestuse sabaga.

Näiteks:

  • loe(Seq("*","2","3","4")) tagastab (6.0, Seq("4"))
  • loe(Seq("2","3","4")) tagastab (2.0, Seq("3", "4"))

Operaatorite meetodid (näiteks liida): meetodiga loe(xs) lugeda sisse (x,r1), siis loe(r1) lugeda (y,r2), nüüd tagastada (x+y, r2).

Näiteks:

  • korruta(Seq("2","3","4")) tagastab (6.0, Seq("4"))
  • loe(Seq("2","4","5")) tagastab (2.0, Seq("4", "5"))

b) Lisa veaolukordade haldus. Veast peab teatama nii juhtudel kui argumente on puudu, üle või on sisestatud tundmatu operaator. (Vihje: sys.exit() sulgeb programmi)

object Calc {
  def main(args: Array[String]): Unit = {
    val xs = args(0).split(" ") // teeb "+ 2 4"-st järjestuse Seq("+","2","4")
    // ...
  }
}
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment