LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2008, 09:47 PM   #1
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Rep: Reputation: 41
how to solve equation from string in java?


ok, i what i want is for the user to be able to input 3 numbers, and if there is an operation in one of those, it will solve it. like this:

type 3 numbers:
1/2,1/3,1/4

and it will output:
.5,.3333333,.25

i can make the user's input go to a string, then that string is broken by commas into a string array, then convert to a double, then output a number. i can't get it to parse equations within the string array

here is a section of it:
Code:
public static void quad()
{
Scanner quad;
double root1, root2, div, neg, dis, meat1, vertexx, vertexy;
root1 = 0.00;
root2 = 0.00;
System.out.print("\n\nWelcome to the Quadratic equation solver!\n\nEnter a values for a, b, and c seperated by commas\nRemember that y = ax\u00b2 + bx + c\n:");
quad = new Scanner(System.in);
String form = quad.nextLine();
String[] quadratic = form.split(",");
double[] quadtab = new double[quadratic.length];
for (int i = 0; i < quadratic.length; i++) 
{
quadtab[i] =  
quadtab[i] = Double.parseDouble(quadratic[i]);
}

thanks,
rabbit2345 ^_^
 
Old 10-29-2008, 05:45 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
How complex equations do you want to support? Just +-*/? Or (),%,**,&,|,^,?: as well? Or ...

This is the kind of job that parser generators are meant to solve. I think it the java world, two names to look for are sablecc and antlr.

What I doesn't know, because I don't know how complex equations you want to handle, is whether you'd be shooting avian carriers with heavy artillery. Maybe something simple could work. Like, say, splitting the string over all four of +-/*, then parseDouble'ing the numbers that are left and shoving the doubles and operators into an array. Then, for each * or / in the array from left to right, evaluate it (that is, "pop" its two neighbours, multiply or divide them, and reinsert the result). Then, evalute each + and -, again from left to right.

If you go the parser generator route, you have an opportunity to learn about regular expressions and context-free grammars if you don't know those already.
 
Old 10-29-2008, 01:30 PM   #3
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Post

You could create an array of operators and then check each string using String method contains() like:
String [] ops = {"+", "/", etc.} ;
for (int i = 0; i < ops.length; i++)
if (quadtab[i].contains(ops[i])){
String [] operands = quadtab[i].split(ops[i]]);
//convert string to number type and process operands using found operator type
break;
}
 
Old 10-29-2008, 06:48 PM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
//convert string to number type and process operands using found operator type
Wouldn't that require recursion? If not, and I feed you "1-2 + 3*4", you're going to split on the "+" and parse "1-2 " and " 3*4" as a number... It sounds like you haven't planned all the way to the endgame.

It's easy when you can leave the hard parts in the comments
 
Old 10-29-2008, 06:58 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by jonaskoelker View Post
Like, say, splitting the string over all four of +-/*, then parseDouble'ing the numbers that are left and shoving the doubles and operators into an array. Then, for each * or / in the array from left to right, evaluate it (that is, "pop" its two neighbours, multiply or divide them, and reinsert the result). Then, evalute each + and -, again from left to right.
Binary expression trees do a good job of this, and it's very simple so long as you have a binary tree to work with and an adequate text parser.
http://www.cs.utexas.edu/~lavender/c...lecture-24.pdf
http://courseweb.sp.cs.cmu.edu/~cs20...lecture18.html
http://www.codeproject.com/KB/recipe...pressions.aspx

ta0kira

Last edited by ta0kira; 10-29-2008 at 06:59 PM.
 
Old 10-29-2008, 07:34 PM   #6
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Quote:
Wouldn't that require recursion? If not, and I feed you "1-2 + 3*4", you're going to split on the "+" and parse "1-2 " and " 3*4" as a number... It sounds like you haven't planned all the way to the endgame.
LOL, yeah, but I tend to stick to the KISS principle. From the description, it didn't look as if anything more complex was needed. Besides, I didn't want to do anyone else's homework without them posting their credit card number.

Last edited by jay73; 10-29-2008 at 11:34 PM. Reason: grammar
 
Old 10-29-2008, 07:43 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If it actually is homework then OP probably already has some slides very similar to the ones I linked to.
ta0kira
 
Old 10-29-2008, 10:31 PM   #8
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
n/m, ignore

Last edited by abolishtheun; 10-29-2008 at 10:33 PM.
 
Old 10-31-2008, 05:43 PM   #9
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Original Poster
Rep: Reputation: 41
no, no. its not my homework. besides, im not old enough to have a credit card yet.

but i am curious, is there a way to split it multiple ways and solve?
like jonaskoelker said, what if someone put in 1+2*3? that would split to 1+(2*3)
 
Old 11-01-2008, 09:46 AM   #10
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Sure, you just have more things to check for. There are various ways of doing so but basically, you have your code pick out the pieces that have the highest priority, process them, then the ones the have the next highest priority, etc. Obviously, it is up to you to instruct the computer on the priority rules and on the rules that determine what constitutes and operand and what is an operator.
 
Old 11-01-2008, 03:26 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jay73 View Post
LOL, yeah, but I tend to stick to the KISS principle. From the description, it didn't look as if anything more complex was needed. Besides, I didn't want to do anyone else's homework without them posting their credit card number.
Be constructively lazy:
  1. wrap the expression to be evaluated into simple Java code;
  2. compile and link the code "on the fly";
  3. execute it in order to get the expression value.

This recipe works for any language that allows compilation and linking with already running program, e.g. Perl, Java, Python, etc.
 
Old 11-02-2008, 05:27 AM   #12
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
The approach to solve this requirement would be:

1. Create methods for each of the operators you plan to support.

2. Get all the inputs from command line

3. Break all the numbers and the operators (+, -, /, %, etc) sequentially. You would now have a list of input params and operators.

4. Once you have the input params and operators, invoke the requisite methods (you can use switch case logic to do so) - and pass them the requisite params.

5. Get the output of the math module of your program and display it on screen.

6. You now have a working application that does what you wanted it to do!
(Yaaaaaaaaay ... Clap ... Clap !!!)

Hope I've been of some assistance to you!


Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:50 AM.
 
Old 11-02-2008, 05:30 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by nishamathew1980 View Post
The approach to solve this requirement would be:

1. Create methods for each of the operators you plan to support.

2. Get all the inputs from command line

3. Break all the numbers and the operators (+, -, /, %, etc) sequentially. You would now have a list of input params and operators.

4. Once you have the input params and operators, invoke the requisite methods (you can use switch case logic to do so) - and pass them the requisite params.

5. Get the output of the math module of your program and display it on screen.

6. You now have a working application that does what you wanted it to do!
(Yaaaaaaaaay ... Clap ... Clap !!!)

Hope I've been of some assistance to you!
I think you are missing the fact that traditionally '*', '/', '%' operations have higher precedence that '+', '-'.

And I also think you are also missing the fact that people might decide to use parenthesis.
 
Old 11-02-2008, 08:00 AM   #14
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

The page http://en.wikipedia.org/wiki/Reverse_Polish_notation can be useful. It discusses RPN as well as a means of converting from infix notation. That scheme is what (some) compilers have used for arithmetic expressions ... cheers, makyo
 
Old 11-02-2008, 08:53 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by makyo View Post
Hi.

The page http://en.wikipedia.org/wiki/Reverse_Polish_notation can be useful. It discusses RPN as well as a means of converting from infix notation. That scheme is what (some) compilers have used for arithmetic expressions ... cheers, makyo
Sure, but what for - if Java parser already does this ?

The point is not to write code if it's already available.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
plz help with JAVA String thtr2k Programming 3 11-03-2007 01:04 AM
How to encrypt a string in Java me4linux Programming 1 09-03-2007 03:30 PM
JAVA: Cleaning up an ugly equation Garda Programming 7 08-07-2007 05:59 PM
how to solve Character and String problems on linux?(screenshot) tradingbr Linux - General 0 04-29-2004 02:51 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration