1 Attachment(s)
how to make a program that does the following arithmetic expressions ?
how to make a program that does the following arithmetic expressions?
x+ y , x-y ، x/y ، x*y ، x%y , x > y , x < y
i would like to make the user to inputs the expression and program to show the answer.
here is an example:
Attachment 849
Re: how to make a program that does the following arithmetic expressions ?
If your expressions are only 2 operands and an operator, then you should be able to parse the three parts of the expression and evaluate it. If the expression is more complicated, You need to create/find an expression parser.
Re: how to make a program that does the following arithmetic expressions ?
Quote:
Originally Posted by
Norm
If your expressions are only 2 operands and an operator, then you should be able to parse the three parts of the expression and evaluate it. If the expression is more complicated, You need to create/find an expression parser.
how to parse the three parts ,, individually x,<,y
can u give an example from 1 of the expressions above
Re: how to make a program that does the following arithmetic expressions ?
There are several ways to do it. I have only used a couple and probably not the easiest ones to use.
The easiest way would be if there were spaces delimiting each of the three parts, then you could use the String class's split() method. That would be the easiest.
Another would be to use the String class's charAt() method to look at each character at a time and find the three substrings for the three parts.
Another way is the StreamTokenizer class. There will be some more to learn to get this one to work.
Another way could be to use regular expressions. I'm not very good with them, but I think they could provide a way to separate the String into its three parts.
Re: how to make a program that does the following arithmetic expressions ?
Quote:
Originally Posted by
Norm
There are several ways to do it. I have only used a couple and probably not the easiest ones to use.
The easiest way would be if there were spaces delimiting each of the three parts, then you could use the String class's split() method. That would be the easiest.
Another would be to use the String class's charAt() method to look at each character at a time and find the three substrings for the three parts.
Another way is the StreamTokenizer class. There will be some more to learn to get this one to work.
Another way could be to use regular expressions. I'm not very good with them, but I think they could provide a way to separate the String into its three parts.
how to do it with the string class split() method ?
can you do an example for me ?
Re: how to make a program that does the following arithmetic expressions ?
Do a Search on the forum for code samples. Or use Google.
Re: how to make a program that does the following arithmetic expressions ?
i know to do a split method, but after splitting the parts how do i take each and parse ?
that's what i'm not getting till now
Re: how to make a program that does the following arithmetic expressions ?
Take a look at some methods in Integer class, particularly valueOf for retrieving the numbers, then get the particular operator using the methods Norm already talked about, and print the returned number.
Note that making a fullblown effective calculator requires a lot of work if the numbers get large or there are more than two numbers, but I'm certainly happy to see you doing it :). However there is no one-line way to solve arithmetic equations in java. What I have seen however is people using command prompt or something to call an OS calculator, but I'm not sure how they did that.
Integer (Java Platform SE 7 )
Re: how to make a program that does the following arithmetic expressions ?
When you parse: x + y
you get the three parts of the expression.
Sorry, I'm confused about where your problem is. If you have split the expression into its three parts, then you can evaluate it directly.
By parsing I mean splitting the expression up into its parts: operands and operator.
Re: how to make a program that does the following arithmetic expressions ?
Quote:
Originally Posted by
Norm
When you parse: x + y
you get the three parts of the expression.
Sorry, I'm confused about where your problem is. If you have split the expression into its three parts, then you can evaluate it directly.
By parsing I mean splitting the expression up into its parts: operands and operator.
what i mean by parsing is like when using JOptionPane.showInputDialog to take numbers from the user and then parsing them into an integer or double,
since i'm going to use a string and split it into 3 parts ( how to declare each part as a variable )
Re: how to make a program that does the following arithmetic expressions ?
Do you want to do it like
Code :
Popup appears: "Enter first number"
User enters 30.
Popup appears: "What operator?"
User enters +
Popup appears: "Enter second number"
User enters 26
Popup appears: "56"
because that would be easy, however that isn't what your pictures show.
Again, to find an Integer or Double, use there valueOf methods
Integer (Java Platform SE 7 )
Double (Java Platform SE 7 )
Re: how to make a program that does the following arithmetic expressions ?
The valueOf methods return objects, the parseXXX methods return primitives.
Re: how to make a program that does the following arithmetic expressions ?
ok i got it now ,, thanks for the big help Norm & Tjstretch
Re: how to make a program that does the following arithmetic expressions ?
@Norm Yeah but it sounds coolor on valueOf, parse sounds all offiicial :P Go autounboxing!