answersLogoWhite

0


Best Answer

#

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;i{symbol=infix[i];if(isoperator(symbol)==0){postfix[j]=symbol;j++;}else{if(symbol=='(')push(symbol);else if(symbol==')'){while(stack[top]!='('){postfix[j]=pop();j++;}pop();//pop out (.}else{if(prcd(symbol)>prcd(stack[top]))push(symbol);else{while(prcd(symbol)<=prcd(stack[top])){postfix[j]=pop();j++;}push(symbol);}//end of else.}//end of else.}//end of else.}//end of for.while(stack[top]!='#'){postfix[j]=pop();j++;}postfix[j]='\0';//null terminate string.}void main(){char infix[20],postfix[20];clrscr();printf("Enter the valid infix string:\n");gets(infix);convertip(infix,postfix);printf("The corresponding postfix string is:\n");puts(postfix);getch();}void push(char item){top++;stack[top]=item;}char pop(){char a;a=stack[top];top--;return a;}//mail me: mchinmay@live.com
User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

/*A VERY SIMPLE PROGRAM TO EVALUATE POSTFIX EXPRESSION USING STACK */

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

{

int *stack;

char *p;

int top=0,i=0;

clrscr();

cout<<"WELCOME TO THE PROGRAM MADE BY SAURABH GNDU JALANDHAR "<<endl;

cout<<""<<endl;

cout<<"ENTER THE POSTFIX EXPRESSION "<<endl;

cin>>p;

int c = strlen(p);

while(i<c)

{

if(!( (p[i]>=42) && (p[i]<=47)) )

{

top = top +1;

stack[top] = p[i];

stack[top] = stack[top] - 48;

//cout<<stack[top]<<endl;

}

else

{

int temp1 , temp2;

temp1 = stack[top];

//cout<<temp1<<endl;

top--;

temp2 = stack[top];

//cout<<temp2<<endl;

top--;

int result;

switch(p[i])

{

case '+':

result = temp2 + temp1;

//cout<<result<<endl;

break;

case '-':

result = temp2 - temp1;

break;

case '*':

result = temp2 * temp1;

break;

case '/':

result = temp2 /temp1;

break;

}

top++;

stack[top]= result;

}

i++;

}

top=1;

cout<<stack[top]<<endl;

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include
#include
#include
struct stack
{
int top;
char item[100];
};
int empty(struct stack *s)
{
if (s->top==-1)
return 1;
else
return 0;
}
char popandtest(struct stack *ps)
{
return (ps->item[ps->top--]);
}
void pushandtest(struct stack *ps,char symb)
{
ps->top=ps->top+1;;
ps->item[ps->top]=symb;
}
int ISDIGIT(char symb)
{
if (symb>='0' && symb<='9')
{
return 1;
}
else
{
return 0;
}
}
double power(double op1,double op2)
{
int i;
double op11;
op11=op1;
for (i=1;i{
op1=op1*op11;
}
return op1;
}
double oper(char symb,double op1,double op2)
{

switch (symb)
{
case '+':

return (op1+op2);
break;
case '-':

return (op1-op2);
break;
case '*':

return (op1*op2);
break;
case '/':

return (op1/op2);
break;
case '$':

return (power(op1,op2));
break;
default:
printf("invalid operator\n");
return 0;
break;
}
}
double evalpre()
{
struct stack ps;
int count=0;

ps.top=-1;
int len;
int i,Break;
char string[100];
char symbol,top1,top2;
double op1,op2,value;
printf("Enter expression in prefix form values\n");
scanf("%s",string);
len=strlen(string);
for (i=0;i{
symbol=string[i];

if(ps.top>=1)
if(ISDIGIT(top1=ps.item[ps.top])==1 && ISDIGIT(top2=ps.item[ps.top-1])==1)
{
Break=1;
char symbol2;
while(Break!=0)
{
op2=(double)popandtest(&ps)-'0';
op1=(double)popandtest(&ps)-'0';
symbol2=popandtest(&ps);
value=oper(symbol2,op1,op2);
pushandtest(&ps,(char)value);
top1=ps.item[ps.top];
top2==ps.item[ps.top-1];
if(ISDIGIT(top1)==1 && ISDIGIT(top2)==1)
Break=1;
else
Break=0;
}
pushandtest(&ps,symbol);
}



else
pushandtest(&ps,symbol);

}
char symbol2;
while(ps.top!=0)
{
op2=(double)popandtest(&ps)-'0';
op1=(double)popandtest(&ps)-'0';
symbol2=popandtest(&ps);
value=oper(symbol2,op1,op2);
pushandtest(&ps,(char)value);
}
printf("top=%d\n",ps.top);
value=((double)(popandtest(&ps)-'0'));

return (value);
}
int main()
{
double val;
val=evalpre();
printf("value = %f\n",val);
return 0;
}





what problem does have this program?

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

/*Postfix->Evaluated value */

#include

#include

#include

#define MAXCOLS 80

#define TRUE 1

#define FALSE 0

struct stack{

int top;

double items[MAXCOLS];

};

double eval(char[]);

double pop(struct stack *);

void push(struct stack *,double);

int empty(struct stack *);

int isdigit(char);

double oper(int,double,double);

void main()

{

char expr[MAXCOLS];

int position =0;

while((expr[position++] = getchar()) != '\n') ;

expr[--position] = '\0';

printf("%s%s", " the original postfix expression is ",expr);

getchar();

printf("\n %f ", eval(expr));

getchar();

}

double pop(struct stack *ps)

{

double x;

if (ps->top==-1)

{

printf("%s","stack underflow");

exit(1);

}

else

{

x=ps->items[ps->top];

ps->top=ps->top - 1;

}

return x;

}

void push(struct stack *ps, double x)

{

if (ps->top == MAXCOLS-1)

{

printf("%s","stack overflow ");

exit(1);

}

else

{

ps->top=ps->top + 1;

ps->items[ps->top]=x;

}

}

double eval(char expr[])

{

int c , position;

double opnd1, opnd2, value ;

struct stack s;

s.top=-1;

for(position =0; (c=expr[position])!='\0'; position++)

if (isdigit(c))

push(&s,(double)(c-'0'));

else

{

opnd2=pop(&s);

opnd1=pop(&s);

value=oper(c ,opnd1 ,opnd2);

push(&s,value);

}

return(pop(&s));

}

int isdigit(char symb)

{

return ( symb >= '0' && symb <= '9');

}

double oper(int symb , double op1 , double op2)

{

switch (symb)

{

case '+' :return (op1 + op2);

case '- ' :return (op1 - op2);

case '*' :return (op1 * op2);

case '/' :return (op1 / op2);

case '$' :return (pow(op1 , op2));

default : printf("%s", "illegal operation ");

exit(1);

}

}

/*

This program is prepared for eveluating a given postfixexpression and when you run this program postfix expression must be given as input string and program will generate a numeric real number that is called as evaluated value.

If you give input string as the output of previous program 9 1 4 2 / + - 2 3 * / Program will generate evaluated value as 1.0

*/

This answer is:
User Avatar

User Avatar

Wiki User

14y ago
/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;i{symbol=infix[i];if(isoperator(symbol)==0){postfix[j]=symbol;j++;}else{if(symbol=='(')push(symbol);else if(symbol==')'){while(stack[top]!='('){postfix[j]=pop();j++;}pop();//pop out (.}else{if(prcd(symbol)>prcd(stack[top]))push(symbol);else{while(prcd(symbol)<=prcd(stack[top])){postfix[j]=pop();j++;}push(symbol);}//end of else.}//end of else.}//end of else.}//end of for.while(stack[top]!='#'){postfix[j]=pop();j++;}postfix[j]='\0';//null terminate string.}void main(){char infix[20],postfix[20];clrscr();printf("Enter the valid infix string:\n");gets(infix);convertip(infix,postfix);printf("The corresponding postfix string is:\n");puts(postfix);getch();}void push(char item){top++;stack[top]=item;}char pop(){char a;a=stack[top];top--;return a;}//mail me: mchinmay@live.com
This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes, you should.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

No

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in C language to convert infix expression to postfix expression?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why do compilers convert infix expressions to postfix?

people almost exclusively use infix notation to write mathematical expressions, computer languages almost exclusively allow programmers to use infix notation. However, if a compiler allowed infix expressions into the binary code used in the compiled version of a program, the resulting code would be larger than needed and very inefficient. Because of this, compilers convert infix expressions into postfix notation expressions, which have a much simpler set of rules for expression evaluation. Postfix notation gets its name from the fact that operators in a postfix expression follow the operands that they specify an operation on. Here are some examples of equivalent infix and postfix expressions Infix Notation Postfix Notation 2 + 3 2 3 + 2 + 3 * 6 3 6 * 2 + (2 + 3) * 6 2 3 + 6 * A / (B * C) + D * E - A - C A B C * / D E * + A C * - Where as infix notation expressions need a long list or rules for evaluation, postfix expressions need very few.


Example program of how to convert infix notation to postfix notation and prefix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


What application will be used to convert an assembly language source program into machine language?

An Assembler converts assembly language instructions into machine language.


A program which convert English language to Assamese language in c program?

No. There is not any program or software. B'coz language is such a vast thing. It needs lots of efforts and knowledge about d specific language. Translator by Google is provided for some famous languages only.


The compiler is responsible for what?

its just convert the source language of a program to target language at once.....whereas interpreter do it line wise....

Related questions

Sample program of postfix to infix?

You can convert from postfix to infix through the use of stacks. Consider the following expression conversion:54+67*+ -> ((5+4)+(6*7))The way this can be achieved is that whenever you encounter an operator, pop the last two expressions and join them using the operator. Remember to include the open braces before the first expression and a close braces after the second expression. Check the given link below for the program:


Why do compilers convert infix expressions to postfix?

people almost exclusively use infix notation to write mathematical expressions, computer languages almost exclusively allow programmers to use infix notation. However, if a compiler allowed infix expressions into the binary code used in the compiled version of a program, the resulting code would be larger than needed and very inefficient. Because of this, compilers convert infix expressions into postfix notation expressions, which have a much simpler set of rules for expression evaluation. Postfix notation gets its name from the fact that operators in a postfix expression follow the operands that they specify an operation on. Here are some examples of equivalent infix and postfix expressions Infix Notation Postfix Notation 2 + 3 2 3 + 2 + 3 * 6 3 6 * 2 + (2 + 3) * 6 2 3 + 6 * A / (B * C) + D * E - A - C A B C * / D E * + A C * - Where as infix notation expressions need a long list or rules for evaluation, postfix expressions need very few.


Example program of how to convert infix notation to postfix notation and prefix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


C plus plus program using a stacks converting a postfix-infix?

Yes


What application will be used to convert an assembly language source program into machine language?

An Assembler converts assembly language instructions into machine language.


A program which convert English language to Assamese language in c program?

No. There is not any program or software. B'coz language is such a vast thing. It needs lots of efforts and knowledge about d specific language. Translator by Google is provided for some famous languages only.


The compiler is responsible for what?

its just convert the source language of a program to target language at once.....whereas interpreter do it line wise....


Algorithm to convert postfix notation into infix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


How do you change source code into machine code?

A program called a compiler, or sometimes an assembler (depending on the programming language) does this for you. You write the source code, then invoke the program that will convert this into machine language.


What is the program to convert Assembly level language to Higher level language?

There is no such program. Low level languages cannot be converted to high level languages. It's one-way only.


How c language convert into machine language?

With a compiler, which is a program that "knows" how to transform the programming language logic in to machine code and make it perform from that.


Write a program to convert stack into queue using c language?

In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.