472,989 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Infix to postfix conversion

Hi guys,

I'm a newbie here, I just want to seek for help regarding my program. I want to implement an infix to postfix conversion only accepting numbers. Right now, I already debugged the errors and encountering loop everytime I execute it.

Here's my sample code:

************************************************** ******************
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<ctype.h>
  4.  
  5.  
  6. struct stack_elem
  7. {
  8.  char elem[10];
  9.  int topelem;
  10. } stckmain;
  11.  
  12. void intoposconvert(char *infix, char *postfix, struct stack_elem *st)
  13. {
  14.  char *inf, *pos;
  15.  struct stack_elem *s;
  16.  char temp;
  17.  int tctr;
  18.  inf = &infix[0];
  19.  pos = &postfix[0];
  20.  s = st;
  21.  
  22. while(*inf){
  23.  if ( *inf == '(' )
  24.   {
  25.    tctr=++s->topelem; /* increment the topelem */
  26.    s->elem[tctr]=*inf; /*push*/
  27.    temp=s->elem[tctr]; /*assign value pushed in stack in temp*/
  28.    inf++;
  29.   }
  30.  else if ( *inf == ')' )
  31.   {
  32.    while ( temp != '(' )
  33.     {
  34.      tctr=s->topelem; /*assign top in tmp*/
  35.      temp=s->elem[tctr]; /*assign value to be popped in temp*/
  36.      --s->topelem; /*pop*/
  37.      *pos=temp;
  38.      pos++;
  39.      inf++;
  40.     }
  41.   }
  42.  else if (*inf == '*' || *inf == '/') /*data coming from ICP*/
  43.   {
  44.    if( s->topelem == -1 || s->elem[tctr] == '(' ) /*if stack is empty or entity in stack is '('*/
  45.     {
  46.      tctr=++s->topelem;
  47.      s->elem[tctr]=*inf;
  48.      temp=s->elem[tctr];
  49.      inf++;
  50.     }
  51.    else /*else stack is not empty*/
  52.     {
  53.      temp=s->elem[tctr];
  54.       if(temp == '+' || temp == '-')
  55.        {
  56.         tctr=++s->topelem;
  57.     s->elem[tctr]=*inf;
  58.     inf++;
  59.        }
  60.       else if (temp == '^')
  61.        {
  62.         tctr=s->topelem;
  63.     temp=s->elem[tctr];
  64.         --s->topelem;
  65.         *pos=temp;
  66.     pos++;
  67.     inf++;
  68.        }
  69.       else if (temp == '*' || temp == '/')
  70.       {
  71.        tctr=s->topelem;
  72.        temp=s->elem[tctr];
  73.        --s->topelem;
  74.        *pos=temp;
  75.        pos++;
  76.        inf++;
  77.        tctr=++s->topelem;
  78.        s->elem[tctr]=*inf;
  79.       }
  80.      }
  81.    }
  82.  else if (*inf == '+' || *inf == '-')
  83.   {
  84.    if( s->topelem == -1 || s->elem[tctr] == '(' ) /*if stack is empty or entity in stack is '('*/
  85.     {
  86.       tctr=++s->topelem;
  87.       s->elem[tctr]=*inf;
  88.       temp=s->elem[tctr];
  89.       inf++;
  90.     }
  91.    else
  92.     {
  93.       temp=s->elem[tctr];
  94.        if(temp == '*' || temp == '/' || temp == '^')
  95.         {
  96.           tctr=s->topelem;
  97.       temp=s->elem[tctr];
  98.           --s->topelem;
  99.       *pos=temp;
  100.       pos++;
  101.       inf++;
  102.         }
  103.        else if(temp == '+' || temp == '-')
  104.        {
  105.      tctr=s->topelem;
  106.      temp=s->elem[tctr];
  107.      --s->topelem;
  108.      *pos=temp;
  109.      pos++;
  110.      inf++;
  111.      tctr=++s->topelem;
  112.      s->elem[tctr]=*inf;
  113.        }
  114.      }
  115.    }
  116.  else if( isdigit(*inf) != 0)
  117.   {
  118.      temp=*inf;
  119.      *pos=temp;
  120.      pos++;
  121.      inf++;
  122.   }
  123.  else
  124.    printf("\n Not a valid input.");
  125.  }
  126. }
  127.  
  128. main()
  129. {
  130.  
  131. char infix[50], postfix[50];
  132. struct stack_elem *st;
  133.  
  134. clrscr();
  135. st = &stckmain;
  136. strcpy(stckmain.topelem, -1);
  137. strcpy(stckmain.elem[0], "");
  138.  
  139. printf("Enter infix statement: ");
  140. /*scanf("%d",&infix[0]);*/
  141. fflush(stdin);
  142. gets(infix);
  143. intoposconvert(&infix[0], &postfix[0], st);
  144. printf("\n Result in postfix statement: ");
  145. printf("%s", postfix);
  146. getch();
  147.  
  148. }
  149.  
************************************************** *********************

Thanks!
-AR
Oct 29 '09 #1
2 5507
newb16
687 512MB
I already debugged the errors and encountering loop everytime I execute it.
How can you execute it if it doesn't even compile? ( no, i'm not talking about conio.h includes that are not everywhere, I'm talking about strcpy)
Oct 29 '09 #2
newb16
687 512MB
if you change that line to
stckmain.topelem = -1;
it runs, but inserts open parenthesis where it shouldn't be.
Oct 29 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: KidLogik | last post by:
Hello! I am converting an infix expression string into a postfix so that I will be able to evaluate it easier -> (5*(((9+8)*(4*6))+7)) == 598+46**7+* I believe the rule is "Replace all...
22
by: Tony Johansson | last post by:
Hello Experts! I'm reading i a book about C++ and they mention infix with telling what it is. I hope you out there can do so. Many thanks! //Tony
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
11
by: Nhd | last post by:
Hi... I need a program to convert an infix arithmetic expression to postfix with the use of stacks... Can anyone tell me how do i go about implementing this? thanks
5
by: Thumbski | last post by:
Alright basically I just have one simple question. I can code perfectly fine and don't have any problems with the .cpp of the infix class but with my header I can't get anything to work really, any...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
0
by: coolguyjas07 | last post by:
plzzzzzz help me out to run my source code ......... i hav written dis code to convert infix expression to postfix but not getting desired output.... plzzz help me to get correct output.. d source...
4
by: madhumitha29 | last post by:
Does anyone know a simple C program to covert an infix to a postfix expression?plz kindly help!
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.