Help with a school project?

Started by
2 comments, last by iMalc 19 years, 3 months ago
Hey everybody. I'm fairly new to programming, but I would like to learn more about it. I've been reading some tutorials and books, and I've been progressing through some "childish" stuff. I do have a question, though. I have an extra credit project in one of my classes, and it could raise my grade significantly. To an A, to be exact...even if I fail the final. I need to make a DOS executable. It needs to be able to take five number fives and use any symbol to obtain a certain number. So say I have 5-5+5+5+5. That obviously equals 15. Or say I have 5x5x5x5x5 (3125). Or say I even have 5x5x5x5 divided by 5 (125). But, it gets more advanced. I'd have to be able to get a number by putting in 5 to the 5555 power (27,775). I need to be able to type in the target number and get the equation in return. How would I go about doing this? If I could get any input from anyone, I would greatly appreciate it...and so would my low grade in the class. Thanks. edit: I'm using Microsoft Visual C++ 6.0.
Advertisement
People will probably be reluctant to help too much since this is for school, but I'm guessing you may find this quite tricky regardless.

I can't immediately think of an obvious way to reverse-engineer an equation like that from a number. There's another way to do it, though - precalculate the values of the equation for every permutation of operators, store the expression & value pairs somehow, and search through them when you need to find an appropriate expression for a given value. That's one possible strategy, and there's quite a few details for you to work out if you choose to follow it, like how you generate/evaluate the expressions and how you store/use the precalculated data.

There's quite a few permutations - it's the factorial of the number of operators. It looks like you are also allowing the omission of an operator, too, so you can have expressions like 5*55-555? And since I don't see any brackets in your expressions, I'm assuming you aren't using them? Brackets make things more complicated. So, you have +, -, /, *, ^ and an empty operator, which is 6. So, if you're not including brackets, you need to calculate 6! expressions, or 720. That kind of number is quite feasible to calculate when the app starts without anyone really noticing. If you do need to use brackets, you'll need to extend this rough design to accommodate them.

Good luck with it anyway :o) I expect you'll get some credit for it even if you don't get it all working perfectly, so long as you demonstrate some problem-solving skills.
Harry.
the best way i can see and the previous person probibly covered this (by the way if you want to preserve the text structure for that tree then use // without the slashes). Ok what i would do i read in an equation then store it in a text file and a string, this alows you to do more with it. then parse through the file several times one time for each operator speed level
^ = 3
* and / = 2
+ and - = 1

then do the operation to the numbers on either side of the equation so 6 ^ 2

read it in then on the fist pass you find a ^ rewinde to the last number befor the ^ store it in a variable then go 2 chars ahead of that (the number after) and store it then do the ^ operation (whatever then function is in the math library (i dont know i dont use it much yet))and store it then print a | symbol before the number on the left side of the operator (this is in case of duplicates) now go throught the string find a the spot were the | is and delete 3 numbers/operators after that then print your new value at the spot right after the | then erase the | (and the space that it leaves if any) and this should give you a nice step by step brake down of how you solved the equation. this dose of course get slighty more complicated if you have bracets other then that though. If any questions about this method let me know. I havnt done this before i just made this up on the fly so use at own risk (i think ill try this some time)
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
If you currently have a low grade, then to deserve an 'A' I'd say you should get stuck into it on your own, and do lots of thinking and research of your own.
People here have given you enough to start on already.
This website has a policy to not do people's homework for them.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement