473,729 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

evaluate postfix problem

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 to what I
want to do but am unable to get it to work. So if someone could please
help me it would greatly be appreciated. Thanks for your help.

code:

#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),l ink(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(con st 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=='+'||s ymb=='-'||symb=='*'||s ymb=='/'||symb=='^'|| symb=='('
||symb==')' )
return '\0';
else
return '1';
}

int isoperator(char symb)
{
int r=0;
if(symb=='+'||s ymb=='-'||symb=='*'||s ymb=='/'||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(c har *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(sym b))
{
postfix[toppos++]=symb;
postfix[toppos]='\0';
}
else
switch(symb)
{
case ')':
while( (top_operator=o perator_stack.p op())
&& top_operator!=' ('
) postfix[toppos++]=top_operator;
break;
case '(':
operator_stack. push(symb);
break;
default :
while( (top_operator=o perator_stack.p op())
&& precedence(top_ operator,symb)
) postfix[toppos++]=top_operator;
if(top_operator )
operator_stack. push(top_operat or);
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(c h1)&&isoperator (*p)) r=1;
if(ch1==')'&&is oprand(*p)) r=1;
if(ch1=='('&&is operator(*p)) r=1;
if(isoprand(ch1 )&& *p=='(') r=1;
ch1=*p++;
}
if(n>SIZE)r=n;
return r;
}

void evaluates(char *infix)
{
int position;
char symbol, numbers;
int number1, number2;

stack operator_stack;
for(position=0; infix[position] !='\0'; position++)
{
symbol=infix[position];
if(isoprand(sym bol))
{
numbers=infix[position];
operator_stack. push(numbers);
}
else
{
//I want to get the top item from the stack_operator stack
and the next number and add them or subtract them and so on depending
on the sign. I'm not really sure what to do so if someone could please
help it would greatly be appreciated.
symbol=infix[position];

switch(symbol)
{
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
case '^':
break;
}
}
}
}

//************
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(infi x);
if(!k)
{
infix_postfix(i nfix,postfix);
puts(postfix);
}
else
{
if(k==1) cout<<"Wrong expression!\n"< <endl;
else cout<<"infix is over flow.\n"<<endl; ;
}
evaluates(postf ix);

delete [] infix;
delete [] postfix;
return 0;
}

again thanks for the help
Jul 22 '05 #1
5 2639
"Henry Jordon" <bu*******@hotm ail.com> wrote...
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 to what I
want to do but am unable to get it to work. So if someone could please
help me it would greatly be appreciated. Thanks for your help.

code:

[...]

Have you tried the FAQ? I recommend 5.8, it should clear some things
up for you. Find FAQ here: http://www.parashift.com/c++-faq-lite/

HTH

V
Jul 22 '05 #2
On 10 Jul 2004 16:35:07 -0700, Henry Jordon <bu*******@hotm ail.com> wrote:
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 to what I
want to do but am unable to get it to work. So if someone could please
help me it would greatly be appreciated. Thanks for your help.

code:

void evaluates(char *infix)
{
Why infix? This function is supposed to be evaluating a postfix
expression. Are you confusing yourself or confusing us?
int position;
char symbol, numbers;
int number1, number2;

stack operator_stack;
for(position=0; infix[position] !='\0'; position++)
{
symbol=infix[position];
if(isoprand(sym bol))
{
numbers=infix[position];
operator_stack. push(numbers);
}
else
{
//I want to get the top item from the stack_operator stack
and the next number and add them or subtract them and so on depending
on the sign. I'm not really sure what to do so if someone could please
help it would greatly be appreciated.
symbol=infix[position];

switch(symbol)
{
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
case '^':
break;
}
}
}
}


OK you are not using the algorithm I tried to explain the last time you
posted, so the above code is the wrong approach. You need a stack for
numbers, not a stack for operators.

Was there something about the method I gave that you didn't understand? Or
did you think it was wrong? Either way its best to respond, not just post
the same question again.

Here's the right way to do it (in pseudocode)

Stack number_stack;
for (all symbols in postfix expression)
{
if (symbol is a number)
{
number_stack.pu sh(symbol);
}
else
{
number1 = number_stack.po p();
number2 = number_stack.po p();
if (symbol is +)
number3 = number1 + number2;
else if (symbol is -)
number3 = number1 - number2;
else
...
number_stack.pu sh(number3);
}
}
answer = number_stack.po p();

john
Jul 22 '05 #3
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<opsaymf4x a212331@androni cus>...

[snip]
Stack number_stack;
for (all symbols in postfix expression)
{
if (symbol is a number)
{
number_stack.pu sh(symbol);
}
else
{
number1 = number_stack.po p();
number2 = number_stack.po p();
if (symbol is +)
number3 = number1 + number2;
else if (symbol is -)
number3 = number1 - number2;
number3 = number2 - number1; // same for division
else
...
number_stack.pu sh(number3);
}
}
answer = number_stack.po p();


/david
Jul 22 '05 #4
On 11 Jul 2004 08:45:10 -0700, David Rubin <da********@war pmail.net> wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:<opsaymf4x a212331@androni cus>...

[snip]
Stack number_stack;
for (all symbols in postfix expression)
{
if (symbol is a number)
{
number_stack.pu sh(symbol);
}
else
{
number1 = number_stack.po p();
number2 = number_stack.po p();
if (symbol is +)
number3 = number1 + number2;
else if (symbol is -)
number3 = number1 - number2;


number3 = number2 - number1; // same for division


Just leaving some work for the OP! ;-)

Good spot.

john
Jul 22 '05 #5
John Harrison wrote:
On 10 Jul 2004 16:35:07 -0700, Henry Jordon <bu*******@hotm ail.com> wrote:
ok I have my problem entering in an expression in infix notation and
it outputs the postfix notation.
[snip]

OK you are not using the algorithm I tried to explain the last time you
posted, so the above code is the wrong approach. You need a stack for
numbers, not a stack for operators.

Was there something about the method I gave that you didn't understand?
Or did you think it was wrong? Either way its best to respond, not just
post the same question again.

Here's the right way to do it (in pseudocode)

Stack number_stack;
for (all symbols in postfix expression)
{
if (symbol is a number)
{
number_stack.pu sh(symbol);
}
else
{
number1 = number_stack.po p();
number2 = number_stack.po p();
if (symbol is +)
number3 = number1 + number2;
else if (symbol is -)
number3 = number1 - number2;
else
...
number_stack.pu sh(number3);
}
}
answer = number_stack.po p();

john


A stack may be the "right way" to do this, but using
a binary tree will help the OP convert expressions
among prefix, infix and postfix, just by the order
of traversing the tree. The data is still there,
the only thing changing is the traversal order.

Last time I did this exercise, stacks had to be
tailored to the 'fix notation.

Just my $0.5, as inflation goes...

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #6

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

Similar topics

3
863
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to Mandrake 9.2 and am reinstalling everything. This thing is kicking my ass and I can't seem to get past it. I could really use some help if anybody has any. Just for reference, this was my primary source for information on installation and...
1
3575
by: Charlie | last post by:
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...
19
11304
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
30
5481
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”. Likewise, they write “(if test this that)” to mean “if (test) {this} else {that}”. LISP codes are all of the form “(a b c ...)”, where the
8
7839
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
2
6742
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 involving more than one character (e.g. XYZ, 1234). (You may assume that the input number is an integer. But obviously the output can be real). 3. The program should send error messages such as "invalid postfix", "undeclared variables", "operator...
1
6544
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 static Stack operators = new Stack(); private static Stack operands = new Stack();
0
2032
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 Godaddy, I have point "A (Host) Record" to my IP and the "MX Record" is also pointing to my IP. I have tested sending e-mail from my gmail.com account to my server, the server receive e-mails from gmail.com to my /home/andre/mbox successfully. ...
2
5604
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 and encountering loop everytime I execute it. Here's my sample code: ******************************************************************** #include<stdio.h> #include<conio.h>
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6022
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.