Question

In: Computer Science

Thus, an expression can contain a sequence of delimiters such as             { [ ( )...

Thus, an expression can contain a sequence of delimiters such as

            { [ ( ) ( ) ] ( ) }

but not

            [ ( ] )

For convenience, we will say that a balanced expression contains delimiters that are paired correctly, or are balanced.

  1. Write a program in java that detects whether an infix expression is balance using stack structure.
  2. Test your program on a set of infix algebraic expression.

Solutions

Expert Solution

import java.util.Stack;

public class BalancedParensChecker {

public static void main(String[] args) {

System.out.println(checkParens("(())"));

System.out.println(checkParens("[]"));

System.out.println(checkParens("(())"));

System.out.println(checkParens("{[]}"));

System.out.println(checkParens("{ [ ( ) ( ) ] ( ) }"));

System.out.println(checkParens(" [ ( ] )"));

}

private static boolean isBalanced(char ch, Stack<Character> mystack) {

boolean flag = false;

switch (ch) {

case ']':

if (!mystack.isEmpty() && (char) mystack.peek() == '[') {

mystack.pop();

flag = true;

} else {

System.out.println("Unbalanced right paranthesis were found");

System.exit(0);

}

break;

case '}':

if (!mystack.isEmpty() && (char) mystack.peek() == '{') {

mystack.pop();

flag = true;

} else {

System.out.println("Unbalanced right paranthesis were found");

System.exit(0);

}

break;

case ')':

if (!mystack.isEmpty() && (char) mystack.peek() == '(') {

mystack.pop();

flag = true;

} else {

System.out.println("Unbalanced right paranthesis were found");

System.exit(0);

}

break;

default:

flag = false;

}

return flag;

}

public static boolean checkParens(String s) {

if (s == null) {

return false;

}

boolean isBalancedParenthesis = false;

char[] arr = s.toCharArray();

Stack<Character> st = new Stack<Character>();

for (char ch : arr) {

if ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-' || ch == '*' || ch == '/') {

continue;

}

if (ch == '[' || ch == '{' || ch == '(') {

st.push(ch);

} else if (ch == ']' || ch == '}' || ch == ')') {

isBalancedParenthesis = isBalanced(ch,st);

}

}

if (!st.isEmpty()) {

System.out.println("Unbalanced left paranthesis were found");

System.exit(0);

}

return isBalancedParenthesis;

}

}

=======================================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Almost all cells within an animal contain DNA with the same sequence, yet different cells can...
Almost all cells within an animal contain DNA with the same sequence, yet different cells can have very different properties and gene expression patterns. What are the primary mechanisms that facilitate the existence of distinct cell types in eukaryotes?
. Write a sequence of instructions to calculate the following arithmetic expression and store the result...
. Write a sequence of instructions to calculate the following arithmetic expression and store the result in register CX: 20 – 6 + (-10) - (-8) + 15 Trace the contents of registers, assume initial contents are 0000 ps(there are multiple boxes) Instruction AX BX CX DX Remark initial 0000 0000 0000 0000
1. Which of the following can be treated as a Boolean expression? A. an int expression...
1. Which of the following can be treated as a Boolean expression? A. an int expression B. any of these C. the result of a comparison(such as <or>) D. a float expression 2. Assuming the variable x contains an integer value what will the result of the following statement be: if x<0 or x>=0. A. True B. False C. Sometimes true and sometimes false D. A "math domain" error will occur
Automobile airbags contain solid sodium azide, NaN3, that reacts to produce nitrogen gas when heated, thus...
Automobile airbags contain solid sodium azide, NaN3, that reacts to produce nitrogen gas when heated, thus inflating the bag. 2NaN3(s)⟶2Na(s)+3N2(g) Calculate the value of work, ?, for the system if 12.9 g NaN3 reacts completely at 1.00 atm and 22 ∘ C.
Automobile airbags contain solid sodium azide, NaN3, that reacts to produce nitrogen gas when heated, thus...
Automobile airbags contain solid sodium azide, NaN3, that reacts to produce nitrogen gas when heated, thus inflating the bag. 2NaN3(s)-->2Na(s)+3N2(g) Calculate the value of work, w, for the following system if 25.6 g of NaN3 reacts completely at 1.00 atm and 22 °C.
Automobile airbags contain solid sodium azide, NaN3, that reacts to produce nitrogen gas when heated, thus...
Automobile airbags contain solid sodium azide, NaN3, that reacts to produce nitrogen gas when heated, thus inflating the bag. 2NaN3(s) ---->2Na(s)+3N2(g) Calculate the value of w (work) for the following system if 30.9g of NaN3 reacts completely at 1.00 atm and 22 degrees celcius. W=___J
(8-10 sentences) How do macronutrients and micronutrients regulate gene expression and thus, protein function (in general).  Give...
(8-10 sentences) How do macronutrients and micronutrients regulate gene expression and thus, protein function (in general).  Give an example of an epigenetic influence that your eating behaviors (macro/micronutrient intake) might have on gene expression.  Use a peer reviewed article written in the past 5 years to support your discussion. As much detail as possible please!
Please explain how it is that an Expense can be recorded as - and thus operate...
Please explain how it is that an Expense can be recorded as - and thus operate as - a net credit and yet still be reported as a debit:
can you make me a sample problem of 2 geometric sequence, harmonic sequence, arithmetic sequence so...
can you make me a sample problem of 2 geometric sequence, harmonic sequence, arithmetic sequence so that's all 5 this is for Grade 10 Mathematics all with answers and solutions thankyou
describe Regulation of Gene Expression in Eukaryotes (Chapter 17). Describe 4 ways gene expression can be...
describe Regulation of Gene Expression in Eukaryotes (Chapter 17). Describe 4 ways gene expression can be regulated in Eukaryotes. (USE SENTENCES)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT