473,419 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

add evaluate function (postfix)

I have the first part of the project done but I'm having difficulty
deciding how to add an evaluate function to this program.
The program asks the user to enter a function in infix notation and
then it prints out the function in postfix notation. I need to add an
evaluate function that evaluates the postfix notation obtained by the
program. I was hoping that someone could please give me some advice as
to how to add the evaluate function to this program. Thanks for the
help and here is the code thus far.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const int SIZE=100;
class stack;

class stacknode
{
private:
char data;
stacknode *link;
stacknode(int d=0,stacknode *l=0):data(d),link(l){};
public:
friend class stack;
};

class stack
{
private:
stacknode *top;
public:
stack(){top=0;};
void push(const char);
char pop();
char empty();
};

void stack::push(const char y)
{
top=new stacknode(y,top);
}

char stack::pop()
{
if(top==0)
return 0;
char retvalue;
stacknode *x=top;
retvalue=top->data;
top=x->link;
delete x;
return retvalue;
}

char stack::empty()
{
if(top==0) return '1';
else return '\0';
}

//************
char isoprand(char symb)
{
if(symb=='+'||symb=='-'||symb=='*'||symb=='/'||symb=='^'|| symb=='('
||symb==')' )
return '\0';
else
return '1';
}

int isoperator(char symb)
{
int r=0;
if(symb=='+'||symb=='-'||symb=='*'||symb=='/'||symb=='^') r=1;
return r;
}

//************
int precedence(char op1,char op2)
{
char s[4][3]={"()","+-","*/","^^"};
int i=0,j,k1,k2,r=1;
for(i=0;i<4;i++)
for(j=0;j<2;j++)
{
if(op1==s[i][j])k1=i;
if(op2==s[i][j])k2=i;
}
if(k1<k2)r=0;
return r;
}

//************
void infix_postfix(char *infix,char *postfix)
{
int position, toppos=0;
char top_operator='\0',symb;
stack operator_stack;
for( position=0; (symb=infix[position])!='\0'; position++)
{
if(isoprand(symb))
{
postfix[toppos++]=symb;
postfix[toppos]='\0';
}
else
switch(symb)
{
case ')':
while( (top_operator=operator_stack.pop())
&& top_operator!='('
) postfix[toppos++]=top_operator;
break;
case '(':
operator_stack.push(symb);
break;
default :
while( (top_operator=operator_stack.pop())
&& precedence(top_operator,symb)
) postfix[toppos++]=top_operator;
if(top_operator)
operator_stack.push(top_operator);
operator_stack.push(symb);
}
}
while(!operator_stack.empty())
postfix[toppos++]=operator_stack.pop();
postfix[toppos]='\0';
}

//************
int isnotexp(char *infix)
{
int r=0, k1=0,k2=0,n=0;
char *p = infix;
char ch1;
while(*p)
{
if(*p=='(')k1++;
else if(*p==')')
k2++;
p++;
n++;
}
if(k1!=k2)
r=1;
p=infix;
ch1=*p++;
while(*p)
{
if(isoperator(ch1)&&isoperator(*p)) r=1;
if(ch1==')'&&isoprand(*p)) r=1;
if(ch1=='('&&isoperator(*p)) r=1;
if(isoprand(ch1)&& *p=='(') r=1;
ch1=*p++;
}
if(n>SIZE)r=n;
return r;
}

//************
int main()
{
char *infix=new char[SIZE];
char *postfix=new char[SIZE];
int k=0;
cout<<"Please enter an expression : "<<endl;
gets(infix);
k=isnotexp(infix);
if(!k)
{
infix_postfix(infix,postfix);
puts(postfix);
}
else
{
if(k==1) cout<<"Wrong expression!\n"<<endl;
else cout<<"infix is over flow.\n"<<endl;;
}
delete [] infix;
delete [] postfix;
return 0;
}

thanks
Jul 22 '05 #1
1 3555
On 9 Jul 2004 16:22:23 -0700, Charlie <bu*******@yahoo.com> wrote:
I have the first part of the project done but I'm having difficulty
deciding how to add an evaluate function to this program.
The program asks the user to enter a function in infix notation and
then it prints out the function in postfix notation. I need to add an
evaluate function that evaluates the postfix notation obtained by the
program. I was hoping that someone could please give me some advice as
to how to add the evaluate function to this program. Thanks for the
help and here is the code thus far.


It's a simple enough procedure using a stack. Read your expression from
left to right. Whenever you see a number push it onto the stack. Whenever
you see an operator pop the last two numbers off the stack, do the
operation, and push the result back onto the stack. At the end you should
have one number left on the stack and that is the answer. Here's an
example (you need a fixed width font to view this properly)

stack:
expression: 9 1 1 + 3 * -
^

stack: 9
expression: 9 1 1 + 3 * -
^

stack: 9 1
expression: 9 1 1 + 3 * -
^

stack: 9 1 1
expression: 9 1 1 + 3 * -
^

stack: 9 2
expression: 9 1 1 + 3 * -
^

stack: 9 2 3
expression: 9 1 1 + 3 * -
^

stack: 9 6
expression: 9 1 1 + 3 * -
^

stack: 3
expression: 9 1 1 + 3 * -
^

answer: 3

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
5
by: Henry Jordon | last post by:
ok I have my problem entering in an expression in infix notation and it outputs the postfix notation. I now need to evaluate the postfix notation. I have some code written and there are comments as...
6
by: sellam | last post by:
I just installed to a new server and am trying to iron out certain bugs. Right now I have a problem with the mail function. I always get a FALSE return value when I call the mail function. ...
2
by: Phil Latio | last post by:
I understand (perhaps wrongly) that the mail() function relies on Sendmail being installed on the server. However I am wanting to run Postfix and Sendmail will not be loaded. Ideally I would...
2
by: rohit tripathi | last post by:
Write a program to evaluate a postfix expression using a stack. Note that 1. The expression may have the operators + - * / div and mod. 2. The expression may have variables and numbers...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
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: lopes80andre | last post by:
Hi, I'am trying to configure a Postfix server to send e-mails by SMTP using the SMTP server of my ISP. I have made a Postfix installation as "Internet with Smarthost". I'am using a domain in...
2
by: zeroeight | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.