3 Replies - 4881 Views - Last Post: 02 October 2010 - 07:41 AM Rate Topic: -----

#1 rychellia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 10

Infix to Prefix

Posted 02 October 2010 - 07:31 AM

I am having trouble configuring the codes from infix to postfix to infix to prefix. I've made a lot of editing, but seem to be not working. I changed the ways on what are the elements will go to the stack first. Nothing is happening. Where do I have to edit the codes so it will be prefix.

#include<stdio.h>
#include<conio.h>
#include<io.h>
#include<string.h>
#include<stdlib.h>

const int size = 50;
char infix[50],postfix[50],stack[50];
int top=-1;
int precedence(char chk);

char pop();
char topelement();
void push(char ch);

int main()
{     char ele,elem;
    int prep,pre,popped,j=0,ch=0,i;
//clrscr();

//    clrscr();
    strcpy(postfix," ");

    gets(infix);
    for(i=0;infix[i]!=0;i++)
        {
        if(infix[i]!='('&&infix[i]!=')'&&!infix[i]!='^'&&infix[i]!='*'
        &&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')
            postfix[j++]=infix[i];
        else if(infix[i]=='(')
            {
            elem=infix[i];
            push(elem);
            }
        else if(infix[i]==')')
            {
            while((popped=pop())!='(')
                postfix[j++]=popped;
            }
        else
            {
            elem=infix[i];
            pre=precedence(elem);
            ele=topelement();
            prep=precedence(ele);

            if(pre>prep)
                push(elem);
            else
                {while(prep>=pre)
                    {
                    if(ele=='#')
                        break;
                    popped=pop();
                    ele=topelement();
                    postfix[j++]=popped;
                    prep=precedence(ele);
                    }
                    push(elem);
                }
            }
        }
        while((popped=pop())!='#')
            postfix[j++]=popped;
        postfix[j]='\0';

        printf("The postfix is: %s",postfix);
        getch();
        system("pause");
        return 0;
}
int precedence(char ch)
{
    switch(ch)
        {
        case'^':return 5;
        case'/':return 4;
        case'*':return 4;
        case'+':return 3;
        case'-':return 3;
        default :return 0;
        }
}
char pop()
{
char ret;
if(top!=-1)
    {ret=stack[top];
    top--;
    return ret;
    }
else
    return '#';
}
char topelement()
{
char ch;
if(top!=-1)
    ch=stack[top];
else
    ch= '#';
    return ch;
}
void push(char ch)
{
if(top!=size-1)
    {top++;
    stack[top]=ch;
    }
}


This post has been edited by sarmanu: 02 October 2010 - 07:41 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Infix to Prefix

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Infix to Prefix

Posted 02 October 2010 - 07:35 AM

First edit your post and put your formatted code in code tags.
:code:

Next, don't ever use gets() or system("pause").

http://www.gidnetwork.com/b-56.html
http://www.gidnetwork.com/b-61.html

This post has been edited by eker676: 02 October 2010 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 rychellia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 10

Re: Infix to Prefix

Posted 02 October 2010 - 07:40 AM

well this is not an assignment. Though I saw other posts that might have been their assignment. I just need a path to which O need to edit. I believe this code is available in the net. (Rychellia)

View Postrychellia, on 02 October 2010 - 06:36 AM, said:

well this is not an assignment. Though I saw other posts that might have been their assignment. I just need a path to which O need to edit. I believe this code is available in the net. (Rychellia)

my edited code is just removed (!) in the if statements for the operations. and changed else if(infix[i]==')') to else if(infix[i]!=')') but still nothing happens. I jumbled the ele and elem but it just reveres the operations are still at the end.
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Infix to Prefix

Posted 02 October 2010 - 07:41 AM

Edit your post and use code tags. No one wants to look at 100 lines of unindented code.

:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1