Closed

# JAVA and C arithmetic resolver

This project was awarded to poiu060 for \$175 USD.

\$30 - \$250 USD
24
###### Project Description

The goal of this project is to write from scratch a function that can resolve arithmetic equations. This solution will be used in a software interpreter.

In each language we will support the following variable types: INTEGER, DOUBLE

(More types will be available but for the purpose of this exercise only the two listed above should be used)
INTEGER must always be treated as a 64-bit integer (int_64t)
DOUBLE must always be treated as a 64-bit double-precision floating point decimal value (double in both C and Java)

In each language, a variable will be defined as follows
(Java)
public enum varTypes
{
INTEGER, DOUBLE
};
public enum varErrors
{
ENOERROR, ENOTFOUND, EOVERFLOW, EDIVBYZERO
};
public class sVariable
{
public varTypes vType; // stores the current type
public varErrors vError; // stores the current error
public int64_t intVal; // stores the integer value if integer
public double dblVal; // stores the double value if double
};

(C)
enum varTypes
{
INTEGER, DOUBLE
};
enum varErrors
{
ENOERROR, ENOTFOUND, EOVERFLOW, EDIVBYZERO
};
struct sVariable
{
varTypes vType; // stores the current type
varErrors vError; // stores the current error
int64_t intVal; // stores the integer value if integer
double dblVal; // stores the double value if double
};

Your goal is to write a function using the following definition:
(C)
struct sVariable resolver(char *equation, varTypes resultType);
(Java)
static public sVariable resolver(String equation, varTypes resultType);

Your function must parse the equation and return a value in the desired type.

Your function MUST support addition (+), subtraction (-), multiplication (*), division (/), parentheses (), exponentiation (^) and modulus (%)

The equation may contain variables, eg
"8*pi"
For each variable, your function must retrieve the value and type of the variable using this function:
(C) struct sVariable getVariable(char *varName);
(Java) static public sVariable getVariable(String varName);
If these functions return any error code (ENOTFOUND, EOVERFLOW or EDIVBYZERO); your function must immediately return the same error.

Your function should first determine the type of each variable and value in the equation; if the equation only contains integers then the equation must be resolved using only integers (eg 5/2 = 2). However, if any of the values in the equation OR the requested RESULT variable are a float, then the entire equation must be evaluated as float (eg requesting a floating result: 5/2 = 2.5)
If the equation contains floating values but the requested result type is integer, the function should be evaluated as floating and then cast to int (by rounding down/floor) before returning the result.

Your solution must return EOVERFLOW or EDIVBYZERO using the provided error value in sVariable when either case (division by zero, and integer overflow) occur (where possible). Your function must not leave any unhandled exceptions.

1: ()
2: ^
3: * and % and /
4: + and -

Your function will not need to handle any embedded functions (cos(), sin()...); in these cases the interpreter will evaluate them before proceeding.
For example in the equation:
"8*2+cos(pi/2)"
"pi/2"
You will resolve pi by calling getVariable("pi") (for this example [url removed, login to view]); your function will return [url removed, login to view]