944,117 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 16895
  • C RSS
May 18th, 2006
0

Prefix to Infix help please

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 18th, 2006
0

Re: Prefix to Infix help please

My first bit of advice would be to clean it up a little first.
Quote ...
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...]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 18th, 2006
0

Re: Prefix to Infix help please

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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
May 19th, 2006
0

Re: Prefix to Infix help please

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: linked lsts
Next Thread in C Forum Timeline: a c program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC