Use c++ and only use pop, push and back functions and make sure to test with the
ID: 3871166 • Letter: U
Question
Use c++ and only use pop, push and back functions and make sure to test with the following text file. Thanks
Please test these
Write a C++ program to read string (sequences of characters) from a text file saved on you hard drive, assume that each string is on a single line. After that, your program will parse the string and determine whether each sequence is parentheses, braces, and curly braces are "balanced." If not, it will display a message that indicates what are the missing characters from the sequence Your program can only use stack(s) data structure. You can only use implement your stack using the vector structure from the STL. Only stack operations can be used for inserting and removing elements from your stack. With the assignment, you should find an example written in C++ on how to read a text file line by line, feel free to adapt the code in that file to solve the problem You should also find a text file with a few expressions to test your code. Hint: for left delimiters, push onto stack; for right delimiters, pop from stack and check whether popped element matches right delimiter. For example, you can create a text file with the following expressions to test your code: (a+b) - c/yd*5Explanation / Answer
Given below are the files and output . Please don't forget to rate the answer if it helped. Thank you.
Stack.h
#ifndef Stack_h
#define Stack_h
#include <vector>
using namespace std;
template <typename T>
class Stack
{
private:
vector<T> elements;
public:
Stack(){};
void push(T);
void pop();
bool empty();
T peek();
int size();
};
template <typename T>
void Stack<T>::push(T data)
{
elements.push_back(data);
}
template <typename T>
void Stack<T>::pop()
{
if(!elements.empty())
elements.pop_back();
}
template <typename T>
bool Stack<T>::empty()
{
return elements.empty();
}
template <typename T>
T Stack<T>::peek()
{
return elements.back();
}
template <typename T>
int Stack<T>::size()
{
return elements.size();
}
#endif /* Stack_h */
check.cpp
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "Stack.h"
using namespace std;
bool isBalanced(string line);
int main()
{
string filename;
cout << "Enter input filename: ";
cin >> filename;
ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout << "Error opening input file " << filename << endl;
return 1;
}
string line;
while(true)
{
getline(infile, line);
if(infile.eof())
break;
bool bal = isBalanced(line);
if(bal)
cout << "Balanced: " << line << endl;
else
cout << "Not Balanced: " << line << endl;
}
infile.close();
}
bool isBalanced(string line)
{
Stack<char> stk;
string opening = "({["; //set of opening braces
string closing = ")}]" ; //set of closing braces in order for the opening braces
char ch;
int index;
for(int i = 0; i < line.size(); i++)
{
ch = line[i];
index = opening.find(ch); //check if it si one of opening
if(index == string::npos) //not opening
{
//check if it is closing
index = closing.find(ch);
if(index == string::npos) //not closing
continue;
if(stk.empty()) //got a closing but no matching opening on stack
return false;
else
{
char top = stk.peek();
stk.pop();
//cout << "popped " << top << endl;
if(top != opening[index]) //opening brace not matching closing brace
{
return false;
}
}
}
else //it is opening
{
stk.push(ch);
//cout << "pushed " << ch << endl;
}
}
if(stk.empty())
return true;
else
return false;
}
inptu file: paren.txt
(a+b)-c/}d*5{
[(3+)*{a^2-(2*e)}]
{(7-2}-a * b )-[a/2 + (5+a)]
z=(a+b)-{5%a}-k+[a-9]
output
Enter input filename: paren.txt
Not Balanced: (a+b)-c/}d*5{
Balanced: [(3+)*{a^2-(2*e)}]
Not Balanced: {(7-2}-a * b )-[a/2 + (5+a)]
Balanced: z=(a+b)-{5%a}-k+[a-9]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.