|
-
December 14th, 2010, 09:29 PM
#1
#URGENT# Infix to postfix conversion and evaluation problem.
Greetings!
The program works fine as long as I dont put any '(' and ')' in the infix, and I can't find the reason why is it doing that. Also, I can't seem to make the program calculate numbers bigger than 9. In terms of let's say 23+17. And after placing 23+17 in the infix, I can't seem to make postfix look like 23 17 +. Could you please help me?
Side note: My fault i started to work on that today, and i need to finish it before today 23:59 CET. So I'd be thankful if you could help me asap.
Code starts here:
----------------------------------------------------------------------
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include <ctype.h>
using namespace std;
const int size =255;
char infix[size],postfix[size],stack[size];
int top=-1;
int precedence(char ch); // function to get the precedence of the operator
char pop(); //function to pop an element from the stack
char topelement(); // returns the top element of the stack
void push(char ch); // pushes an element into the stack
int eval(char *postf); // evaluation of postfix
int precedence(char ch)
{
switch(ch)
{
case '/' : return 4;
case '*' : return 4;
case '+' : return 3;
case '-' : return 3;
default : return 0;
}
}
char pop() //function to pop the element from the stack
{
char ret;
if(top!=-1)
{ ret =stack[top];
top--;
return ret;
}
else
return '#';
}
char topelement() // function to return top element from the stack without popping
{
char ch;
if(top!=-1)
ch=stack[top];
else
ch='#';
return ch;
}
void push(char ch) // function to push an element in the stack
{
if(top!=size-1)
{
top++;
stack[top]= ch;
}
}
int eval(char *postf){ //Evaluation of postfix
char *p;
int op1,op2,result;
p=&postf[0];
while(*p!='\0'){
if(isdigit(*p)){
push(*p-48);}
else{
op1=pop();
op2=pop();
switch(*p)
{
case '+':
result = op2 + op1;
break;
case '-':
result = op2 - op1;
break;
case '/':
result = op2 / op1;
break;
case '*':
result = op2 * op1;
break;
default:
printf("\nInvalid Operator");
return 0;
}
push(result);
}
p++;
}
result=pop();
return result;
}
//-----------------MAIN----------------------
int main()
{
char ele,elem,st[2];
int prep,pre,popped,j=0,chk=0,p;
strcpy(postfix," ");
cout<<"infix: ";
gets(infix);
for(int i=0;infix[i]!=0;i++){
while(infix[i] == ' ' || infix[i] == '\t'){
i++;
}
if(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);//stores the precedence of operator coming frm infix
ele=topelement();
prep=precedence(ele);//stores the precedence of operator at the top of the stack
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';
cout<<"\npost fix :"<<postfix<<endl;
cout<<"equals: "<<eval(&postfix[0])<<endl;
system("pause");
return 0;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|