In: Computer Science
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;
}
Please answer in c++
The code provided below, follows the exact algorithm given in the problem description, no extra thing have been added. Please note that the algorithm in the last gives "The code is correct" even when the number of opening symbol is equal to number of closing symbol, no matter what the order is. Also there was a confusion in the algorithm that after showing the errors, we had to end the program or had to go on till the end of file. The code just follows the algorithm and read the file till the last character, and if there is mismatch shows error and continue reading the next character. And in last prints the output. If the both testfiles will be generated according to the algorithm it will give the correct output.
for ex:
input file : " { ] "
the code will give a error of not equal but prints the code is correct (as for the algorithm says as the closing symbol comes, first pop the stack ( " { " ) and then match if it is corresponding to the closing symbol just read. so it will give the "not equal error", but as the stack is now empty after the stack, so the algorithm says the code is correct.
If you need the code which gives the code incorrect for this input file, the algorithm will be different, you can ask for that in comment section.
CODE(StackMain.cpp reamaining two files(StackArr.cpp & StackArr.h ) will be same):
// StackMain.cpp
#include<iostream>
#include <fstream>
#include "StackArr.h"
#include "StackArr.cpp"
using namespace std;
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;
}
void solve(string str){
int n = str.length(); // CALCULATE THE LENGTH OF STRING IN THE FILE
StackArr mystack(n); // MADE THE EMPTY STACK OF SIZE == LENGTH OF STRING
for(int i=0;i<n;i++){
if (str[i]=='(' || str[i]=='{' || str[i]=='[') // IF INPUT SYMBOL ENCOUNTERED JUST PUSH IT
mystack.push(str[i]);
else if(str[i]==')' || str[i]=='}'|| str[i]==']'){ // IF CLOSING SYMBOL
if (mystack.isEmpty()) // CHECK IF STACK IS EMPTY THEN SHOW ERROR
cout<<"Error: Empty stack"<<endl;
else{
char ch = mystack.pop(); // ELSE POP THE STACK
if ((ch=='(' && str[i]==')') || (ch=='{' && str[i]=='}') || (ch=='[' && str[i]==']')) // CHECK IF THE POPPED SYMBOL CORRESPONDS TO THE CLOSED SYMBOL READ IF IT IS CONTINUE
continue;
else // ELSE SHOW THE ERROR NOT EQUAL
cout<<"Error: Not equal"<<endl;
}
}
}
if (mystack.isEmpty()) // AFTER READING ALL THE CHARACTERS, CHECK IF THE STACK IS EMPTY IF IT IS PRINT THE CODE IS CORRECT
cout<<"The code is correct"<<endl;
else
cout<<"The code is incorrect"<<endl; // ELSE PIRNT CODE IS INCORRECT
}
int main(){
string str = readFile();
cout<<"input file content: "<<str<<endl;
solve(str);
return 0;
}
OUTPUT(SAMPLE):
NOTE: In case of any query or doubt, or you want any modification in the code, you can mention that in comment section. HAPPY LEARNING!!