java.util.scanner - Get the Java program to read parenthesis, brackets, and curly braces -



java.util.scanner - Get the Java program to read parenthesis, brackets, and curly braces -

i writing programme verifies arithmetic look correctly formed. (for example: right form "2 + (2-1)", wrong form ")2+(2-1")

once it's been verified, programme compute results.

at moment, can compute in parenthesis easily. if there bracket involved (for example, "2 [ 3 + (1) ]") programme verifies look correct, cannot calculate results.

here code i'm concerned about

void postfixexpression() { stk.clear(); // re-using stack object scanner scan = new scanner(expression); char current; // algorithm doing conversion.... follow bullets while (scan.hasnext()) { string token = scan.next(); if (isnumber(token)) { postfix = postfix + token + " "; } else { current = token.charat(0); if (isparentheses(current)) { if (stk.empty() || current == constants.left_normal) { // force element on stack; stk.push(new character(current)); } else if (current == constants.right_normal) { seek { character ch = (character) stk.pop(); char top = ch.charvalue(); while (top != constants.left_normal) { postfix = postfix + top + " "; ch = (character) stk.pop(); top = ch.charvalue(); } } grab (emptystackexception e) { } } } else if (isoperator(current))// { if (stk.empty()) { stk.push(new character(current)); } else { seek { char top = (character) stk.peek(); boolean higher = hashigherprecedence(top, current); while (top != constants.left_normal && higher) { postfix = postfix + stk.pop() + " "; top = (character) stk.peek(); } stk.push(new character(current)); } grab (emptystackexception e) { stk.push(new character(current)); } } }// bullet # 3 ends } } // outer loop ends seek { while (!stk.empty()) // bullet # 4 { postfix = postfix + stk.pop() + " "; } } grab (emptystackexception e) { } }

i've created 2 methods: isbracket, , iscurly. @ first, thought appropriate solution include 2 method in same manner isparentheses. so:

if (isparentheses(current)) { if (stk.empty() || current == constants.left_normal) { // force element on stack; stk.push(new character(current)); } else if (current == constants.right_normal) { seek { character ch = (character) stk.pop(); char top = ch.charvalue(); while (top != constants.left_normal) { postfix = postfix + top + " "; ch = (character) stk.pop(); top = ch.charvalue(); } } grab (emptystackexception e) { } } if (iscurly(current)) { if (stk.empty() || current == constants.left_curly) { // force element on stack; stk.push(new character(current)); } else if (current == constants.right_curly) { seek { character ch = (character) stk.pop(); char top = ch.charvalue(); while (top != constants.left_curly) { postfix = postfix + top + " "; ch = (character) stk.pop(); top = ch.charvalue(); } } grab (emptystackexception e) { if (isbracket(current)) { if (stk.empty() || current == constants.left_square) { // force element on stack; stk.push(new character(current)); } else if (current == constants.right_square) { seek { character ch = (character) stk.pop(); char top = ch.charvalue(); while (top != constants.left_squacre) { postfix = postfix + top + " "; ch = (character) stk.pop(); top = ch.charvalue(); } } grab (emptystackexception e) {

but programme still not consider brackets , braces (but it's still reading parentheses fine.)

i'm not using methods correctly understand, how can utilize them appropriately?

here's thought thought of taking different approach problem.

you split numbers , brackets 2 different stacks, making easier ensure look formed correctly.

at origin of code, declare 2 stack variables:

stack<integer> numbers = new stack<integer>(); stack<character> operators = new stack<character>();

and force operators , numbers accordingly.

this quick method created demonstrate implementation of using 2 stack objects:

public double docalculation(string input) throws dataformatexception { if (input == null) { homecoming 0; } char[] characters = input.tochararray(); (char character: characters) { seek { // tries force number onto number stack numbers.push(integer.parseint("" + character)); } grab (numberformatexception e1) { // if caught, means character non-numerical operators.push(character); } } while (operators.size() > 0) { int = numbers.pop(); int j = numbers.pop(); char operator = operators.pop(); switch (operator) { case '+': numbers.push(j + i); break; case '-': numbers.push(j - i); break; case '*': numbers.push(j * i); break; case '/': numbers.push(j / i); break; case '^': numbers.push((int)(math.pow(j, i))); break; default: throw new dataformatexception(); } } homecoming numbers.pop(); }

just fun: if code added catch block before non-numerical character pushed stack, calculate equation in terms of order of operations:

char top; seek { top = operators.peek(); } grab (emptystackexception e2) { operators.push(character); continue; } if (getvalue(character) > getvalue(top)) { operators.push(character); continue; } else { seek { while (!(getvalue(character) > getvalue(operators.peek()))) { char operator; operator = operators.pop(); int = numbers.pop(); int j = numbers.pop(); switch (operator) { case '+': numbers.push(j + i); break; case '-': numbers.push(j - i); break; case '*': numbers.push(j * i); break; case '/': numbers.push(j / i); break; case '^': numbers.push((int)(math.pow(j, i))); break; default: throw new dataformatexception(); } } } grab (emptystackexception e3) { operators.push(character); continue; }

assuming getvalue() method defined accordingly:

public int getvalue(char character) throws dataformatexception { switch (character) { case '+': homecoming 1; case '-': homecoming 1; case '*': homecoming 2; case '/': homecoming 2; case '^': homecoming 3; default: throw new dataformatexception(); } }

java java.util.scanner

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -