Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Freelance Programming Jobs


You are currently viewing our Freelance Programming Jobs as a guest. Please register to participate.
Login



 
Easy JAVA job - Quick $1.44 @ paypal instant + $10 echeck @ paypal
Old 12-15-2006, 04:22 PM Exclamation Easy JAVA job - Quick $1.44 @ paypal instant + $10 echeck @ paypal
Experienced Talker

Posts: 32
Trades: 0
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
locazo is offline
View Public Profile
 
 
Register now for full access!
Old 12-30-2006, 04:03 PM Lightbulb Re: Easy JAVA job - Quick $1.44 @ paypal instant + $10 echeck @ paypal
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
I know I'm beyond your time frame for the answer but, I figure the problem is more interesting than the money.

I think the path using regular expressions is going to turn out to be overly painful.
This is the type of thing often done by compiler developers. The normal method for approaching this is to convert your infix expression to postfix. Postfix is easier for a computer to deal with.

If you google for "infix postfix" you will find volumes of information going into explicit detail.

I've posted a bit of code on my site that should get you going.
http://willcode4beer.com/design.jsp?set=evalInfix
It only deals with integers but, if you want to work with decimals, it should be an easy change.

The page also has a link to a jUnit test to demonstrate usage.

enjoy
willcode4beer is offline
View Public Profile
 
   

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.63222 seconds with 12 queries