| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 26 Apr 2024 17:37:56 +0100 | |
| changeset 485 | 02d2d49c9feb | 
| parent 425 | 6e990ae2c6a3 | 
| permissions | -rw-r--r-- | 
| 219 | 1  | 
// Shunting Yard Algorithm  | 
2  | 
// by Edsger Dijkstra  | 
|
3  | 
// ========================  | 
|
4  | 
||
| 396 | 5  | 
object C3a {
 | 
| 219 | 6  | 
|
| 220 | 7  | 
// type of tokens  | 
| 219 | 8  | 
type Toks = List[String]  | 
9  | 
||
| 220 | 10  | 
// the operations in the basic version of the algorithm  | 
| 219 | 11  | 
val ops = List("+", "-", "*", "/")
 | 
12  | 
||
13  | 
// the precedences of the operators  | 
|
14  | 
val precs = Map("+" -> 1,
 | 
|
15  | 
"-" -> 1,  | 
|
16  | 
"*" -> 2,  | 
|
17  | 
"/" -> 2)  | 
|
18  | 
||
19  | 
// helper function for splitting strings into tokens  | 
|
20  | 
def split(s: String) : Toks = s.split(" ").toList
 | 
|
21  | 
||
| 
425
 
6e990ae2c6a3
updated solutions and templates
 
Christian Urban <christian.urban@kcl.ac.uk> 
parents: 
396 
diff
changeset
 | 
22  | 
// ADD YOUR CODE BELOW  | 
| 
 
6e990ae2c6a3
updated solutions and templates
 
Christian Urban <christian.urban@kcl.ac.uk> 
parents: 
396 
diff
changeset
 | 
23  | 
//======================  | 
| 219 | 24  | 
|
25  | 
||
| 
425
 
6e990ae2c6a3
updated solutions and templates
 
Christian Urban <christian.urban@kcl.ac.uk> 
parents: 
396 
diff
changeset
 | 
26  | 
// (1)  | 
| 346 | 27  | 
def is_op(op: String) : Boolean = ???  | 
28  | 
def prec(op1: String, op2: String) : Boolean = ???  | 
|
| 219 | 29  | 
|
| 346 | 30  | 
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ???  | 
| 219 | 31  | 
|
32  | 
||
33  | 
// test cases  | 
|
34  | 
//syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
 | 
|
35  | 
//syard(split("10 + 12 * 33"))       // 10 12 33 * +
 | 
|
36  | 
//syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
 | 
|
37  | 
//syard(split("5 + 7 / 2"))          // 5 7 2 / +
 | 
|
38  | 
//syard(split("5 * 7 / 2"))          // 5 7 * 2 /
 | 
|
39  | 
//syard(split("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
 | 
|
40  | 
||
41  | 
//syard(split("3 + 4 + 5"))           // 3 4 + 5 +
 | 
|
42  | 
//syard(split("( ( 3 + 4 ) + 5 )"))    // 3 4 + 5 +
 | 
|
43  | 
//syard(split("( 3 + ( 4 + 5 ) )"))    // 3 4 5 + +
 | 
|
44  | 
//syard(split("( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )")) // 3 4 5 + +
 | 
|
45  | 
||
46  | 
||
| 
425
 
6e990ae2c6a3
updated solutions and templates
 
Christian Urban <christian.urban@kcl.ac.uk> 
parents: 
396 
diff
changeset
 | 
47  | 
// (2)  | 
| 346 | 48  | 
def compute(toks: Toks, st: List[Int] = Nil) : Int = ???  | 
| 219 | 49  | 
|
50  | 
||
51  | 
// test cases  | 
|
52  | 
// compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
 | 
|
53  | 
// compute(syard(split("10 + 12 * 33")))       // 406
 | 
|
54  | 
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
 | 
|
55  | 
// compute(syard(split("5 + 7 / 2")))          // 8
 | 
|
56  | 
// compute(syard(split("5 * 7 / 2")))          // 17
 | 
|
57  | 
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
 | 
|
58  | 
||
| 288 | 59  | 
}  | 
| 219 | 60  | 
|
61  |