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

Given a text file, your program will determine if all the parentheses, curly bra

ID: 3692003 • Letter: G

Question

Given a text file, your program will determine if all the parentheses, curly braces, and square brackets match, and are nested appropriately. Your program should work for mathematical formulas and most computer programs. Your program should read in the characters from the file, but ignore all characters except for the following: { } ( ) [ ] The general algorithm is to use a stack to store the opening unmatched brackets. When a closing bracket is encountered, check it against the one on top of the stack (pop it off)--make sure it matches. When you are finished there should be no unmatched brackets left on the stack. Your program should first implement a char stack. CharStack.h is provided on the website. You must supply the CharStack.cpp file that includes the implementations of the functions in the class declaration. Note that the stack elements will be stored in a string, and no variable named top is necessary. You can complete this assignment using these string functions: at(int), size(), append() or +=, and substr(int, int). Note: Do NOT use these functions: push_back, pop_back, or back.

class CharStack

{

private: string stackString;

//contains the stack of characters

public:

// Constructor

CharStack();

// Stack operations

void push(char);

char pop();

bool isFull() const;

bool isEmpty() const;

};

Explanation / Answer

I have executed it under NetBeans IDE.

package stackparanthesis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Parentheses {
private static final char L_PAREN = '(';
private static final char R_PAREN = ')';
private static final char L_BRACE = '{';
private static final char R_BRACE = '}';
private static final char L_BRACKET = '[';
private static final char R_BRACKET = ']';

public static boolean Balanced(String s) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {

if (s.charAt(i) == L_PAREN) stack.push(L_PAREN);

else if (s.charAt(i) == L_BRACE) stack.push(L_BRACE);

else if (s.charAt(i) == L_BRACKET) stack.push(L_BRACKET);

else if (s.charAt(i) == R_PAREN) {
if (stack.isEmpty()) return false;
if (stack.pop() != L_PAREN) return false;
}

else if (s.charAt(i) == R_BRACE) {
if (stack.isEmpty()) return false;
if (stack.pop() != L_BRACE) return false;
}

else if (s.charAt(i) == R_BRACKET) {
if (stack.isEmpty()) return false;
if (stack.pop() != L_BRACKET) return false;
}

// ignore all other characters

}
return stack.isEmpty();
}


public static void main(String[] args) throws IOException {
//String s = StdIn.readAll();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="";
Parentheses p=new Parentheses();
System.out.println(p.Balanced(str=br.readLine()));
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote