I need someone to finish a simple calculator code in JAVA in the next 2hours.
Basically, the input is a string with an operation on this format: "4+25+14/2*6-2+5"
Want I need it to solve this using the right order of operations:
The way it must be solve is trough extracting first 5 tokens (already done) from the string, assign them to variables, and solve according to the right order of operations, then extracting 2 more and continue solving until the string is empty and all the opperations are done. All these must be done with only 5 variables, meaning that you would need to do moving of values after each operation if necessary.
I'm looking for someone fast, I really need this in a couple of hours, As I said, about 45%+ of the code I already wrote.
Here's the code:
calculator.java
Code:
import java.util.*;
public class calculator
{
public String operString;
public float Num1;
public char Oper1;
public float Num2;
public char Oper2;
public float Num3;
public String tempStr;
public float tempResult;
private boolean isValidd(String s)
{
String validChars = "+-/*0123456789.";
boolean isValidd = true;
for (int i = 0; i < s.length() && isValidd; i++)
{
char c = s.charAt(i);
if (validChars.indexOf(c) == -1)
{
isValidd = false;
}
else
{
isValidd = true;
}
}
return isValidd;
}
public void eval()
{
String theRegex = "\\+|-|\\*|/";
String[] split = operString.split(theRegex); // get first 3 numbers to an array
Num1 = Integer.parseInt(split[0]);
Num2 = Integer.parseInt(split[1]);
Num3 = Integer.parseInt(split[2]);
tempStr = operString;
int conT = 1;
for(int i=0;i<tempStr.length();i++) // get opperator 1 to variables
{
if (conT == 1)
{
char a = tempStr.charAt(i);
if(a=='+' || a=='-' || a=='*' || a=='/')
{
Oper1 = a;
conT = 0;
}
}
}
tempStr = tempStr.substring(tempStr.indexOf(Oper1)+1, tempStr.length() ); // Get new String
conT = 1;
for(int i=0;i<tempStr.length();i++) // get opperator 2 to variable
{
if (conT == 1)
{
char a = tempStr.charAt(i);
if(a=='+' || a=='-' || a=='*' || a=='/')
{
Oper2 = a;
conT = 0;
}
}
}
tempStr = operString.substring(operString.indexOf(split[3])-1, operString.length() );
System.out.println(tempStr);
/*Continue Code here*/
public void input()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("\nPlease your operation in this format ex: 41+14*5+2/4");
operString = keyboard.nextLine();
operString = operString.replaceAll(" ", ""); //removes white spaces from string
}
public void output()
{
eval();
System.out.println(Result);
}
}
main.java (just to input and display results)
Code:
public class main
{
public static void main(String[] args)
{
calculator o1 = new calculator();
o1.input();
o1.output();
}
}
Whoever Finish the code 1st and PM, will be the winner. As I said in the title, I will give $1.44 intanlty to the winner, and also a $10 through and echeck which usually takes 3 or 4 days to clear out.
Thanks
|