Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "[
- Evaluator for arithmetic expressions involving
- +, -, *, / in REAL_32 arithmetic
- Use Dijsktra's two stack algorithm
- https://algs4.cs.princeton.edu/13stacks/Evaluate.java.html
- TBD -- features marked with this are To Be Done
- ]"
- author: "wahab safdar"
- date: "$1/02/2018$"
- revision: "$Revision$"
- class
- EVALUATOR
- create
- make
- feature {NONE} -- Constructor
- make (stack_type: STRING)
- -- initialize
- require
- stack_type ~ "array" OR stack_type ~ "list"
- do
- if stack_type ~ "array" then
- create {STACK_ARRAY [STRING]} ops.make
- create {STACK_ARRAY [REAL]} vals.make
- else
- check
- stack_type ~ "list"
- end
- create {STACK_LIST [STRING]} ops.make
- create {STACK_LIST [REAL]} vals.make
- end
- error := True
- expression := "None"
- end
- feature -- Queries
- ops: ABSTRACT_STACK [STRING]
- -- operations stack
- vals: ABSTRACT_STACK [REAL]
- -- values stack
- expression: STRING
- -- string espression to be evaluated
- value: REAL
- -- value if no error
- require
- not error
- attribute
- end
- error: BOOLEAN
- -- Is there a syntax error in `expression'
- error_string (s: STRING): STRING
- -- Error message if any
- local
- tokenizer: TOKENIZER
- do
- create tokenizer.make
- Result := tokenizer.error_string (s)
- end
- is_valid (s: STRING): BOOLEAN
- -- Is string `s' a valid arithmetic expression?
- local
- tokenizer: TOKENIZER
- do
- create tokenizer.make
- Result := tokenizer.is_arithmetic_expression (s)
- end
- evaluated (s: STRING): REAL
- require
- valid_expresion : is_valid (s)
- local
- list: ARRAY [STRING];
- l_val: REAL;
- l_ops: STRING
- token1: TOKENIZER
- n_ops : STACK_ARRAY [STRING]
- n_vals : STACK_ARRAY [REAL]
- do
- create list.make_empty
- create token1.make
- create {STACK_ARRAY[STRING]} n_ops.make
- create {STACK_ARRAY[REAL]} n_vals.make
- list := token1.get_tokens (s) -- tokenizer
- error := false
- expression := s
- across list as cursor
- loop
- if cursor.item ~ "(" then -- do nothing
- elseif cursor.item ~ "+" then
- n_ops.push (cursor.item)
- elseif cursor.item ~ "-" then
- n_ops.push (cursor.item)
- elseif cursor.item ~ "*" then
- n_ops.push (cursor.item)
- elseif cursor.item ~ "/" then
- n_ops.push (cursor.item)
- elseif cursor.item ~ ")" then
- l_ops := n_ops.top
- n_ops.pop
- l_val := n_vals.top
- n_vals.pop
- if l_ops ~ "+" then
- l_val := n_vals.top + l_val
- n_vals.pop
- elseif l_ops ~ "-" then
- l_val := n_vals.top - l_val
- n_vals.pop
- elseif l_ops ~ "*" then
- l_val := n_vals.top * l_val
- n_vals.pop
- elseif l_ops ~ "/" then
- l_val := n_vals.top / l_val
- n_vals.pop
- end
- n_vals.push (l_val)
- else
- n_vals.push (cursor.item.to_real)
- l_val := n_vals.top
- end
- end
- Result := l_val -- store the value of the expression
- end
- feature -- Commands
- evaluate (a_expression: STRING)
- require
- -- ??? need a space between each token
- local
- list: ARRAY [STRING];
- l_val: REAL;
- l_ops: STRING
- token1: TOKENIZER
- do
- create list.make_empty
- create token1.make
- list := token1.get_tokens (a_expression) -- tokenizer
- error := false
- expression := a_expression
- across list as cursor
- loop
- if cursor.item ~ "(" then -- do nothing
- elseif cursor.item ~ "+" then
- ops.push (cursor.item)
- elseif cursor.item ~ "-" then
- ops.push (cursor.item)
- elseif cursor.item ~ "*" then
- ops.push (cursor.item)
- elseif cursor.item ~ "/" then
- ops.push (cursor.item)
- elseif cursor.item ~ ")" then
- l_ops := ops.top
- ops.pop
- l_val := vals.top
- vals.pop
- if l_ops ~ "+" then
- l_val := vals.top + l_val
- vals.pop
- elseif l_ops ~ "-" then
- l_val := vals.top - l_val
- vals.pop
- elseif l_ops ~ "*" then
- l_val := vals.top * l_val
- vals.pop
- elseif l_ops ~ "/" then
- l_val := vals.top / l_val
- vals.pop
- end
- vals.push (l_val)
- else
- vals.push (cursor.item.to_real)
- end
- end
- value := vals.top -- store the value of the expression
- end
- feature {NONE} -- implementation
- -- put your implementation features here
- invariant
- consistency1: (expression /~ "None") implies (value = evaluated (expression))
- -- not the other way because?
- consistency2: (expression /~ "None") = (not error)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement