5 Replies - 2485 Views - Last Post: 02 October 2010 - 05:21 AM Rate Topic: -----

#1 josephparagua  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 30
  • Joined: 28-September 10

I dont know how to convert this in to Infix to Prefix

Posted 01 October 2010 - 07:33 AM

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

const int size = 50;
char infix[50],postfix[5],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;
}
}




Could you please help me to INFIX to PREFIX

idont know how to convert it

This post has been edited by josephparagua: 01 October 2010 - 07:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I dont know how to convert this in to Infix to Prefix

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: I dont know how to convert this in to Infix to Prefix

Posted 01 October 2010 - 07:37 AM

So you found some code that almost does what you want, and you want us to make it do what you need? Isn't that CHEATING?
Was This Post Helpful? 0
  • +
  • -

#3 josephparagua  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 30
  • Joined: 28-September 10

Re: I dont know how to convert this in to Infix to Prefix

Posted 01 October 2010 - 07:59 AM

View PostJackOfAllTrades, on 01 October 2010 - 09:37 PM, said:

So you found some code that almost does what you want, and you want us to make it do what you need? Isn't that CHEATING?



gud day sir but you posted is postfix to infix not infix to prefix :(
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Josh*


Reputation:

Re: I dont know how to convert this in to Infix to Prefix

Posted 01 October 2010 - 08:18 PM

I am having trouble converting infix to prefix. No one taught us how to even make the code. The current code only makes it to postfix. I have to tinker this to make it infix to prefix. Sorry if I don't know how to post codes here. All I know that it involves stacks, arrays void functions.


#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;
    }
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:
Was This Post Helpful? 0

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: I dont know how to convert this in to Infix to Prefix

Posted 01 October 2010 - 08:37 PM

look into converting it all to trees. with trees it can be put in any form very easily. post code in code tags please :)

This post has been edited by ishkabible: 01 October 2010 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: I dont know how to convert this in to Infix to Prefix

Posted 02 October 2010 - 05:21 AM

Merged duplicate topics. So I see you've really done nothing since the original post, huh?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1