I want to design a special calculator .. and I came to a problem like this : X=1+(12*4+2) i need to get number of operands first like here i have two operands 1 and (12*4+2) how could i distinguish between the outer + and the inner one ?
thanks
what an amazing community here .. different answers from easiest to hardest .. guyz my problem is not calculator nor anything else related to math .. 开发者_如何学GoI just asked about the outer and inner plus to apply the strategy in a completely different thing .
I am implementing unification algorithm in java (just like what Prolog interpreter does when you give it two expressions )
here is the algorithm :
function unify(E1, E2);
begin
case
both E1 and E2 are constants or the empty list:
if E1 = E2 then return {}
else return FAIL;
E1 is a variable:
if E1 occurs in E2 then return FAIL
else return {E2/E1}
E2 is a variable
if E2 occurs in E1 then FAIL
else return {E1/E2}
either E1 or E2 are empty then return FAIL
otherwise:
begin
HE1 := first element of E1;
HE2 := first element of E2;
SUBS1 := unify(HE1, HE2);
if SUBS1 := FAIL then return FAIL;
TE1 := apply(SUBS1, rest of E1);
TE2 := apply(SUBS1, rest of E2);
SUBS2 := unify(TE1, TE2);
if SUBS2 = FAIL then return FAIL;
else return composition(SUBS1, SUBS2)
end
end
now my question is if I have such input : a(X,Y)=a(b(c,Y),Z)..
how could I extract number (and values)of the elements (i.e. X and Y for the first Expression )
I have come to different new techniques for me when i read and try to solve this .. like Lexical Analysis,Parsing I have no clue about Lexical Alanysis though I know parsing and tokens (in String manner) moreover i think it's not gonna solve my problem .. I am now trying to implement what Joey Adams said .. I think it's useful to my problem..
soory for this essay guyz ... appreciate you help
Your question is very general, and a complete answer could take up several chapters in a book.
That being said, I would suggest you start by Googling for some terms and learning about:
Tokenizing and Parsing (extracting the individual components from the string.)
Infix Evaluation (taking a pair of operands and an intervening operator and storing an answer)
Postfix Evaluation (taking a pair of operands and a following operator -- this won't make sense yet, but probably will after you've read a bit.)
.. and for further reading:
Compiler Design
... and as you encounter specific questions, continue to visit this site. Don't forget to search for answers to questions similar to yours that have already been asked!
For parsing simple math expressions, I have a slightly unconventional but easy-to-implement solution: create a binary tree and push each token to the tree one by one. Example:
X
=
/
X
=
/ \
X 1
=
/ \
X +
/
1
=
/ \
X +
/ \
1 (
=
/ \
X +
/ \
1 (
|
12
=
/ \
X +
/ \
1 (
|
*
/
12
=
/ \
X +
/ \
1 (
|
*
/ \
12 4
=
/ \
X +
/ \
1 (
|
+
/
*
/ \
12 4
=
/ \
X +
/ \
1 (
|
+
/ \
* 2
/ \
12 4
=
/ \
X +
/ \
1 ( )
|
+
/ \
* 2
/ \
12 4
The algorithm for pushing is: place the new token to the right of the last inserted token as a child, then make it float up until it satisfies the order of operations (lower precedence goes above higher precedence, and nothing goes above an open parenthesis (i.e. '(') until it is closed (i.e. '( ... )').
Here's my implementation of this kind of parser in eC: http://www.funsitelots.com/pub/ecas0.zip . You'll need the Ecere SDK to build it. Look in form1.ec at the PushOperator method. Sorry if it's not the easiest code code in the world to read.
I would take a look at the Interpreter Design Pattern. Google that "pattern" with calculator and there are some decent hits, for example this one.
One approach you could take would be to generate a regular expression that would pull out the operatands and the operators.
You could also take a look at some formula engines such as JFormula(there are others as well) written in java. They will have their own syntax required for the formulas, and some parsing may still be involved, but you will be able to handle mathematical expressions and order of calculation through the engine.
This is a pretty bad solution, and please do what everyone else has suggested - read up on that pattern. However, just for the sake of getting this working, you might be able to do something like
1 - remove all decimals and periods from the input
2 - Use a regular expression to find / remove any parenthesis and the stuff in between them
3 - Use the remaining operations to guess at the number of operands
For example
Your input :
1+(12*4+2)
1 - remove all #'s and .'s
+(*+)
2 - remove all ()'s and internals
+
3 - guess number of operands
1 operation, so 2 operands
as I said, this is pretty bad, and will fail easily. However, it might be enough to get you started. After you use it for a bit you will start to spot the problems, and will hopefully start to understand why you need a more robust solution.
HTH
Here's a fantastic link that I found through researching @Bob Kaufman's answer:
users.cis.fiu.edu/~weiss/dsj2/code/Evaluator.java
This is just the code. Here is an detailed explanation (although it is using C++, but the logic is exactly the same): http://www.lawrence.edu/fast/greggj/CMSC270/Infix.html
精彩评论