calculator issue

2019-08-04 23:20发布

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

标签: java parsing
7条回答
你好瞎i
2楼-- · 2019-08-04 23:58

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.

查看更多
做自己的国王
3楼-- · 2019-08-04 23:59

One approach you could take would be to generate a regular expression that would pull out the operatands and the operators.

查看更多
够拽才男人
4楼-- · 2019-08-05 00:07

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

查看更多
劳资没心,怎么记你
5楼-- · 2019-08-05 00:08

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!

查看更多
来,给爷笑一个
6楼-- · 2019-08-05 00:08

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

查看更多
beautiful°
7楼-- · 2019-08-05 00:21

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.

查看更多
登录 后发表回答