-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadder.scala
More file actions
executable file
·52 lines (44 loc) · 1.18 KB
/
adder.scala
File metadata and controls
executable file
·52 lines (44 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
echo "Adder - Simple app for summing grades."
echo "Type numbers to add them. Empty line (double enter) to display sum."
exec scala "$0" "$@"
!#
/*
Usage: ./adder.scala
*/
import java.nio.file.{Paths,Path,Files}
import java.util.Date
object App {
private val fmt = new java.text.SimpleDateFormat("hh:mm:ss.S")
import Console.{GREEN, RED, RESET, YELLOW}
val numScanner = "^[0-9]+(\\.[0-9]*)?$".r
var currentSum=0.0
def printSum() = Console.println(s"${RESET}${YELLOW}${currentSum}${RESET}")
def start() = {
var go = true
while ( go ) {
Console.print(s"${RESET}${GREEN}> ${RESET}")
val line = io.StdIn.readLine()
if ( line == null ) {
go = false
} else if ( line.isEmpty ) {
if ( currentSum != 0) {
printSum()
currentSum = 0.0
}
} else {
numScanner.findFirstIn(line) match {
case Some(numberString) => currentSum = currentSum+numberString.toDouble
case None => line match {
case "\\q" => go = false
case _ => println( "decimal numbers, or \\q")
}
}
}
}
if ( currentSum != 0) {
printSum()
}
}
}
App.start()