Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** Please make sure to use Visual Studio(C++). Thank you.** **The code** Task 1

ID: 3871081 • Letter: #

Question

*** Please make sure to use Visual Studio(C++). Thank you.**

**The code**

Task 1 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

Explanation / Answer

Given below is the code and output. Post a comment in case of any issues. Please rate it it helped. Thank you

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

template <typename T>
class my_stack
{
private:
vector<T> contents;
public:
my_stack(){}
bool empty()
{
return contents.empty();
}
T peek()
{
return contents.back();
}
void push(T data)
{
contents.push_back(data);
}
void pop()
{
if(!contents.empty())
contents.pop_back();
}
};

//returns true if balanced and false otherwise
bool checkBalance(string line);

int main()
{
string line;

string filename = "c:\myfiles\test_file.txt";
ifstream myfile(filename.c_str());
  
if (myfile.is_open())
{
while ( getline (myfile, line) )
{
if(checkBalance(line))
cout << line << " --> Balanced" << endl;
else
cout << line << " --> Not Balanced" << endl;
}

myfile.close();
}
else
cout << "Unable to open file" << filename << endl;

return 0;
}


bool checkBalance(string line)
{
my_stack<char> stack1;
char ch;
for(int i = 0; i < line.size(); i++)
{
ch = line[i];
if(ch == '(' || ch == '{' || ch == '[') //is opening parenthesis
stack1.push(ch);
else //not opening
{
if(ch == ')')
{
if (stack1.empty() || stack1.peek() != '(') //if its closing , but the stack is empty or top not matching
return false;
else
stack1.pop();
}
else if(ch == '}')
{
if (stack1.empty() || stack1.peek() != '{') //if its closing , but the stack is empty or top not matching
return false;
else
stack1.pop();
}
else if(ch == ']')
{
if (stack1.empty() || stack1.peek() != '[') //if its closing , but the stack is empty or top not matching
return false;
else
stack1.pop();
}

//nothing to do for other characters
}
}

if(stack1.empty())
return true;
else
return false;
}

output