Recursive program

• Question

• Hi everyone! I just started to learn C# and  I need help in recursive program.

Here is it:

Write a RECURSIVE program to solve this problem:
You are given an array of strings like this:
“8”
“+”
“9”
“*”
“4”
“*”
“2”
“-”
“20”
Each character is either a digit or an arithmetic operations +, -, *. (no
division)
Compute and return the the value of this mathematical expression using
RECURSION.
Example output: 60.

Monday, April 9, 2018 7:39 PM

All replies

• I need help in recursive program.

Here is it:

Write a RECURSIVE program to solve this problem:

We don't do assignments for students. Make an attempt at writing your own
code, and when you get stuck at some point ask here for suggestions to
overcome the problem. We'll help guide you through it, but we won't do
all the coding for you.

- Wayne

Monday, April 9, 2018 7:57 PM
• You can represent it as tree. Root of tree will be first +. Left leaf will be 8 and right leaf will be * and it will have two children (9 and *). Leaf with value * will have two leafs (4 and *), etc...

Evaluation is followed: You start with root of tree. There is value 8 + GetValue(RightNode). Value of right node is 9 * GetValue(RighNode), ...

It is very nice to use composite pattern for solution.

• Proposed as answer by Monday, April 9, 2018 8:10 PM
Monday, April 9, 2018 8:00 PM
• Specifically, an Abstract Syntax Tree. This should be the answer.
Monday, April 9, 2018 8:11 PM
• ok, Thanks :)
Monday, April 9, 2018 8:12 PM
• Thank you very much, I will try it
Monday, April 9, 2018 8:16 PM
• Hello Krishtikri,

Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

Best regards,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Wednesday, April 18, 2018 7:06 AM