Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this question, you will also write a function that calculates the result of a

ID: 3793406 • Letter: I

Question

In this question, you will also write a function that calculates the result of a arithmetic expression between scala where the arithmetic expression is given as a character "30+5 4"). The operators allowed in the arithmetic expression are and which represent addition, subtraction, multiplication, division, and exponentiation, re- spectively. Additionally, we introduce the undo operator The operator signi es cancel the previous operation". Two successive undo operators signify lcancel the previous two operations and so on. The operation that the undo operator removes consists of the number and the operator that precede the undo operator. For exam combination of the ple, in the expres *30!+5 4', the undo operator cancels the multiplication by 30 and the arithmetic expression reduces to '2+54'. As other example, in the expression '1+2 30!!+5 4', the undo operators cancel the multiplication by 30 and the addition of 2, and the arithmetic expression reduces to '1+5 4'.The arithmetic expression should be evaluated from left to right, assuming that all operators have equal precedence. Write a function with the following header: function [result] my calculator with undo(expression) where: expression is a row vector of class char that represents an arithmetic expression as described above. result is a scalar of class double that represents the value of the arithmetic expression described by expression. You can assume that: expression is not empty. xpression is a valid arithmetic expression. There are enough operations before undo operators such that there remains at least one scalar to the left of where the undo operators were, after the corresponding operations have been undone. expression contains only characters among: 0123456789 n particular, expression does not contain spaces nor parentheses. The rst character in expression is one of the 10 digits There are no two operators (among the list: and T) in a row (e.g., +-)in expression. There can be, however, one or more undo operators directly before (but not after) any other operator, and/or at the end of expression You may not use Matlab's built-in functions split, strsplit, find, strfind, eval, and feval in this question Note: there are many ways to solve this question, including using a stack to keep track of ach operation and to facilitate the undoing of operations. Test cases: result my calculator with undo( 2.5-2) result 0.5000 result my calculator with undo(2 result 2.5000

Explanation / Answer

Matlab function my_calculator_with_undo.m

function [result] = my_calculator_with_undo( expression )
   stk_front = 0; % variable indicating the front of stack
   st = ''; % empty stack
   op_stk = []; % operator position monitor stack
   op_pos = 0; % operator position stack front
   N = length(expression);% length of the expression
   for k = 1:N % loop to monitor each element in expression
       % checking expression(k) is a operator or not
       if(expression(k) == '+' || ...
          expression(k) == '-' || ...
          expression(k) == '*' || ...
          expression(k) == '/' || ...
          expression(k) == '^' )
      % if yes add it in stack st and update operator position monitor stack
             stk_front = stk_front+1;
             st(stk_front) = num2str(expression(k));
             op_pos = op_pos +1;
             op_stk(op_pos) = stk_front;
       % checking expression(k) is a undo operator
       elseif(expression(k) == '!')
       % if yes remove the elements from the stacks
           stk_front = op_stk(op_pos) -1;
           op_pos = op_pos-1;
       % checking expression(k) is operand
       else
       % if yes add it in the stack
           stk_front = stk_front+1;
           st(stk_front) = num2str(expression(k));
       end
   end
   % evaluating the expression
   result = str2num(st(1:stk_front));
end

Test outputs

>> my_calculator_with_undo('2*30!+5^4')

ans =

   627

>> my_calculator_with_undo('1+2*30!!+5^4')

ans =

   626

>> my_calculator_with_undo('2.5-2!')

ans =

    2.5000

>> my_calculator_with_undo('2.5-2')

ans =

    0.5000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote