Prefix to Infix help please

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

Prefix to Infix help please

 
0
  #1
May 18th, 2006
I'm supposed to take user input in the form of a prefix expression, convert it to infix and postfix, and then print the results. So far, I've got both working, but I would like to add some parenthesis to the infix expression once its converted. At the moment, I can't think of a way to sneak it in there.

Here's my code so far.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. static char s[50], i;
  7. static char a[50];
  8.  
  9. void push(int data) {
  10. s[i++] = data;
  11. }
  12.  
  13. char pop() {
  14. return s[--i];
  15. }
  16.  
  17. // prefix -> infix
  18. // prefix -> postfix
  19. void prefix() {
  20.  
  21. puts("Enter a prefix expression:");
  22. fgets(a, 50, stdin);
  23. int a_size = strlen(a);
  24.  
  25. puts("Prefix -> Infix:");
  26. char *token, delim[] = {" \n"}, match[] = {"*+/-"};
  27. int j = 0, m_size = strlen(match);
  28. token = strtok(a, delim);
  29. while (token != NULL) {
  30. if (isdigit(*token)) {
  31. printf("%d ", atoi(token));
  32. if (i >= 0) printf("%c ", pop());
  33. }
  34. else
  35. while (j < m_size) {
  36. if (*token == match[j]) {
  37. push(*token);
  38. break;
  39. }
  40. j++;
  41. }
  42. token = strtok(NULL, delim);
  43. j = 0;
  44. }
  45.  
  46. puts("");
  47. puts("Prefix -> Postfix:");
  48. int k = a_size;
  49. while (k >= 0) {
  50. if (a[k] == '\0' || a[k] == ' ') k--;
  51. printf("%c ", a[k--]);
  52. }
  53. puts("");
  54. }
  55.  
  56. void infix() {
  57.  
  58. puts("Enter an infix expression:");
  59. fgets(a, 50, stdin);
  60.  
  61.  
  62. }
  63.  
  64. void postfix() {
  65. }
  66.  
  67. void main(void) {
  68.  
  69. int c;
  70. puts("1: Prefix");
  71. puts("2: Infix");
  72. puts("3: Postfix");
  73. scanf("%d", &c);
  74. fflush(stdin);
  75.  
  76. prefix();
  77. /*
  78. switch(c) {
  79. case 1: prefix();
  80. break;
  81. case 2: infix();
  82. break;
  83. case 3: postfix();
  84. break;
  85. }
  86. */
  87. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Prefix to Infix help please

 
0
  #2
May 18th, 2006
My first bit of advice would be to clean it up a little first.
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software 1985-2006

--- Module: test.c (C)
test.c 10 [Info 734] Loss of precision (assignment) (31 bits to 7 bits)
test.c 22 [Warning 534] Ignoring return value of function 'fgets(char *, int, FILE *)'
test.c 23 [Info 713] Loss of precision (initialization) (unsigned int to int)
test.c 27 [Info 713] Loss of precision (initialization) (unsigned int to int)
test.c 51 [Warning 676] Possibly negative subscript (-1) in operator '[' [Reference: file test.c: line 51]
test.c 59 [Warning 534] Ignoring return value of function 'fgets(char *, int, FILE *)'
test.c 6 [Info 728] Symbol 'i' (line 6, file test.c) not explicitly initialized

--- Global Wrap-up
test.c 56 [Info 714] Symbol 'infix(void)' (line 56, file test.c) not referenced
test.c 64 [Info 714] Symbol 'postfix(void)' (line 64, file test.c) not referenced

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test.c:
Warning W8071 test.c 10: Conversion may lose significant digits in function push
Error E2140 test.c 23: Declaration is not allowed here in function prefix
Error E2140 test.c 26: Declaration is not allowed here in function prefix
Error E2140 test.c 27: Declaration is not allowed here in function prefix
Warning W8065 test.c 32: Call to function 'pop' with no prototype in function prefix
Error E2140 test.c 48: Declaration is not allowed here in function prefix
Warning W8065 test.c 76: Call to function 'prefix' with no prototype in function main
*** 4 errors in Compile ***
The compiler errors are due to compiling as C++, but the others are worth a look. Some of the lint can be overlooked, but it's always worth looking into and finding out the reason behind the diagnostic.

And this always needs fixin':
  1. scanf("%d", &c);
  2. fflush(stdin);
User Input: Strings and Numbers [C]

Add in not using void main, and it might look like something I'd consider looking into. [I'm not trying to be offensive if it may sound so, I just hate rewriting the same stuff over and over and over...]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Prefix to Infix help please

 
0
  #3
May 18th, 2006
I hate these new quote tags. And what is it with the slider. It doesn't look nice at all. Plz bring back old ones.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Prefix to Infix help please

 
0
  #4
May 19th, 2006
>So far, I've got both working

wouldn't it be better to have an infix to postfix converter?

>I would like to add some parenthesis to the infix expression once its converted. At the moment, I can't think of a way to sneak it in there.

This is not something you can just hope to sneak in. You have to plan it very carefully, from the onset of your program.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC