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

Write the code to check for balanced parenthesis in a C++ program. Please be spe

ID: 3755315 • Letter: W

Question

Write the code to check for balanced parenthesis in a C++ program. Please be specific on the skipping of characters as well as the error reporting. PLS DONOT ANSWER IF YOU DONT KNOW HOW TO READ AND SKIP COMMENT /* */ WITH ERROR REPORTING

PShow transcribed image text

C++ is empty. Here is the algorithm: program ends successfully after the end of the program is reached and the stack delimiterMatching (file) read character ch from file: while not end of file if ch is C T, or f push (ch) i else if ch is), 1 or ' ch and popped off delimiter do not match failure if else if ch is / read the next character if this character is skip all characters until «+/"is found and report an e if the end offile is reached before " " is encountered: else ch the character read in; continue // go to the beginning of the loop; // else ignore other characters; read next character ch from file if stack is empty successi else failure

Explanation / Answer

#include<iostream>
#include<stack>
#include<string>
#include <fstream>
#include <iostream>
using namespace std;
bool delimiterMatching(string filename);
bool FindPair(char opening,char closing);
int main()
{
string filename = "TEST.txt";
if(delimiterMatching(filename))
cout<<"Balanced ";
else
cout<<"Not Balanced ";
}
bool FindPair(char opening,char closing)
{
if(opening == '(' && closing == ')') return true;
else if(opening == '{' && closing == '}') return true;
else if(opening == '[' && closing == ']') return true;
return false;
}
bool delimiterMatching(string filename)
{
ifstream in;
in.open(filename);
char ch;
stack<char> S;
int flag = 0;
  
while(!in.eof()){
  
in.get(ch);
if(ch == '(' || ch == '{' || ch == '[')
S.push(ch);
else if(ch == ')' || ch == '}' || ch == ']')
{
if(S.empty() || !FindPair(S.top(),ch))
return false;
else
S.pop();
}
//To check that /* */ logiv inside CODE
else if(ch == '/'){
in.get(ch);
if(ch == '*'){
flag = 2;
  
while(flag != 0 || !in.eof()){
in.get(ch);
if(ch == '*' && flag == 2){
flag = 1;
}
if(ch == '/' && flag == 1){
flag = 0;
}
}
  
}
else{
continue;
}
}
}

// Condition to checK "*/" found or NOT
if(flag != 0){
cout<<'ERROR Incorrect END Of FILE';
return false;
}
return S.empty() ? true:false;
}