ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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]);
}
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.
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;
}
//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
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.
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
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)
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.
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:
wrap the expression to be evaluated into simple Java code;
compile and link the code "on the fly";
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.
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 !!!)
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.