C++ Implement a Stack ADT (abstract data type) as discussed in class; this invol
ID: 3571649 • Letter: C
Question
C++
Implement a Stack ADT (abstract data type) as discussed in class; this involves writing a stack.h header file. Then, write prog6.cpp which should include stack.h; what your program 6 should do is to allow for integer expression evaluation. Open the file expr.txt (which you can get from the cs server also) and read in each line of text as a string which represents an integer arithmetic expression, such as ((2+3)*4)/9 -- integer constants, parentheses, and the four basic arithmetic operators. For each line of text, print it out and on the same line, lined up on the right, print the integer evaluation of the expression. Thus your output might appear as
You basically need two functions -- one for converting infix strings to postfix strings, and one for evaluating postfix strings into integer results. These will be discussed in class. Both functions involve the use of a stack.
Get pfix.cpp (as seen in class) here. This code shows the routine for converting infix strings to postfix -- but you will also need to write an evaluate function that will take a postfix string and "evaluate" it. This will be discussed in class. Note, files like expr.txt and pfix.cpp can be gotten from the CS server. Just copy them to your account home directory like:
Explanation / Answer
SOURCE CODE:
stack.h
#define MAX_SIZE 1000
int top=-1;
int stack[1000];
void push(int num)
{
if(top == MAX_SIZE-1)
{
}
else
{
stack[++top]=num;
}
}
int pop()
{
if(top == -1)
{
}
return stack[top--];
}
prog6.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include "STACK.h"
using namespace std;
int power(int A,int B)
{
int i=0,res=1;
for(i=0;i<B;i++)
res=res*A;
return res;
}
int postfixEvaluation(char str[])
{
int A=0,B=0,i=0,res=0;
char c;
for(i=0;str[i]!='';i++)
{
c=str[i];
if(c=='0' || c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9')
{
push(c-48);
}
else
{
A=pop();
B=pop();
if(c=='+') { res=A+B;}
else if(c=='-'){ res=B-A;}
else if(c=='*'){ res=A*B;}
else if(c=='/'){ res=B/A;}
else if(c=='^'){res=power(B,A);}
else
{
printf(" Invalid Operation");
return -9999;
}
push(res);
}
}
return(pop());
}
int infixToPostfix(char str[])
{
int i=0,j=0;
char c;
char ret[MAX_SIZE];
for(i=0;str[i]!='';i++)
{
c=str[i];
if(c=='(')
push(c);
else if(c==')')
{
c=pop();
while(c!='(')
{
ret[j++]=c;
c=pop();
}
}
else if(c=='0' || c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9')
{
ret[j++]=c;
}
else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^')
{
push(c);
}
}
i = postfixEvaluation(ret);
return i;
}
int main()
{
string line="",filename;
int res=0,i=0;
char temp[MAX_SIZE];
cout<<"Enter the filename:";
cin>>filename;
ifstream myfile(filename.c_str());
cout<<" Expression Evaluation";
if (myfile.is_open())
{
while(getline (myfile,line) )
{
line="("+line+")";
for(i=0;i<line.length();i++)
temp[i]=line.at(i);
temp[i]='';
int res=infixToPostfix(temp);
cout<<" "<<line<<" "<<res;
line="";
}
myfile.close();
}
return 1;
}
SAMPLE OUTPUT:
Enter the filename:D:expr.txt
Expression Evaluation
(((2+3)*4)/9) 2
(5+4*8) 37
Compiler Used: gcc compiler
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.