COP 3014 Lab 08 A postfix expression is an expression in which each operator fol
ID: 648686 • Letter: C
Question
COP 3014
Lab 08
A postfix expression is an expression in which each operator follows
its operands. For example,
infix expression postfix expression result
5 * 6 5 6 * 30
5 * ( 6 + 1 ) 5 6 1 + * 35
5 * 6 - 15 5 6 * 15 - 15
(5 * 6 ) + 15 5 6 * 15 + 45
40 + ( 5 * 6 ) / 3 40 5 6 * 3 / + 50
Your code should read one string at a time that represents a valid
postfix expression, as in the middle column above.
Please use the STL class stack that we used in the stack-STL example
in class. Your program should read one string at a time and determine
- if the first character of the string is a digit, assume
it is a number; convert the string into an int and push
it onto the stack
- anything else should be a binary operator; your program should
them pop the top two elements on the stack, compute the
result, and push the result back onto the stack
How does one convert a string into an int?
If you have a string str, you can obtain a C string from it using c_str( ):
str.c_str( )
The function atoi( ) takes a C string and converts it to an int:
int n = atoi( str.c_str( ) );
One needs to use
#include <cstdlib>
to make atoi( ) visible in a program.
This assignment is due on the BlackBoard one week after it is assigned.
Submit only the file lab08.cpp as FAUID_lab08.cpp.
No late assignments.
Please
- use braces around any block
- use blocks as we did in our class examples
- indent your code as we have used indentations in our examples
- comment your code; do not over comment your code
Penalties:
o -2 for each missing block
o -2 for each missing indentation
o no credit if your cpp file does not compile or does not run
o some partial credit if your program crashes depending on
the problem
end of lab 08.
Explanation / Answer
// iinfixtopostfix.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string"
#include <iostream>
#include <cmath>
#include <stack>
#include<math.h>
#include<cstdlib>
using namespace std;
stack<char> st;
stack<double> sti;
char var[20];
double data[20];
int no_of_var=0;
struct exp
{
char type;
double val;
}e[100];
int i=0;
int priority ( char c )
{
if ( c == '^' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
double search(char c,int *fl)
{
int j=0;
for(j=0;j<no_of_var;j++)
if(var[j]==c)
{
*fl=j;
return data[j];
}
*fl=-1;
return -1;
}
void create(char *s, double v)
{
double n;
int fl;
n = search(*s,&fl);
if(fl>=0)
{
var[fl] = *s;
data[fl] = v;
}
else
{
var[no_of_var] = *s;
data[no_of_var++] = v;
}
}
void convert(char *s )
{
char temp[100];
char *t = temp;
double n,d;
int fl;
i=0;
int cnt=0;
while ( *s )
{
if(*s==' ')
s++;
if ( isdigit ( *s ) || *s=='.')
{
n=0;d=1;
while ( isdigit ( *s ) )
{
n = n*10+ (*s-'0') ;
s++ ;
}
if(*s=='.')
{
s++;
while(isdigit ( *s ))
{
n = n + (*s-'0')/pow(10,d++);
s++;
}
}
e[i].type='n';
e[i++].val=n;
cnt++;
}
if(isalpha(*s))
{
n = search(*s++,&fl);
if(fl==-1)
{
cout<<" Variable not found !!";
return;
}
e[i].type='n';
e[i++].val=n;
cnt++;
}
if ( *s == '(' || *s == '[' || *s == '{')
{
st.push ( *s ) ;
s++ ;
cnt++;
}
char opr ;
if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '^' )
{
if(cnt==0 && *s=='-')
{
s++;
if ( isdigit ( *s ) || *s=='.')
{
n=0;d=1;
while ( isdigit ( *s ) )
{
n = n*10+ (*s-'0') ;
s++ ;
}
if(*s=='.')
{
s++;
while(isdigit ( *s ))
{
n = n + (*s-'0')/pow(10,d++);
s++;
}
}
n=0-n;
e[i].type='n';
e[i++].val=n;
cnt++;
}
}
if ( !st.empty() )
{
opr = st.top() ;
st.pop();
while ( priority ( opr ) >= priority ( *s ) && !st.empty() )
{
e[i].type='o';
e[i++].val=opr;
opr = st.top() ;
st.pop();
}
st.push ( opr ) ;
st.push ( *s ) ;
}
else
st.push ( *s ) ;
s++ ;
cnt=0;
}
if ( *s == ')' )
{
opr = st.top( ) ;
st.pop();
while ( ( opr ) != '(' )
{
e[i].val = opr ;
e[i++].type='o';
opr = st.top( ) ;
st.pop();
}
s++ ;
cnt=0;
}
if ( *s == ']' )
{
opr = st.top( ) ;
st.pop();
while ( ( opr ) != '[' )
{
e[i].val = opr ;
e[i++].type='o';
opr = st.top( ) ;
st.pop();
}
s++ ;
cnt=0;
}
if ( *s == '}' )
{
opr = st.top( );
st.pop();
while ( ( opr ) != '{' )
{
e[i].val = opr ;
e[i++].type='o';
opr = st.top( ) ;
st.pop();
}
s++ ;
cnt=0;
}
}
while ( !st.empty() )
{
char opr = st.top();
st.pop();
e[i].val = opr ;
e[i++].type='o';
}
}
void clear()
{
while(!st.empty())
st.pop();
while(!sti.empty())
sti.pop();
}
double calculate()
{
clear();
int j=0;
double n1, n2, n3,nn ;
while (j<i)
{
if ( e[j].type=='n' )
{
sti.push ( e[j].val ) ;
}
else
{
n1 = sti.top( ) ;
sti.pop();
n2 = sti.top( ) ;
sti.pop();
switch ( char(e[j].val) )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
// n3 = int(n2) % int(n1) ;
n3 = fmod(n2,n1);
break ;
case '^' :
n3 = pow ( n2 , n1 ) ;
break ;
default :
cout << "Unknown operator" ;
exit ( 1 ) ;
}
sti.push ( n3 ) ;
}
j++ ;
}
return sti.top();
}
int chk(char *s)
{
double v;
char *t;
t = s;
while(*s)
{
if(*s=='-'&& ( *(s+1) >= 'a' && *(s+1) <= 'z' ))
{
cout<<" - as unary operator not allowed !!";
return -1;
}
s++;
}
s = t;
while(*s)
{
if(*s++ == '=')
{
if(*t >='a' && *t <= 'z')
{
convert(s);
v = calculate();
create(t,v);
return 1;
}
else
{
cout<<" Only lower case letters allowed !";
return -1;
}
}
}
return 0;
}
int main( )
{
string expr ;
int r;
while(1)
{
cout << " >";
getline(cin,expr);
if(expr.length()==0)
break;
char expr1[100];
strcpy(expr1,expr.c_str());
r = chk(expr1);
if(r==0)
convert(expr1);
for(int j=0;j<i;j++)
{
if(e[j].type=='n')
cout << e[j].val;
else
cout << char(e[j].val);
}
cout<<endl<<calculate();
// else if(r==1)
}
}