-
- Forum
- General Discussion
- Algorithms & Data Structures
- arithmetic expression tree
Thread: arithmetic expression tree -
March 10th, 2007,09:20 PM #1 arithmetic expression tree I have a problem which I can't figure out I need to draw an arithmetic expression tree that has four external noders,storing the numbers 1,5,6, and 7 (with each number stored in a distinct external node, but not necesarily in this order), and has three internal nodes, each storing an operator from the set {+,-,*,/} so that the value of the root is 21. The operators may return and act on fractions, and an operator may be used more than once. I think I tried almost all options and I can't get those four numbers to equal to 21 Some tips would be good -
March 10th, 2007,10:28 PM #2 Re: arithmetic expression tree I created a nested loop in VB, and these all came out to 21 EDIT: Found it 43 times! Code: Option Explicit ' Add a reference to Microsoft Script Control 1.0 Public sc As New ScriptControl Private Sub Form_Activate() Dim n As Variant, o As Variant, tot As Integer Dim x0 As Integer, x1 As Integer, x2 As Integer, x3 As Integer Dim y0 As Integer, y1 As Integer, y2 As Integer, y3 As Integer Dim ct As Integer ct = 1 n = Array(1, 5, 6, 7) o = Array("+", "-", "/", "*") sc.Language = "vbScript" For x0 = 0 To 3 For x1 = 0 To 3 For x2 = 0 To 3 For x3 = 0 To 3 For y0 = 0 To 3 For y1 = 0 To 3 For y2 = 0 To 3 tot = sc.Eval(n(x0) & o(y0) & _ n(x1) & o(y1) & _ n(x2) & o(y2) & _ n(x3)) If tot = 21 Then Debug.Print ct, ": ", n(x0) & o(y0) & _ n(x1) & o(y1) & _ n(x2) & o(y2) & _ n(x3) ct = ct + 1 End If Next y2 Next y1 Next y0 Next x3 Next x2 Next x1 Next x0 End Sub Private Sub Form_Load() ' 1,5,6,7 *,/,+,- End Sub Here: 1 : 1+5*5-5 2 : 1-5+5*5 3 : 1+6+7+7 4 : 1+7+6+7 5 : 1+7+7+6 6 : 1*7+7+7 7 : 5*5+1-5 8 : 5*5-5+1 9 : 5+5+5+6 10 : 5*5*5/6 11 : 5+5+6+5 12 : 5*5/6*5 13 : 5*5*6/7 14 : 5*5/7*6 15 : 5+6+5+5 16 : 5/6*5*5 17 : 5*6*5/7 18 : 5*6/7*5 19 : 5/7*5*6 20 : 5/7*6*5 21 : 5*7-7-7 22 : 6+1+7+7 23 : 6+5+5+5 24 : 6*5*5/7 25 : 6*5/7*5 26 : 6+7+1+7 27 : 6/7*5*5 28 : 6+7+7+1 29 : 7+1+6+7 30 : 7+1+7+6 31 : 7+1*7+7 32 : 7/1+7+7 33 : 7*1+7+7 34 : 7*5-7-7 35 : 7+6+1+7 36 : 7+6+7+1 37 : 7+7+1+6 38 : 7+7+1*7 39 : 7+7/1+7 40 : 7+7*1+7 41 : 7+7+6+1 42 : 7+7+7/1 43 : 7+7+7*1 Last edited by dglienna; March 10th, 2007 at 11:07 PM. David CodeGuru Article: Bound Controls are Evil-VB6 2013 Samples: MS CODE Samples CodeGuru Reviewer 2006 Dell CSP 2006, 2007 & 2008 MVP Visual Basic If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post! -
March 11th, 2007,02:39 AM #3 Re: arithmetic expression tree Yes it works great, but I thought I have to use all four operands only once, but the operators I can use multiple times? And this is where my problem was. -
March 11th, 2007,03:00 AM #4 Re: arithmetic expression tree I just read what you wrote: The operators may return and act on fractions, and an operator may be used more than once. David CodeGuru Article: Bound Controls are Evil-VB6 2013 Samples: MS CODE Samples CodeGuru Reviewer 2006 Dell CSP 2006, 2007 & 2008 MVP Visual Basic If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post! Posting Permissions - You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
- BB code is On
- Smilies are On
- [IMG] code is On
- [VIDEO] code is On
- HTML code is Off
Forum Rules | Click Here to Expand Forum to Full Width |
0 Response to "draw an arithmetic expression tree that has four external nodes"
Post a Comment