Data Structure Labs

profileSiper
2018-mcis6214001-LabProg08.docx

Lab Assignment - MCIS 6214 Data Structure and Algorithms (Lab#08)

No Name, ID and section name : 2 point deduction

No output is provided: 10 point deduction,

No indentation: 10 points deduction

Late Policy: 1 day per 10% deduction

Problem:

The following code is an implementation of the stack operation. Complete the program to convert postfix to infix using the given C++ sample code.

Hand in:

1) A Single zipped file which contains Source code and output:

(Lab8.cpp, Lab8.h, Lab8A.h, Lab8.doc (Capture your screen shot- Don’t type ))

2) Upload two files to the Black board.

# When you upload files to the black board, those files should be compressed to one single file as attachment.( only five chances to upload)

Input

A B C * + D E / F * -

Output

(A+(B*C))-((D/E)*F))

Algorithm of Postfix to infix

1. Push post fix notation to the PostfixStack reverse order

( eg. push - * F / E D + * C B A)

2. while there are no data at PostfixStack

3. read one symbol from the PostfixStack

4. if the symbol is an operand

5. push it into the InfixStack

6. else

7. pop the top 2 values from the InfixStack

8. put the operator between two operand

9. Encapsulate the resulted string with parenthesis

10. if there is only one value in the stack

That value in the stack is the desired Infix notation

#include <iostream>

#include ".h"

//Include header file named "Lab8A.h"

using namespace std;

void testCopyConstructor(stackType<int> otherStack);

int main()

{

// Define stacks

stackType<int> stack(50);

stackType<int> copyStack(50);

stackType<int> dummyStack(100);

//Create two stacks PostfixStack type of char, InfixStack type of string.

//Push post fix notation to the stack

//A B C * + D E / F * -

stack.push(23);

stack.push(45);

//Function call “isOperator”

// Test function call using while loop

while ( )

{

//read all contents from the PostfixStack

//call function isOperator with actual parameter

//convert a char to string using stringstream

// If a content is an opcode

if ( ) {

// push to the InfixStack

}

else

{

// Take two operand from the InfixStack and save each value to the

Variable opr1 and opr2. And concatenate opr1, operator, opr2

And enclosed with ( ) and then save to the InfixStack.

}//end if

} // end while

// Check contents of the InfixStack

copyStack = stack; //copy stack into copyStack

cout << "The elements of copyStack: ";

while (!copyStack.isEmptyStack()) //print copyStack

{

cout << copyStack.top() << " ";

copyStack.pop();

}

cout << endl;

copyStack = stack;

testCopyConstructor(stack); //test the copy constructor

if (!stack.isEmptyStack())

cout << "The original stack is not empty." << endl

<< "The top element of the original stack: "

<< copyStack.top() << endl;

dummyStack = stack; //copy stack into dummyStack

cout << "The elements of dummyStack: ";

while (!dummyStack.isEmptyStack()) //print dummyStack

{

cout << dummyStack.top() << " ";

dummyStack.pop();

}

cout << endl;

system("pause");

return 0;

}

void testCopyConstructor(stackType<int> otherStack)

{

if (!otherStack.isEmptyStack())

cout << "otherStack is not empty." << endl

<< "The top element of otherStack: "

<< otherStack.top() << endl;

}

//Header file: myStack.h

Page 4 of 4