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 .. I 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