Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

• Write a C++ program hw6.cpp that does the following: – The program first creat

ID: 3868632 • Letter: #

Question

• Write a C++ program hw6.cpp that does the following:

– The program first creates an STL stack that stores char data.

– Then ask user to enter a series of parentheses and/or braces, then report whether or not they’re properly nested:

Example run:

Enter parentheses and/or braces: ((){}{()})

Parentheses/braces are nested properly

Continue? (y/n)

Enter parentheses and/or braces: {{{()}}

Parentheses/braces are NOT nested properly

Continue? (y/n)

Enter parentheses and/or braces: {({{()(){}}})}

Parentheses/braces are NOT nested properly

Continue? (y/n)

– After each round, ask user if he/she wants to continue. If yes, go back to the previous step and take another string of parentheses/braces. If no, terminate program.

– Hint: As the program reads characters, have it push each left parenthesis or left brace into the STL stack. When it reads a right parenthesis or brace, have it pop the stack and check if the popped item is a matching parenthesis or brace (If not, the parentheses/braces aren’t nested properly). When there is no more character to read, check if the stack is empty; if so, the parentheses/braces are matched.

– If the user enters any character other than parenthesis or brace, simply ignore it. Therefore, entering a wrong character shouldn’t lead to program malfunction.

Explanation / Answer

#include <iostream.h>
#include <string>
string match int ;

{

string ret = " ";

if ( s == "(" ) ret = ")";

if ( s == ")" ) ret = "(";

if ( s == "{" ) ret = "}";

if ( s == "}" ) ret = "{";

if ( s == "[" ) ret = "]";

if ( s == "]" ) ret = "[";

return ret;

};

int main() {

item_type item;

stack s;

int count = 0;

while (cin >> item )

{ count ++;

if ( item == "(" || item == "{" || item == "[" )

s.push(item);

if ( item == ")" || item == "}" || item == "]" )

{

if ( s.empty() == true )

cout << " parenthesis " << item << " at position "

<< count << " is unbalanced " << endl;

else

{

if ( s.peek() == match(item) )

cout << " parenthesis " << item << " at position "

<< count << " matches with stack-top " << s.pop()

<< endl;

else

cout << " parenthesis " << item << " at position "

<< count << " does not match with stack top "

<< s.peek() << endl;

}

}

};

if ( s.empty() ) cout << " processing is over " << endl;

else

while ( !s.empty() )

cout << " parenthesis " << s.pop() << " at position " << count

<< " is unbalanced " << endl;
52
}

Write a C++ program series of parentheses and/or braces
The program first creates an STL stack that stores char data


#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>

using namespace std;

struct Stack{
static const unsigned MAX_SIZE = 5;
char data[ MAX_SIZE ];
unsigned size;
};

struct Stack2{

unsigned unmatchedLeftPos, unmatchedRightPos;

};

void initialize( Stack & stack );
void show( const Stack & stack );
unsigned getSize( const Stack & stack );
void push( Stack & stack, char c );
char pop( Stack & stack );
char top( const Stack & stack );
bool die( const string & msg );
bool balanced (unsigned & unmatchedLeftPos, unsigned & unmatchedRightPos, const string & expr);

int main(){


Stack2 s2;

cout << " Please enter your expression - enter a blank line to quit. ";

for(;;){

string line;
getline(cin, line);


if( line.size() == 0 ) break;

if (balanced(s2.unmatchedLeftPos, s2.unmatchedRightPos, line) == 1){

cout << "OK ";

}

else if (balanced(s2.unmatchedLeftPos, s2.unmatchedRightPos, line) == 0){

cout << string(s2.unmatchedLeftPos, ' ') << '^';
cout << string(s2.unmatchedRightPos, ' ') << '^';

}
}

return 0;
}

void initialize( Stack & stack ){
stack.size = 0;
}

void show( const Stack & stack ){
cout <<"[" << stack.size <<"]:";
for( unsigned i = 0; i < stack.size; i++ )
cout <<stack.data[i];
cout <<endl;
}

unsigned getSize( const Stack & stack ) {return stack.size;}

void push( Stack & stack, char c ){
if( stack.size == Stack::MAX_SIZE ) die( "push: overflow" );
stack.data[stack.size++] = c;
}
char pop( Stack & stack ){
if( stack.size == 0 ) die( "pop: underflow" );
return stack.data[--stack.size];
}

char top( const Stack & stack ){
if( stack.size == 0 ) die( "top: underflow" );
return stack.data[stack.size-1];
}
bool die( const string & msg ){
cerr <<endl <<"Fatal error: " << msg <<endl;
exit( EXIT_FAILURE );
}

bool balanced (unsigned & unmatchedLeftPos, unsigned & unmatchedRightPos, const string & expr){

Stack s;
initialize(s);

unsigned i;

for (i = 0; i < expr.size(); i++){

char c = expr[i];

if( expr.size() == Stack::MAX_SIZE) {
die( "push: overflow" );
}

if (c == '(')
{
push(s, c);
}

else if (c == '['){
push(s, c);
}

else if (c == '{'){
push(s, c);
}

if (s.size == 0 && (c == ')' || c == ']' || c == '}'))

{
unmatchedRightPos = i;
return false;
}

else if (c == ')' && top(s) == '('){
pop(s);
}

else if (c == ']' && top(s) == '['){
pop(s);
}

else if (c == '}' && top(s) == '{'){
pop(s);
}

}

if (s.size == 0){

return true;
}

else if (top(s) == '(' || top(s) == '[' || top(s) == '{'){

unmatchedLeftPos = i;
return false;
}

}