Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract divisors from an expression

Status
Not open for further replies.

jalbao

Programmer
Nov 27, 2000
413
US
I'm having difficulty coming up with an algorithm that will extract all
the divisors of a given expression.

For example...

Input: X * 5 + (Y / ((10 / T) + 15 / W - 20) / 25

Output:
divisor 1 -> ((10 / T) + 15 / W - 20)
divisor 2 -> T
divisor 3 -> W
divisor 4 -> 25

It may be worth noting that the order of the output result doesn't need
to correspond with the order of operations in the given input. So, as
far as I'm concerned, the output result could also be:
divisor 1 -> 25
divisor 2 -> W
divisor 3 -> T
divisor 4 -> ((10 / T) + 15 / W - 20)

The sole reason that I'm in need to extract the divisors of a given
equation is, I need to trap for divide-by-zero prior to evaluating the
expression.

The fact that the expression can contain divisors within divisors
within divisors ad nauseam makes this a difficult puzzle (for me,
anyway).
 
Why not use a try catch and catch the zero division?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
The expression will actually be a part of a SQL select statement. In the WHERE clause of the statement I need to set the divide-by-zero conditions.

Something like the following:

SELECT 8 / (T + 4) - 9/Y
FROM table....
WHERE
(T + 4) <> 0
AND
Y <> 0
 
... I forgot to mention in the previous post -

If I can create an algorithm that will return a list of divisors derived from the given expression, I can easily create the WHERE clause of the SQL statement.
 
Well what you need is a recursive descent parser since you can have an arbitrary level of nesting. Google can help you out with the theory.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top