In: Computer Science
Please complete it in C++
Part 1: 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".
Use the provided files, testfile1.txt and testfile2.txt to test your program. The code file testfile1.txt is correct, while the code file testfile2.txt is incorrect.
Useful code:
#include <fstream> string readFile() { string tempString = ""; string filename;
cout << "Input file name: "; cin >> filename; ifstream inputFile(filename);
// Prompt user again if wrong filename received while (!inputFile.good()) { cout << "Wrong file name, input again please: "; cin >> filename; inputFile.open(filename); }
// Append all lines from the file into a long string while ((!inputFile.eof())) { string str; getline(inputFile, str); tempString += str; }
return tempString; } |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////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;
}
Below is the code for StackMain.cpp
#include <fstream>
#include<string>
#include"StackArr.h"
#include<iostream>
using namespace std;
string readFile()
{
string tempString = "";
string filename;
cout << "Input file name: ";
cin >> filename;
ifstream inputFile(filename);
while (!inputFile.good())
{
cout << "Wrong file name, input again please: ";
cin >> filename;
inputFile.open(filename);
}
while ((!inputFile.eof()))
{
string str;
getline(inputFile, str);
tempString += str;
}
return tempString;
}
void areParanthesisBalanced(string dataFromFile)
{
StackArr stack(dataFromFile.length() + 1);
char x;
// Traversing the dataFromFile
for (int i = 0; i < dataFromFile.length(); i++)
{
if (dataFromFile[i] == '(' || dataFromFile[i] == '[' || dataFromFile[i] == '{') //if opening bracket then push in stack
{
// Push the element in the stack
stack.push(dataFromFile[i]);
continue;
}
// IF current current character is not opening
// bracket, then it must be closing. So stack
// cannot be empty at this point.
if (stack.isEmpty())
{
cout << "Error: Empty stack";
return;
}
switch (dataFromFile[i]) {
case ')':
// Store the top element in a
x = stack.top();
stack.pop();
if (x == '{' || x == '[')
{
cout << "Error: Not equal";
return;
}
break;
case '}':
// Store the top element in b
x = stack.top();
stack.pop();
if (x == '(' || x == '[')
{
cout << "Error: Not equal";
return;
}
break;
case ']':
// Store the top element in c
x = stack.top();
stack.pop();
if (x == '(' || x == '{')
{
cout << "Error: Not equal";
return;
}
break;
}
}
// Check Empty Stack
if (stack.isEmpty())
{
cout << "The code is correct.";
}
else
{
cout <<"The code is incorrect";
}
}
int main()
{
string dataFromFile = readFile(); /* read data from file */
areParanthesisBalanced(dataFromFile); /* call functio to check if parenthesis are valid or not */
return 0;
}
output screenshot with validinput.txt
outputscreenshot with invalidinput.txt