# Scary programming question

Watch
Announcements
#1
eek ;/

Write a program that recursively parses expressions, input as strings, from the following recursively defined language and calculates and prints out the answer to the calculations.

<EXP> = + <DIGIT> <EXP> | - <DIGIT> <EXP> | &<EXP> | <DIGIT>
<DIGIT> = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Here EXP stands for expressions, + means do addition, - means subtract and & means calculate the sum up
to the given number. Legal expressions in this language involve putting the operator before its arguments (this is called Polish notation). Instead of writing 1+2, in this language you write +12. Notice only single digits are allowed and spaces are not allowed. Further legal expressions can be seen below.

An example run of the program (characters in bold are typed in by the user and pop-up boxes may be used
for input and output):
Another example run:
Another example run:
Another example run:
Another example run:
Make sure you split the program in to multiple methods, and use a series of recursive methods following the
structure of the recursive definition about expressions. You must not use an explicit loop at all in your
program. Comment your program with useful comments that give useful information, with every method
commented with what it does, use indentation consistently and ensure that your variable names convey
useful information. Your variables should be positioned so that their scope is small.
0
1 month ago
#2
what have you tried?
0
#3
(Original post by MalcolmX)
what have you tried?
Tried to understand polish notation a little better, an example of factorials using recursion methods (not sure how relevant), and a simple idea of recursion where counting up to a number is the aim. I get why a loop isn't to be used because the recursive methods are meant to take on that job but all the expression stuff really puts me off the question.

I see you're a fan of Malcolm X, you don't get many like Malc
0
#4
bump
0
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Should there be a new university admissions system that ditches predicted grades?

No, I think predicted grades should still be used to make offers (713)
33.89%
Yes, I like the idea of applying to uni after I received my grades (PQA) (901)
42.82%
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (397)
18.87%
I think there is a better option than the ones suggested (let us know in the thread!) (93)
4.42%