Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 4.49 KB | None | 0 0
  1. note
  2.     description: "[
  3.         Evaluator for arithmetic expressions involving
  4.         +, -, *, / in REAL_32 arithmetic
  5.         Use Dijsktra's two stack algorithm
  6.         https://algs4.cs.princeton.edu/13stacks/Evaluate.java.html
  7.        
  8.         TBD -- features marked with this are To Be Done
  9.     ]"
  10.     author: "wahab safdar"
  11.     date: "$1/02/2018$"
  12.     revision: "$Revision$"
  13.  
  14. class
  15.     EVALUATOR
  16.  
  17. create
  18.     make
  19.  
  20. feature {NONE} -- Constructor
  21.  
  22.     make (stack_type: STRING)
  23.             -- initialize
  24.         require
  25.             stack_type ~ "array" OR stack_type ~ "list"
  26.         do
  27.             if stack_type ~ "array" then
  28.                 create {STACK_ARRAY [STRING]} ops.make
  29.                 create {STACK_ARRAY [REAL]} vals.make
  30.             else
  31.                 check
  32.                     stack_type ~ "list"
  33.                 end
  34.                 create {STACK_LIST [STRING]} ops.make
  35.                 create {STACK_LIST [REAL]} vals.make
  36.             end
  37.             error := True
  38.             expression := "None"
  39.         end
  40.  
  41. feature -- Queries
  42.  
  43.     ops: ABSTRACT_STACK [STRING]
  44.             -- operations stack
  45.  
  46.     vals: ABSTRACT_STACK [REAL]
  47.             -- values stack
  48.  
  49.     expression: STRING
  50.             -- string espression to be evaluated
  51.  
  52.     value: REAL
  53.             -- value if no error
  54.         require
  55.             not error
  56.         attribute
  57.         end
  58.  
  59.     error: BOOLEAN
  60.             -- Is there a syntax error in `expression'
  61.  
  62.     error_string (s: STRING): STRING
  63.             -- Error message if any
  64.         local
  65.             tokenizer: TOKENIZER
  66.         do
  67.             create tokenizer.make
  68.             Result := tokenizer.error_string (s)
  69.         end
  70.  
  71.     is_valid (s: STRING): BOOLEAN
  72.             -- Is string `s' a valid arithmetic expression?
  73.         local
  74.             tokenizer: TOKENIZER
  75.         do
  76.             create tokenizer.make
  77.             Result := tokenizer.is_arithmetic_expression (s)
  78.         end
  79.  
  80. evaluated (s: STRING): REAL
  81.     require
  82.         valid_expresion : is_valid (s)
  83.         local
  84.             list: ARRAY [STRING];
  85.  
  86.             l_val: REAL;
  87.             l_ops: STRING
  88.             token1: TOKENIZER
  89.             n_ops : STACK_ARRAY [STRING]
  90.             n_vals : STACK_ARRAY [REAL]
  91.         do
  92.             create list.make_empty
  93.             create token1.make
  94.             create {STACK_ARRAY[STRING]} n_ops.make
  95.             create {STACK_ARRAY[REAL]} n_vals.make
  96.             list := token1.get_tokens (s) -- tokenizer
  97.         error := false
  98.         expression := s
  99.             across list as cursor
  100.  
  101.             loop
  102.  
  103.                 if cursor.item ~ "(" then -- do nothing
  104.                 elseif cursor.item ~ "+" then
  105.                     n_ops.push (cursor.item)
  106.                 elseif cursor.item ~ "-" then
  107.                     n_ops.push (cursor.item)
  108.                 elseif cursor.item ~ "*" then
  109.                     n_ops.push (cursor.item)
  110.                 elseif cursor.item ~ "/" then
  111.                     n_ops.push (cursor.item)
  112.                 elseif cursor.item ~ ")" then
  113.                     l_ops := n_ops.top
  114.                     n_ops.pop
  115.                     l_val := n_vals.top
  116.                     n_vals.pop
  117.                     if l_ops ~ "+" then
  118.                         l_val := n_vals.top + l_val
  119.                         n_vals.pop
  120.                     elseif l_ops ~ "-" then
  121.                         l_val := n_vals.top - l_val
  122.                         n_vals.pop
  123.                     elseif l_ops ~ "*" then
  124.                         l_val := n_vals.top * l_val
  125.                         n_vals.pop
  126.                     elseif l_ops ~ "/" then
  127.                         l_val := n_vals.top / l_val
  128.                         n_vals.pop
  129.                     end
  130.                     n_vals.push (l_val)
  131.                 else
  132.                     n_vals.push (cursor.item.to_real)
  133.                     l_val := n_vals.top
  134.                 end
  135.  
  136.             end
  137.             Result := l_val -- store the value of the expression
  138.         end
  139.  
  140. feature -- Commands
  141.    
  142.  
  143.     evaluate (a_expression: STRING)
  144.         require
  145.             -- ??? need a space between each token
  146.         local
  147.             list: ARRAY [STRING];
  148.  
  149.             l_val: REAL;
  150.             l_ops: STRING
  151.             token1: TOKENIZER
  152.         do
  153.             create list.make_empty
  154.             create token1.make
  155.             list := token1.get_tokens (a_expression) -- tokenizer
  156.         error := false
  157.         expression := a_expression
  158.             across list as cursor
  159.  
  160.             loop
  161.  
  162.                 if cursor.item ~ "(" then -- do nothing
  163.                 elseif cursor.item ~ "+" then
  164.                     ops.push (cursor.item)
  165.                 elseif cursor.item ~ "-" then
  166.                     ops.push (cursor.item)
  167.                 elseif cursor.item ~ "*" then
  168.                     ops.push (cursor.item)
  169.                 elseif cursor.item ~ "/" then
  170.                     ops.push (cursor.item)
  171.                 elseif cursor.item ~ ")" then
  172.                     l_ops := ops.top
  173.                     ops.pop
  174.                     l_val := vals.top
  175.                     vals.pop
  176.                     if l_ops ~ "+" then
  177.                         l_val := vals.top + l_val
  178.                         vals.pop
  179.                     elseif l_ops ~ "-" then
  180.                         l_val := vals.top - l_val
  181.                         vals.pop
  182.                     elseif l_ops ~ "*" then
  183.                         l_val := vals.top * l_val
  184.                         vals.pop
  185.                     elseif l_ops ~ "/" then
  186.                         l_val := vals.top / l_val
  187.                         vals.pop
  188.                     end
  189.                     vals.push (l_val)
  190.                 else
  191.                     vals.push (cursor.item.to_real)
  192.                 end
  193.  
  194.             end
  195.             value := vals.top -- store the value of the expression
  196.         end
  197.  
  198. feature {NONE} -- implementation
  199.     -- put your implementation features here
  200.  
  201. invariant
  202.     consistency1: (expression /~ "None") implies (value = evaluated (expression))
  203.         -- not the other way because?
  204.     consistency2: (expression /~ "None") = (not error)
  205.  
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement