SOP expressions are sums of products of doubles: Expression ::= Sum | Product Su
ID: 3786793 • Letter: S
Question
SOP expressions are sums of products of doubles:
Expression ::= Sum | Product
Sum ::= Product~("+"~Product)*
Product ::= Double~("*"~Double)*
The SOP console reads a string from the keyboard and breaks it into a list of sub-strings separated by + signs. These sub-strings are broken into a list of sub-sub-strings separated by * signs. The sub-sub-strings should be parsable as doubles. (Otherwise and exception should be thrown.)
The lists of sub-sub-strings should be converted into instances of a Product class. Lists of products should be converted into lists of expressions
Hint
You can modify the console presented in class.
You could break a string such as 1+2*3 + 4 * 5 * 6 into the lists of lists [1, [2, 3], [4, 5, 6]] by creating scanners using the right delimiter pattern. For example:
Scanner tokens1 = new Scanner(input).useDelimiter("\s*\+\s*");
«interface» Expression +execute0: Double Sum Product -operands: Product(1. -operands: Double[1. Console +repl0 +parse(line: String): ExpressionExplanation / Answer
strange_list = [(1, 2), [1, 2], '12', 12, 12.0] >>> print(strange_list[0], type(strange_list[0])) scanner sc= new scanner.sc(); (1, 2) print(strange_list[0:1], type(strange_list[0:1])) [(1, 2)] print(strange_list[2], type(strange_list[2])) print(strange_list[2:3], type(strange_list[2:3]))Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.