In: Computer Science
Stack
The algorithm for checking is as follows:
At the end of the file, if the stack IS NOT EMPTY, output "The code is incorrect". Otherwise, output "The code is correct".
Code below:
//////////////////////////////////StackArr.h////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef STACKARR_H
#define STACKARR_H
class StackArr {
private:
int maxTop;
int stackTop;
char *values;
public:
StackArr(int);
~StackArr();
bool isEmpty() const;
bool isFull() const;
char top() const;
void push(const char& x);
char pop();
void displayStack() const;
};
#endif
#pragma once
//////////////////////////////////StackArr.cpp////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "StackArr.h"
#include <string>
#include <iostream>
using namespace std;
StackArr::StackArr(int size) {
maxTop = size;
values = new char[size];
stackTop = -1;
}
StackArr::~StackArr() {
delete[] values;
}
bool StackArr::isEmpty() const {
return stackTop == -1;
}
bool StackArr::isFull() const {
return stackTop == maxTop;
}
void StackArr::push(const char& x) {
if (isFull())
cout << "Error! The stack is
full!" << endl;
else
values[++stackTop] = x;
}
char StackArr::pop() {
if (isEmpty()) {
cout << "Error! The stack is
empty!" << endl;
return -1;
}
else
return values[stackTop--];
}
char StackArr::top() const {
if (isEmpty()) {
cout << "Error! The stack is
empty!" << endl;
return -1;
}
else
return values[stackTop];
}
void StackArr::displayStack() const {
cout << "Top -->";
for (int i = stackTop; i >= 0; i--)
cout << "\t|\t" <<
values[i] << "\t|" << endl;
cout << "\t|---------------|" << endl
<< endl;
}
Hi there,
I have completed the code as per your requirements. I have made some changes to your above starter files as they were writing unwanted output to the terminal.
The code:
#include "StackArr.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
StackArr::StackArr(int size) {
maxTop = size;
values = new char[size];
stackTop = -1;
}
StackArr::~StackArr() {
delete[] values;
}
bool StackArr::isEmpty() const {
return stackTop == -1;
}
bool StackArr::isFull() const {
return stackTop == maxTop;
}
void StackArr::push(const char& x) {
if (isFull())
cout << "Error! The stack is full!" << endl;
else
values[++stackTop] = x;
}
char StackArr::pop() {
if (isEmpty()) {
return -1;
}
else
return values[stackTop--];
}
char StackArr::top() const {
if (isEmpty()) {
return -1;
}
else
return values[stackTop];
}
void StackArr::displayStack() const {
cout << "Top -->";
for (int i = stackTop; i >= 0; i--)
cout << "\t|\t" << values[i] << "\t|" <<
endl;
cout << "\t|---------------|" << endl <<
endl;
}
//Code starts
int main()
{
ifstream infile;
string file;
cout<<"Please enter the file name : ";
cin>>file;
//opening the file
infile.open(file);
//check if file exists or not
if (!infile.is_open())
{
cout<<"\nFile did not
open!! please try again...\n";
exit(0);
}
char ch;
//create the stack
StackArr st = StackArr(100);
//bool to track if the code is correct or
not
bool correct=true;
//read the file character by character
while(infile.get(ch))
{
//if it is a opening
bracket
if(ch=='(' || ch=='{' ||
ch=='[')
st.push(ch);
//if it is a closing
bracket
if(ch==')' || ch=='}' ||
ch==']')
{
//if the
closing bracket matches the opening bracket
if(!st.isEmpty() && (ch==')' && st.top() == '(') ||
(ch=='}' && st.top() == '{') || (ch==']' &&
st.top() == '['))
st.pop();
else
{
correct = false;
break;
}
}
}
if(!correct)
{
cout<<"\nError: Not equal";
}
else if(!st.isEmpty())
cout<<"\nThe code is
incorrect";
else
cout<<"\nThe code is
correct";
cout<<"\n";
//close the file
infile.close();
return 0;
}
The Code(screenshot) :
The test file(Screenshot) :
The Output(Screenshot) :
Hope you liked my answer. If you have any doubts, please feel free to ask in the comment section.
Happy Coding :)