need Recursion help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

need Recursion help

 
0
  #1
Apr 24th, 2006
This problem is asking me to convert an infix expression to a postfix expression using recursion. I did the exact same problem using stacks (w/o recursion), which was much easier, but on this one, I get stumped constantly when I try to come up with a line of code to call the function(itself) again. So far, what I've come up with is not really a true recursion function as it does not return a value =\ Thanks for any tips you guys can give. (And sorry if the code in the brackets is too compact).

fyi: The format of the input has to be something like (5*9) or it won't work.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define LEN 100
  7.  
  8. static char input[LEN], hold[LEN];
  9. static int i, j, cnt;
  10.  
  11. void eval();
  12.  
  13. int main(void)
  14. {
  15.  
  16. puts("Enter an infix expression:");
  17. fgets(input, LEN, stdin);
  18.  
  19. eval();
  20. return 0;
  21. }
  22.  
  23. void eval()
  24. {
  25. int x = 0;
  26. if (input[i] == '(') i++;
  27. if (input[i] == ')')
  28. {
  29. printf("%c", hold[--j]);
  30. i++;
  31. }
  32. if (input[i] == '*' || input[i] == '+')
  33. { hold[j++] = input[i]; i++; }
  34. while (isdigit(input[i]))
  35. {
  36. x = 10*x + (input[i++]-'0');
  37. printf("%d", x);
  38. }
  39. eval();
  40. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: need Recursion help

 
0
  #2
Apr 24th, 2006
-bah- Irrelevant post. Will look further.

Edit.
Looks okay at first glance.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: need Recursion help

 
2
  #3
Apr 25th, 2006
You're right; this isn't really recursion; you're just making a cute loop (and never returning :-)).

Once you get this working properly, the stack datastructure you are currently using will be replaced by the chain of recursive calls (which in a way, can be thought of a datastructure, too).

Here is how I recommend you think: "Reading an expression either requries reading an individual number, or, it requires reading one expression, reading an operator, and reading another expression." See the recursion in the way I've defined 'reading an expression'?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: need Recursion help

 
0
  #4
Apr 25th, 2006
heheee yes your definition calls itself.

i'm gonna give this another shot ;p
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: need Recursion help

 
0
  #5
Apr 26th, 2006
OK..I went through a couple of C books and some webpages to do some simple excersizes on recursion and I understand it a little better now. SoOoOo, here's my program which is only slightly modified from the original I posted 2 days ago. Any suggestions you guys can give would be helpful b/c I know that this is not a real recursion =\

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define LEN 100
  7.  
  8. static char input[LEN];
  9. static int i;
  10.  
  11. void eval();
  12.  
  13. int main(void)
  14. {
  15. puts("Enter an infix expression:");
  16. fgets(input, LEN, stdin);
  17.  
  18. eval();
  19.  
  20. return 0;
  21. }
  22.  
  23. void eval()
  24. {
  25. char hold;
  26.  
  27. if (input[i] == '(') i++;
  28. if (isdigit(input[i]))
  29. { printf("%c", input[i]); i++;}
  30. if (input[i] == '*' || input[i] == '+')
  31. {
  32. hold = input[i];
  33. i++; eval();
  34. printf("%c", hold);
  35. }
  36. if (input[i] == ')')
  37. { i++; return; }
  38. eval();
  39. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: need Recursion help

 
0
  #6
Apr 27th, 2006
first of all you did'nt initalize i !!!

The a close look at this program.

  1. int product(int n);
  2. int main(void)
  3. {
  4. char buf[100];
  5. int num;
  6. printf("Enter number for porduct : ");
  7. fgets(buf, sizeof(buf), stdin);
  8. num = atoi(buf);
  9. num = product(num);
  10. printf("num = %d\n", num);
  11. printf("Press enter to end ....\n");
  12. getchar();
  13. return(0);
  14. }
  15. /* There are 3 steps in writing a recursion
  16.   1) assume it knows how to calcluate the
  17.   previsous value.
  18.   2) write the current value by the
  19.   previsous step.
  20.   3) Make a stop conditon.
  21. */
  22. int product(int n)
  23. {
  24. int tmp;
  25.  
  26.  
  27. /* step 3 */
  28. if ( n < 2 )
  29. return(1);
  30.  
  31.  
  32. /* step 1 */
  33. tmp = product(n-1);
  34.  
  35. /* step 2 */
  36. return(n * tmp);
  37.  
  38. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: need Recursion help

 
0
  #7
Apr 27th, 2006
i is automatically initialized to 0 if its external. Also that looks like the factorial program that's pretty much everywhere =\
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: need Recursion help

 
0
  #8
Apr 27th, 2006
Originally Posted by degamer106
i is automatically initialized to 0 if its external. Also that looks like the factorial program that's pretty much everywhere =\
No offence, but if you're new to recursion, isn't it asking a bit much to use it to make an infix to postfix calculator?

But if you're dead set on doing this, essentially "the stack datastructure you are currently using will be replaced by the chain of recursive calls"

So, you need to understand exactly what your stack was doing before in order to replicate the same functionality using recursion.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: need Recursion help

 
0
  #9
Apr 27th, 2006
Here's my initial thoughts on the process:

The input is a string representing the infix notation of the expression and the return value will be a string representing the outfix notation of the expression.

The function will be passed the input string and an int representing the index of the current element of the string as parameters.

Each call to the function will find 3 strings--operand1, operator, and operand2. The return string will be built up by copying operand1 to the return string and then concatenating a space, operand2, a space, and the operator onto the return string in that order.

If a left parenthesis is found before all three strings have been found the function will advance the index by 1 and call itself using the input string and the current value of the index as parameters.

The terminating condition for the function calls will be finding a right parenthesis or the null char in the input string.

Each return string will be an operand in the calling function.

I haven't actually worked all this out, but it looks like a reasonable place to start from. On the other hand my brain's a bit fried from work today so who knows what pitfall(s) lurk(s) therein. Use at your own risk.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: need Recursion help

 
0
  #10
Apr 28th, 2006
Originally Posted by iamthwee
No offence, but if you're new to recursion, isn't it asking a bit much to use it to make an infix to postfix calculator?

But if you're dead set on doing this, essentially "the stack datastructure you are currently using will be replaced by the chain of recursive calls"

So, you need to understand exactly what your stack was doing before in order to replicate the same functionality using recursion.
Well i'm trying to learn recursion from this Algorithm book (Algorithms in C by Robert Sedgewick) and the author jumps from factorial recursion to a recursive program to evaluate prefix expressions in 3 pages :eek: And because I did stacks already, the author has created, as an excersize, problems involving conversion/calculations using recursion.
Thanks for your guys's help on this. Still wanna know if the function in the program I put up counts as recursin..

And the stack implementation goes something like...
- If its a number, print it
- if its an operator, push the operator onto the stack
- when you encounter a right parenthesis, pop the operator and print it.

So if you have (5*9), you would get 59*.

I think that's basically it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2273 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC