# Expression Algorithm

## Recommended Posts

Hello,

Imagine that you have a string of instructions, like:

`FuncA(param1, 56, FuncB( (56+78) / 4, 3.14), "Text...", 2 + FuncC( -6 )/ 4)`

And you want to execute it in correct order. The first you should add 56 to 78. Then divide the result by 4 etc.

I say only that I need it to make special calculator. If you need know more to help me, tell me.

I'd like only know how to do it. Just tell me your idea for the fast and smart way. I don't wait for full code. Never!

I might also add that I am a veteran of programming. So don't talk me about Execute function and similar, please. I don't want them.

PS: Thank you for the welcome...

#Edit: Now I know what I'm doing. I'm looking for an arithmetic algorithm expression.

Edited by Mharsy

##### Share on other sites

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

##### Share on other sites

I'd like to manually process the code. To do this, I'm looking for some idea for getting the order of operations, like:

1/ (a + b) * c - d / e

2/ ( r ) * c - d / e

3/ r * c - d / e

4/ r - d / e

5/ r - s

where r and s are results of mathematical operations.

Edited by Mharsy

##### Share on other sites

Mmmmh. I also don't understand your problem Mharsy / what do you wan't to achive. Some example available?

##### Share on other sites

I want to create a calculator with additional functions, but I've no idea where to start the main process of  numbering.

Example

##### Share on other sites

From reading your original post and looking at the function call you posted, you want to pass to FuncB the calculated result (56+78) / 4 and another parameter of 3.14, correct? That would work, but what are you trying to achieve from doing that is the question.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

Hi Mharsy
you might start (or get some ideas) from >this post
bye

edit:

or even >this

Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

From reading your original post and looking at the function call you posted, you want to pass to FuncB the calculated result (56+78) / 4 and another parameter of 3.14, correct? That would work, but what are you trying to achieve from doing that is the question.

I have already said. I try to create a calculator. Imagine that your program finds the first operation in hierarchy and writes it in words. And repeats it to the end. For example:

Input:                         Output:

A + B / ( C - D )    Subtracts D from C.

A + B / ( R )          Removes parentheses.

A + B / R                     Divides A by a result.

A + R                         Adds A to a result.

Hi Mharsy

you might start (or get some ideas) from >this post

bye

edit:

or even >this

Thanks for trying to help. I checked two codes. Both are only improved version of Execute function.

I think you aren't able to help me. If no one answers me in the near future, I'll close the thread.

##### Share on other sites

So, you want to achieve an expression solver?
something similar to (or better this) where you enter an expression, and it gives you back all the necessary steps to resolve it?

Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

So, you want to achieve an expression solver?

something similar to this site, where you enter an expression, and it gives you back all the necessary steps to resolve it?

Yes. This is exactly what I need. I've just analyzed resources of the page. The main scripting is here. Unfortunately, it's not large and all operations are executed at jQuery. It's included by this script. As you can note, all the code is written in one line, so it's not readable for a human eye. Of course, there are specials program and websites, which convert such code to more friendly for us. But jQuery is only JavaScript library, so all calls are directed deeper and we still get nothing.

##### Share on other sites

now I understand the question, but unfortunately I do not have the answer.

analyzing a little this interesting site, I saw that if you use the link below,  and you add as parameter the expression that you want to solve (properly "escaped"), the resolution of the expression is returned back;

this is the expression: (x-3)*(x^6-2*x^3+1)=0

this is the link with the expression added as parameter (properly escaped):

http://www.algebra.com/services/rendering/simplifier.mpl?expression=%28x-3%29%2A%28x%5E6-2%2Ax%5E3%2B1%29=0

whereas if you use this other link added with the same parameter as the above, it will return the formula in graphical form:

http://formula.algebra.com/cgi-bin/plot-formula.mpl?expression=%28x-3%29%2A%28x%5E6-2%2Ax%5E3%2B1%29=0

maybe you can get something analyzing that site (or by sending an e-mail to the site manager)

however,
your question should be divided into two parts:
the first part, not closely related to autoit, is to find the '"algorithm" for the expressions resolution (I think the most difficult part)

then, when you have this algorithm, it will surely be an easy step implement it in autoit (easy part)

good luck

Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

I tested this site and in my opinion it's not finished yet. The script has problems with basic expression from my book of mathematical. Check
x^5-8x^3+16x or x^4+4. For the first result is x(x-2)^2(x+2)^2 and (x^2 - 2x + 2)(x^2 + 2x + 2) for the second. Meanwhile you won't find a result there.

Yeah, now I think my question was badly formulated. The main fault is my English.

Everything what I need is a "expression algorithm" or something similar. I've searched a little for this and I've found many articles. Most of them are written in 90s. The best for me is this. If you find something interesting then post it here, please.

##### Share on other sites

http://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions

edit:

free book's text from Niklaus Wirth (the inventor of Pascal and Modula-2)

Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

Ok, you may not think it but you actually want to google "compiler construction" and get reading. Doesn't sound relevant but a lot of thought has gone into exactly what you are trying to do, and there are a lot of ways to do it. It's something I've been interested in for a long time.

I won't go into much detail because there are a lot of really good resources out there, only thing I'll say is that AutoIt is not a particularly good language for this, it's possible but you won't be able to do it that cleanly. Just to see how messy it would be I wrote a couple of very quick scripts for a lexer and parser. You could definitely do them better than that, but compare it to just how simple the same parser is written in another language, like C# or C++. ASTs are very well suited for use in OOP, you can have a generic branch or leaf class that has a method Evaluate or Simplify or whatever you want to do on the tree, and then all the different types of nodes can override that method. C# in particular was very easy to code this in.

All the examples above use a Top Down Operator Precedence Parser, which is a sortof recursive descent parser from a paper by a guy called Pratt. I love it, particularly for maths expressions (which is what I do mostly) it is very simple to implement initially, and then very easy to incrementally extend (something that is very important for big projects and testing). Again it lends itself very well to OOP rather than procedural programming.

Good luck. You've chosen a very interesting topic to look into, where you will learn a lot of techniques that will make you a better programmer, and a couple of bits of theory you will use elsewhere. Two years ago on one of my first paid jobs writing control software the guy I was working with couldn't understand why I knew all about finite state machines for example.

Do the research, if you need any specific help then send me a pm. I'm doing 60+ hours a week at university at the moment so I can't offer you much time, but I know from experience that a human can adapt their teaching to be more specific to an encountered problem, as opposed to a book which if you don't understand won't change how it explains something.

Speaking of books, the dragon book is what I read initially. It covered everything, including a lot of stuff I haven't really looked into like tools to do most of the stuff for you. It was definitely well worth getting, and is still sitting on my shelf next to some of the more chunky reference books now.

##### Share on other sites

From your previous post, you are not evaluating expressions but factoring polynomials. Consider using Wolfram Alpha or lower quality Web service.

For instance:

Factor[x^10 - y^10]

(x - y) (x + y) (x^4 - x^3 y + x^2 y^2 - x y^3 + y^4) (x^4 + x^3 y + x^2 y^2 + x y^3 + y^4)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Mat,

your message is very helpful for me. Another step forward. I reviewed your links and I still stay with AutoIt. Also I will google for a few words from your post. I'm much better reader than writer. Here is a screen of the my project. It has grown to large sizes.

I think I can now continue work on the my project with an increased knowledge. The request is still valid. If you finds something interesting, feel free to post this. With no reply for longer time, I will close thread.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...