
Patrick B. answered 07/27/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <string>
#include <string.h>
#include "stacktype.h"
int main()
{
StackType<int> IntegerStack;
if (IntegerStack.IsEmpty())
{
cout << "Integer Stack Is Empty " <<endl;
}
int iNums[]={5,7,4,2};
for (int iLoop=0; iLoop<4; iLoop++)
{
cout << "Pushing " << iNums[iLoop] << endl;
IntegerStack.Push(iNums[iLoop]);
}
if (IntegerStack.IsEmpty())
{
cout << "Integer Stack Is Empty " <<endl;
}
string stackMsg= (IntegerStack.IsFull()) ? "The Integer Stack Is Full" : "The Integer Stack Is Not Full";
cout << stackMsg <<endl;
stackMsg= (IntegerStack.IsEmpty()) ? "The Integer Stack Is Empty" : "The Integer Stack Is Not Empty";
cout << stackMsg <<endl;
for (int iLoop=0; iLoop<4;iLoop++)
{
cout << "Popping " << (int) IntegerStack.Top();
IntegerStack.Pop() ; cout << " ......POP!!!"<<endl;
}
stackMsg= (IntegerStack.IsEmpty()) ? "The Integer Stack Is Empty" : "The Integer Stack Is Not Empty";
cout << stackMsg <<endl;
//outputting them in the order they were input
for (int iLoop=0; iLoop<4; iLoop++)
{
cout << "Pushing " << iNums[iLoop] << endl;
IntegerStack.Push(iNums[iLoop]);
cout << " Top = " << (int)IntegerStack.Top() << endl;
}
int iNum=3;
IntegerStack.Push(iNum); cout << "pushed " <<iNum<<endl;
stackMsg= (IntegerStack.IsFull()) ? "The Integer Stack Is Full" : "The Integer Stack Is Not Full";
cout << stackMsg <<endl;
stackMsg= (IntegerStack.IsEmpty()) ? "The Integer Stack Is Empty" : "The Integer Stack Is Not Empty";
cout << stackMsg <<endl;
cout << "Stack Dump:" << endl;
for (int iLoop=0; iLoop<5;iLoop++)
{
cout << "Popping " << (int) IntegerStack.Top();
IntegerStack.Pop() ; cout << " ......POP!!!"<<endl;
}
/**************************************************************************************/
/*
For each input character...
If LEFT Parenthesis '(' encountered, PUSHES the stack....
If RIGHT Parenthesis ')' :
if stack is empty then the parenthesis are NOT balanced ... TOO MANY Right Parenthesis!!!
otherwise POPs the stack
At the end of the input
if the stack is NOT empty, then the parenthesis are NOT balanced...TOO MANY Left Parenthesis!!!
*/
char inbuff[25];
cout << "input the expression containing the parenthesis :>";
cin.getline(inbuff,25,'\n');
int n = strlen(inbuff);
iNum=1;
bool parensBalanced=true;
for (int iLoop=0; iLoop<n; iLoop++)
{
char chCurChar = inbuff[iLoop];
cout << "current char = " <<chCurChar << endl;
if (chCurChar=='(')
{
IntegerStack.Push(1);
}
else if (chCurChar==')')
{
if (IntegerStack.IsEmpty())
{
parensBalanced=false;
break;
}
IntegerStack.Pop();
}
}
if (!IntegerStack.IsEmpty())
{
parensBalanced=false;
}
if (parensBalanced)
{
cout << "The parenthesis are balanced"<<endl;
}
else
{
cout << "The parenthesis are not balanced" << endl;
}
}
Emmma W.
Sir it will be kink of u=you if tou send the answer littel early07/29/21