Solved

Need code that adds brackets to multi-leveled expressions (group by indentation levels)

Posted on 2004-04-18
6
266 Views
Last Modified: 2010-04-17
I need a simple and elegant algorithm that will add properly
positioned brackets between 2 or more multi-leveled expressions.
The diagram below shows exactly what I am looking for:

!! NOTE: VIEW IN FIXED FONT SUCH AS COURIER NEW!!

=============================================================
8 POSSIBLE SCENARIOS FOR 3 EXPRESSIONS USING UP TO 2 LEVELS
=============================================================
               (1)   (2)   (3)   (4)   (5)   (6)   (7)   (8)
              +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+
GROUP LEVEL:  |1|2| |1|2| |1|2| |1|2| |1|2| |1|2| |1|2| |1|2|
              +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+
EXPRESION A:  |A| | |A| | |A| | |A| | | |A| | |A| | |A| | |A|
              +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+
EXPRESION B:  |B| | |B| | | |B| | |B| |B| | |B| | | |B| | |B|
              +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+
EXPRESION C:  |C| | | |C| |C| | | |C| |C| | | |C| |C| | | |C|
              +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+ +-+-+
=============================================================
BRACKET SOLUTIONS FOR THE 8 SCENARIOS SHOWN ABOVE
=============================================================
(1)       A  +  B  +  C
(2)       A  +  B  + (C)
(3)       A  + (B) +  C
(4)       A  + (B  +  C)
(5)      (A) +  B  +  C
(6)      (A) +  B  + (C)
(7)      (A  +  B) +  C
(8)      (A  +  B  +  C)

Things become more complicated as we add more levels and
expressions. Is there an elegant approach?.



Thanks


0
Comment
Question by:const71
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 10856313
This can be thought of as a mapping between the parentheses and binary numbers.

In your sample with 8 possibilities, the possible combinations are equal to 2^3 (two levels of three expressions).

You can also think of your expressions as binary numbers:

1. 000
2. 001
3. 010
4. 011
5. 100
6. 101
7. 110
8. 111

The pattern is everywhere there are sequences of 1, you'll need to put parentheses around things.
0
 

Author Comment

by:const71
ID: 10856318
I know that, but i need code that does this for me. I have some Visual Basic code as a starting point if that will help...
0
 

Author Comment

by:const71
ID: 10856360
Heres the code that I have so far

http://www3.sympatico.ca/cnterekas/EXPRESSION.ZIP


Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Accepted Solution

by:
sitbon earned 500 total points
ID: 10856928
Hi,

I didn't look at your code yet, but you need tp write a simple expression parser, just for the reason that adding even one more little thing will ruin the code if you don't. What you probably should do is start with a string that has a line of code in it. Strip everything but the characters (ie remove spaces and newlines). The easiest solution is a recursive one:

function evaluate( left, right, operator )
{
    if left has a " ( " then
        find first " ) " from the end (of hte left part), split in between
        left = evaluate( left->left, left->right, left->op )

    return operator->do_something(left,right)
}

This probably isn't the speediest, but you're probably not worried about performance (after all, you *are* using VB lol) so this is the simplest solution for you.

So for the expression

        A +  ( B + C )

the logic would be:

      evaluate( "A", "(B+C)","+") ->

          return add(  "A", add("B","C") )
                                    ^---- evaluate("B+C") -> return add->do_something("B","C")

and so on.

good luck! look at some scripting engine source code... should help.
0
 
LVL 2

Expert Comment

by:sitbon
ID: 10856964
>>    if left has a " ( " then
>>        find first " ) " from the end (of hte left part), split in between
>>        left = evaluate( left->left, left->right, left->op )

sorry, I should indicate that you need to do the same for the right side :)  This forms an expression tree:

X = A + ( B + (C + D) )

 Gives
 
        X
       / \
     /     \
   /         \
  A          +
             /  \
           /      \
         /          \
        B           +
                    /  \
                  /      \
                /          \
               C           D


as you can see, each node either has 2 children or none - nodes with two children are operators and nodes with none are operands. This binary tree allows for some efficient login operations, so you don't have to limit yourself to recursion. If it were me, I'd use a stack.
0
 

Author Comment

by:const71
ID: 10858646
great!  thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now