JAVA and C arithmetic resolver
This project was awarded to poiu060 for $175 USD.Get free quotes for a project like this
Project Budget$30 - $250 USD
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
public enum varTypes
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
ENOERROR, ENOTFOUND, EOVERFLOW, EDIVBYZERO
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:
struct sVariable resolver(char *equation, varTypes resultType);
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
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.
Your function must follow the proper mathematical order for operations:
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:
Your function will first receive
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]
Your function will then receive from the interpreter:
"8*2+[url removed, login to view]"
Your function must return [url removed, login to view]
You may not use any existing code, and you must not use any form of in-language evaluation (such as 'eval'). You must not use any third-party library or tools; in C you may only use functions found in the standard C library. In Java your function will be used on multiple platforms including but not limited to Windows, Android, Linux and J2ME, and must compile under these platforms with no external or additional requirements.
We also want both done, we want the above done in C and separately in Java. Bid is for both.
Looking to make some money?
- Set your budget and the timeframe
- Outline your proposal
- Get paid for your work
Hire Freelancers who also bid on this project
Looking for work?
Work on projects like this and make money from home!Sign Up Now
- The New York Times
- Wall Street Journal
- Times Online