I am using python language in PyCharm. Write a function moronic calc() that take
ID: 3802523 • Letter: I
Question
I am using python language in PyCharm.
Write a function moronic calc() that takes a string consisting of a mathematical expression written in Moronic numerals, such as ’FFSSEI - FIIE’, and returns the value of the expression in Moronic numerals.
In the argument you may assume that there is always exactly one space after the first operand, followed by an operator, followed by a space, followed by the second operand. Negation of numbers is also allowed, but with some limitations mentioned below. If a number is negated, then the negation symbol appears immediately to the left of the leftmost numeral.
Your calculator must support the following mathematical operators:
Operation Symbol Example Addition + FIE + –SSSEIII Subtraction – –SIE – –FSSSEIII Multiplication * –FSFEIII / EIII Integer (Floor) Division / IIIII * EEEEEIII Exponentiation ˆ –IIIII ˆ III
Important notes: • Either or both operands could be negative, except for the exponent in an exponentiation operation, which you may assume is positive. • You may assume that the result of an operation is in the range [3071, 3071], with the exception of zero. • If the result is positive, do NOT include a + sign in front of it. • Finally, you will want to have the moronic calc() function call your arabic2moronic() and moronic2arabic() functions as needed to help solve this problem.
Examples:
Function Call Return Value
moronic calc(’FIE + SSSEIII’) ’FSSSEEII’
moronic calc(’-SIE - -FSSSEIII’) ’FSSIIII’
moronic calc(’-FSFEIII / EIII’) ’-SEEEI’
moronic calc(’IIIII * EEEEEIII’) ’SSSEEIE’
moronic calc(’-IIIII ˆ III’) ’-SESIIIII’
Explanation / Answer
/* A Java program to evaluate a given expression where tokens are separated
by space.
Test Cases:
"10 + 2 * 6" ---> 22
"100 * 2 + 12" ---> 212
"100 * ( 2 + 12 )" ---> 1400
"100 * ( 2 + 12 ) / 14" ---> 100
*/
import java.util.Stack;
public class EvaluateString
{
public static int evaluate(String expression)
{
char[] tokens = expression.toCharArray();
Stack<Integer> values = new Stack<Integer>();
Stack<Character> ops = new Stack<Character>();
for (int i = 0; i < tokens.length; i++)
{
if (tokens[i] == ' ')
continue;
if (tokens[i] >= '0' && tokens[i] <= '9')
{
StringBuffer sbuf = new StringBuffer();
while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9')
sbuf.append(tokens[i++]);
values.push(Integer.parseInt(sbuf.toString()));
}
else if (tokens[i] == '(')
ops.push(tokens[i]);
else if (tokens[i] == ')')
{
while (ops.peek() != '(')
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
ops.pop();
}
else if (tokens[i] == '+' || tokens[i] == '-' ||
tokens[i] == '*' || tokens[i] == '/')
}
while (!ops.empty())
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
return values.pop();
}
public static boolean hasPrecedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}
public static int applyOp(char op, int b, int a)
{
switch (op)
{
case '+':
return a + b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
UnsupportedOperationException("Cannot divide by zero");
return a / b;
}
return 0;
}
public static void main(String[] args)
{
System.out.println(EvaluateString.evaluate("16 + 2 * 5"));
System.out.println(EvaluateString.evaluate("100 * ( 2 + 16 )"));
System.out.println(EvaluateString.evaluate("100 * ( 2 + 16 ) / 12"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.