Wont produce the expected output Truthtable in java using stack - java

This is my code:
import java.util.*;
public class TruthTable {
public static boolean evaluateExpression(String expression, boolean p, boolean q) {
Stack<Boolean> stack = new Stack<>();
Stack<Character> opStack = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c == 'p') {
stack.push(p);
} else if (c == 'q') {
stack.push(q);
} else if (c == '¬') {
boolean b = stack.pop();
stack.push(!b);
} else if (c == '∧') {
// Check if the next character is a ¬ character.
if (i + 1 < expression.length() && expression.charAt(i + 1) == '¬') {
// If the next character is a ¬ character, pop the top of the stack and negate it,
// then push the conjunction of the negated value and the value of q.
boolean b = stack.pop();
stack.push(!(b && q));
// Increment the index to skip the ¬ character.
i++;
} else {
// If the next character is not a ¬ character, simply push the conjunction of the
// value of p and the value of q.
stack.push(stack.pop() && q);
}
} else if (c == '∨' || c == '→' || c == '↔' || c == '⊕' || c == '⊼' || c == '⊽') {
while (!opStack.isEmpty() && getPrecedence(c) <= getPrecedence(opStack.peek())) {
char op = opStack.pop();
applyOperator(op, stack);
}
opStack.push(c);
}
}
while (!opStack.isEmpty()) {
char op = opStack.pop();
applyOperator(op, stack);
}
return stack.pop();
}
private static void applyOperator(char op, Stack<Boolean> stack) {
boolean b1 = stack.pop();
boolean b2 = stack.pop();
switch (op) {
case '∧':
stack.push(b1 && b2);
break;
case '∨':
stack.push(b1 || b2);
break;
case '→':
stack.push(!b1 || b2);
break;
case '↔':
stack.push(b1 == b2);
break;
case '⊕':
stack.push(b1 != b2);
break;
case '⊼':
stack.push((b1 && b2) || (!b1 && b2) || (b1 && !b2));
break;
case '⊽':
stack.push(b1 && b2 && stack.pop());
break;
case '¬':
stack.push(!b1);
break;
}
}
private static int getPrecedence(char op) {
switch (op) {
case '¬':
return 3;
case '∧':
return 2;
case '∨':
return 1;
case '→':
return 0;
case '↔':
return 0;
case '⊕':
return 1;
case '⊼':
if (op == '∨' || op == '→' || op == '↔' || op == '⊕') {
return 2;
} else {
return 3;
}
case '⊽':
return 2;
default:
return -1;
}
}
public static void main(String[] args) {
String expression = "pq¬∨pq∧→";
System.out.println("p\tq\t(" + expression + ")");
for (boolean p : new boolean[]{true, false}) {
for (boolean q : new boolean[]{true, false}) {
System.out.println(p + "\t" + q + "\t" + evaluateExpression(expression, p, q));
}
}
}
}
The expected result is true false true false but it prints out false true false true and ive been trying to figure out how to fix it but I just cant. Will appreciate any help
Ive tried changing the precedence but it only produced the output True true true true

I don't expect many people will be able to help you, especially if you cannot see the logic error and you are the author!
When writing code like this, your best option is to write Unit Tests. Test parts of the implementation first and then give your algorithm expressions bit by bit and work up to more complex cases.
Note that your use of private static methods may frustrate your ability to test parts or mock out parts of the code. Go find an article about Unit Testing and Dependency Injection; it will be worthwhile your investment.

Related

setCharAt method not working as expected inside for loop (Java)

I am trying to develop a method to reverse the vowels in a string. For this, I have developed my own small stack. I am iterating backwards through the string twice, once to populate the stack with each vowel I locate, the second time to replace the vowel with the vowel stored at the top of the stack. I have added a bunch of print statements to determine where this is failing and it seems to be failing at the setCharAt method. For testing purposes, I provide a string aeiou and I am expecting to get back uoiea. The chars are getting replaced during each iteration, but unfortunately, they don't stay that way. As of the next iteration, the chars that were replaced on the previous iteration return to what they were before. As a result, I am getting back ueiou, where only the first character in the string is as expected (and the third which is a noop). Here is my code. Any tips are appreciated.
import java.util.*;
public class Solution {
static String result;
static class StackOfVowels {
static Node top;
public StackOfVowels() {
Node top = null;
}
static class Node {
Node next = null;
char vowel = '\0';
Node(char value) {
this.vowel = value;
}
}
public void push(char item) {
Node node = new Node(item);
if (top == null) {
top = node;
}
else {
node.next = top;
top = node;
}
}
public char top() {
if (top == null) throw new EmptyStackException();
return top.vowel;
}
public void pop() {
int result = -1;
if (top != null) {
result = top.vowel;
top = top.next;
}
}
}
public static String reverseVowels(String s) {
StackOfVowels stackOfVowels = new StackOfVowels();
for(int i = s.length()-1; i >= 0; i--) {
char c = s.charAt(i);
if ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u')) {
System.out.println("Initial sequence of iterations identified vowel: " + c);
stackOfVowels.push(c);
System.out.println("Pushed to stack, current top is: " + stackOfVowels.top());
}
}
for(int j = s.length()-1; j >= 0; j--) {
char b = s.charAt(j);
if ((b == 'a') || (b == 'e') || (b == 'i') || (b == 'o') || (b == 'u')) {
System.out.println("Second sequence of iterations identified vowel: " + b);
StringBuilder newstr = new StringBuilder(s);
char d = stackOfVowels.top();
System.out.println("Variable d set to top of: " + stackOfVowels.top());
newstr.setCharAt(j, d);
result = newstr.toString();
System.out.println("Here is the new string: " + result);
stackOfVowels.pop();
System.out.println("Stack was popped");
}
}
return result;
}
public static void main(String[] args) {
String s = "aeiou";
reverseVowels(s);
System.out.println("Final result: " + result);
}
}
A slight improvement (debatable) on #Elliot answer is to create the StringBuilder with the original String and only replace when necessary
String vowels = new StringBuilder(s.replaceAll("[^AEIOUaeiou]", ""))
.reverse().toString();
StringBuilder sb = new StringBuilder(s);
for (int i = 0, p = 0; i < s.length(); i++) {
switch (Character.toLowerCase(s.charAt(i))) {
// Note that these fall-through...
case 'a': case 'e': case 'i': case 'o': case 'u':
sb.setCharAt(i, vowels.charAt(p++));
break;
default:
break;
}
}
return sb.toString();
You generate always a new StringBuilder in your second loop with StringBuilder newstr = new StringBuilder(s);
But you always use ´s´ and not ´result´ to build your new StringBuilder.
It would be better anyways to not create a new Object of StringBuilder in each Iteration.
Also it is confusing that your methode has a return value, which is never used.
Following methode should work:
public static String reverseVowels(String s) {
StackOfVowels stackOfVowels = new StackOfVowels();
for(int i = s.length()-1; i >= 0; i--) {
char c = s.charAt(i);
if ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u')) {
System.out.println("Initial sequence of iterations identified vowel: " + c);
stackOfVowels.push(c);
System.out.println("Pushed to stack, current top is: " + stackOfVowels.top());
}
}
StringBuilder result_builder = new StringBuilder(s);
for(int j = s.length()-1; j >= 0; j--) {
char b = s.charAt(j);
if ((b == 'a') || (b == 'e') || (b == 'i') || (b == 'o') || (b == 'u')) {
System.out.println("Second sequence of iterations identified vowel: " + b);
char d = stackOfVowels.top();
System.out.println("Variable d set to top of: " + stackOfVowels.top());
result_builder.setCharAt(j, d);
stackOfVowels.pop();
System.out.println("Stack was popped");
}
}
result = result_builder.toString();
return result_builder.toString();
}
I would approach the problem a little differently, first I would use a regular expression to remove all consonants from the String to create a String of vowels. Then I would iterate through the characters of the String, using StringBuilder to build the output (passing consonants through, but replacing vowels using the vowels String previously created). Like,
public static String reverseVowels(String s) {
// Match all consonants, and remove them. Reverse that String.
String vowels = new StringBuilder(s.replaceAll("[^AEIOUaeiou]", ""))
.reverse().toString();
StringBuilder sb = new StringBuilder();
for (int i = 0, p = 0; i < s.length(); i++) {
switch (Character.toLowerCase(s.charAt(i))) {
// Note that these fall-through...
case 'a': case 'e': case 'i': case 'o': case 'u':
sb.append(vowels.charAt(p++));
break;
default:
sb.append(s.charAt(i));
}
}
return sb.toString();
}
This is one of the places that switch and fall-through behavior can be taken advantage of.
And if you're using Java 8+, you could express the same with an IntStream and map the characters; like
String vowels = new StringBuilder(s.replaceAll("[^AEIOUaeiou]", ""))
.reverse().toString();
int[] counter = new int[1]; // <-- A bit of a kludge really.
return IntStream.range(0, s.length()).mapToObj(i -> {
switch (Character.toLowerCase(s.charAt(i))) {
case 'a': case 'e': case 'i': case 'o': case 'u':
return Character.toString(vowels.charAt(counter[0]++));
default:
return Character.toString(s.charAt(i));
}
}).collect(Collectors.joining());

How to use variables in stack?

I'm trying to write a calc program that finds the infix. In addition the user will input numbers for the x variable and the program will solve it. My program works but it only solves it the first time. The following times it gives the same answer as the first time.
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
class Stack {
char a[] = new char[100];
int top = -1;
void push(char c) {
try {
a[++top] = c;
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Stack full , no room to push , size=100");
System.exit(0);
}
}
char pop() {
return a[top--];
}
boolean isEmpty() {
return (top == -1) ? true : false;
}
char peek() {
return a[top];
}
}
public class intopost {
static Stack operators = new Stack();
public static void main(String argv[]) throws IOException {
String infix;
// create an input stream object
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
// get input from user
System.out.print("\nEnter the algebraic expression in infix: ");
infix = keyboard.readLine();
String postFx = toPostfix(infix);
// output as postfix
System.out.println("The expression in postfix is:" + postFx);
if (postFx.contains("x")) {
String line = "";
do {
System.out.println("Enter value of X : ");
line = keyboard.readLine();
if (!"q".equalsIgnoreCase(line)) {
postFx = postFx.replaceAll("x", line);
System.out.println("Answer to expression : "
+ EvaluateString.evaluate(postFx));
}
} while (!line.equals("q"));
} else {
System.out.println("Answer to expression : "
+ EvaluateString.evaluate(postFx));
}
}
private static String toPostfix(String infix)
// converts an infix expression to postfix
{
char symbol;
String postfix = "";
for (int i = 0; i < infix.length(); ++i)
// while there is input to be read
{
symbol = infix.charAt(i);
// if it's an operand, add it to the string
if (symbol != ' ') {
if (Character.isLetter(symbol) || Character.isDigit(symbol))
postfix = postfix + " " + symbol;
else if (symbol == '(')
// push (
{
operators.push(symbol);
} else if (symbol == ')')
// push everything back to (
{
while (operators.peek() != '(') {
postfix = postfix + " " + operators.pop();
}
operators.pop(); // remove '('
} else
// print operators occurring before it that have greater
// precedence
{
while (!operators.isEmpty() && !(operators.peek() == '(')
&& prec(symbol) <= prec(operators.peek()))
postfix = postfix + " " + operators.pop();
operators.push(symbol);
}
}
}
while (!operators.isEmpty())
postfix = postfix + " " + operators.pop();
return postfix.trim();
}
static int prec(char x) {
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
return 0;
}
}
class EvaluateString {
public static int evaluate(String expression) {
char[] tokens = expression.toCharArray();
// Stack for numbers: 'values'
LinkedList<Integer> values = new LinkedList<Integer>();
// Stack for Operators: 'ops'
LinkedList<Character> ops = new LinkedList<Character>();
for (int i = 0; i < tokens.length; i++) {
// Current token is a whitespace, skip it
if (tokens[i] == ' ')
continue;
// Current token is a number, push it to stack for numbers
if (tokens[i] >= '0' && tokens[i] <= '9') {
StringBuffer sbuf = new StringBuffer();
// There may be more than one digits in number
while (i < tokens.length && tokens[i] >= '0'
&& tokens[i] <= '9')
sbuf.append(tokens[i++]);
values.push(Integer.parseInt(sbuf.toString()));
}
// Current token is an opening brace, push it to 'ops'
else if (tokens[i] == '(')
ops.push(tokens[i]);
// Closing brace encountered, solve entire brace
else if (tokens[i] == ')') {
while (ops.peek() != '(')
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
ops.pop();
}
// Current token is an operator.
else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*'
|| tokens[i] == '/') {
// While top of 'ops' has same or greater precedence to current
// token, which is an operator. Apply operator on top of 'ops'
// to top two elements in values stack
while (!ops.isEmpty() && hasPrecedence(tokens[i], ops.peek()))
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
// Push current token to 'ops'.
ops.push(tokens[i]);
}
}
// Entire expression has been parsed at this point, apply remaining
// ops to remaining values
while (!ops.isEmpty())
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
// Top of 'values' contains result, return it
return values.pop();
}
// Returns true if 'op2' has higher or same precedence as 'op1',
// otherwise returns false.
public static boolean hasPrecedence(char op1, char op2) {
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}
// A utility method to apply an operator 'op' on operands 'a'
// and 'b'. Return the result.
public static int applyOp(char op, int b, int a) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new UnsupportedOperationException("Cannot divide by zero");
return a / b;
}
return 0;
}
There is a mistake inside while loop in main method. See snippet below.
postFx = postFx.replaceAll("x", line);
System.out.println("Answer to expression : "
+ EvaluateString.evaluate(postFx));
Here postFx = postFx.replaceAll("x", line); you lost reference to postfix form that contains variable x. Subsequent calls of replaceAll doesn't have any effect. So expression with first entered value is evaluated.
You can easily fix it by replacing code above with
System.out.println("Answer to expression : "
+ EvaluateString.evaluate(postFx.replaceAll("x", line)));

Why isn't this program running properly? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
The assignment was to create a postfix to infix converter using Stacks. The program compiles properly but when I tried to make a demo class, I received a null point exception line 32. Please share any observations, better coding conventions, or solutions.
import java.util.Stack;
public class PostfixtoInfix {
private String expression;
private Stack<Character> s;
Character pOpen = new Character('(');
Character pClose = new Character(')');
public String PostfixtoInfix(String e) {
expression = e;
String output = "";
for (int i = 0; i < e.length(); i++) {
char currentChar = e.charAt(i);
if (isOperator(currentChar)) {
while (!s.empty() && s.peek() != pOpen
&& hasHigherPrecedence(s.peek(), currentChar)) {
output += s.peek();
s.pop();
}
s.push(currentChar);
} else if (isOperand(currentChar)) {
output += currentChar;
} else if (currentChar == '(') {
s.push(currentChar);
} else if (currentChar == ')') {
while (!s.empty() && s.peek() != pClose) {
output += s.peek();
s.pop();
}
}
while (!s.empty()) {
output += s.peek();
s.pop();
}
}
return output;
}
public boolean isOperator(char c) {
if (c == '+' || c == '-' || c == '/' || c == '*' || c == '^')
return true;
return false;
}
public boolean isOperand(char c) {
if (c >= '0' && c <= '9')
return true;
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return true;
return false;
}
public int getOperatorWeight(char operator) {
int weight = -1;
switch (operator) {
case '+':
case '-':
weight = 1;
break;
case '*':
case '/':
weight = 2;
break;
case '^':
weight = 3;
}
return weight;
}
public boolean hasHigherPrecedence(char operator1, char operator2) {
int op1 = getOperatorWeight(operator1);
int op2 = getOperatorWeight(operator2);
if (op1 == op2) {
if (isRightAssociative(operator1))
return false;
else
return true;
}
return op1 > op2 ? true : false;
}
public boolean isRightAssociative(char op) {
if (op == '^')
return true;
return false;
}
}
To fix the NPE initialize your objects. Unlike C++, Stack<Character> s; is equivalent to Stack<Character> s = null;; not to Stack<Character> s = new Stack<>();!
Beware of == and != not behaving as you might expect for boxed objects.
Character a = new Character('A');
Character aa = new Character('A');
System.out.println(a == aa);
gives the (correct!) answer false.
They are different objects. If you want to compare for equality, use either:
System.out.println(a.equals(aa));
System.out.println((char)a==(char)aa);
The first uses an explicit method for comparing the object contents. The second one avoids this problem by using non-object primitives, where equality is bitwise, not reference-equality.
It appears that you declare a private member s, never assign anything to it, and then attempt to use it in expressions like s.empty() and s.pop(). If nothing is ever assigned to s, then it is null, and attempting to call a method on it will result in a NullPointerException.
To create an empty stack, you probably want to change the declaration to:
private Stack <Character> s = new Stack<Character>();
First of all, you have a method looking like a constructor:
public String PostfixtoInfix(String e) {
try changing it to something else, like:
public String transform(String e) {
Second, your s field never gets assigned a stack. Put
s = new Stack<Character>();
in your constructor. Also, new Character('a') != new Character('a'), because that will bypass automatic (pillowweight cached) boxing. Use instead just simple chars as pOpen and pClose.
Your access modifier may be preventing the program to access the stack.
Change:
private Stack <Character> s;
to:
protected Stack <Character> s;
Read more here

Arithmetic calculation for BODMAS in Java

I have written this code for Bodmas, but getting some error in this. If I do 3-5+9, it will result in 3.04.0.
It just start concatenating, though it works for all other operations like *, / and -, please help.
public static String calculation(BODMASCalculation bodmas, String result) {
while (bodmas.hasMatch()) {
double value, leftOfOperator = bodmas.getLeft();
char op = bodmas.getOperator();
double rightOfOprator = bodmas.getRight();
switch (op) {
case '/':
if(rightOfOprator == 0) //Divide by 0 generates Infinity
value = 0;
else
value = leftOfOperator / rightOfOprator;
break;
case '*':
value = leftOfOperator * rightOfOprator;
break;
case '+':
value = leftOfOperator + rightOfOprator;
break;
case '-':
value = leftOfOperator - rightOfOprator;
break;
default:
throw new IllegalArgumentException("Unknown operator.");
}
result = result.substring(0, bodmas.getStart()) + value + result.substring(bodmas.getEnd());
bodmas = new BODMASCalculation(result);
}
return result;
}
Another function is:-
public boolean getMatchFor(String text, char operator) {
String regex = "(-?[\\d\\.]+)(\\x)(-?[\\d\\.]+)";
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(regex.replace('x', operator)).matcher(text);
if (matcher.find()) {
this.leftOfOperator = Double.parseDouble(matcher.group(1));
this.op = matcher.group(2).charAt(0);
this.rightOfOprator = Double.parseDouble(matcher.group(3));
this.start = matcher.start();
this.end = matcher.end();
return true;
}
return false;
}
I have a solution by adding
String sss = null;
if(op == '+' && !Str.isBlank(result.substring(0, bodmas.getStart())) && value >= 0)
sss = "+";
else
sss = "";
result = result.substring(0, bodmas.getStart()) + sss + value + result.substring(bodmas.getEnd());
But don't want to do that, I want this to work for all the operators.
import java.util.Stack;
public class EvaluateString
{
public static int evaluate(String expression)
{
char[] tokens = expression.toCharArray();
// Stack for numbers: 'values'
Stack<Integer> values = new Stack<Integer>();
// Stack for Operators: 'ops'
Stack<Character> ops = new Stack<Character>();
for (int i = 0; i < tokens.length; i++)
{
// Current token is a whitespace, skip it
if (tokens[i] == ' ')
continue;
// Current token is a number, push it to stack for numbers
if (tokens[i] >= '0' && tokens[i] <= '9')
{
StringBuffer sbuf = new StringBuffer();
// There may be more than one digits in number
while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9')
sbuf.append(tokens[i++]);
values.push(Integer.parseInt(sbuf.toString()));
}
// Current token is an opening brace, push it to 'ops'
else if (tokens[i] == '(')
ops.push(tokens[i]);
// Closing brace encountered, solve entire brace
else if (tokens[i] == ')')
{
while (ops.peek() != '(')
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
ops.pop();
}
// Current token is an operator.
else if (tokens[i] == '+' || tokens[i] == '-' ||
tokens[i] == '*' || tokens[i] == '/')
{
// While top of 'ops' has same or greater precedence to current
// token, which is an operator. Apply operator on top of 'ops'
// to top two elements in values stack
while (!ops.empty() && hasPrecedence(tokens[i], ops.peek()))
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
// Push current token to 'ops'.
ops.push(tokens[i]);
}
}
// Entire expression has been parsed at this point, apply remaining
// ops to remaining values
while (!ops.empty())
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
// Top of 'values' contains result, return it
return values.pop();
}
// Returns true if 'op2' has higher or same precedence as 'op1',
// otherwise returns false.
public static boolean hasPrecedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}
// A utility method to apply an operator 'op' on operands 'a'
// and 'b'. Return the result.
public static int applyOp(char op, int b, int a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
UnsupportedOperationException("Cannot divide by zero");
return a / b;
}
return 0;
}
// Driver method to test above methods
public static void main(String[] args)
{
System.out.println(EvaluateString.evaluate("10 + 2 * 6"));
System.out.println(EvaluateString.evaluate("100 * 2 + 12"));
System.out.println(EvaluateString.evaluate("100 * ( 2 + 12 )"));
System.out.println(EvaluateString.evaluate("100 * ( 2 + 12 ) / 14"));
}
}
The Java Scripting API allows you to pass parameters from Java application to the script engine and vice versa.
You can use Javax ScriptEngine to pass values from your app to a script.
And using it's eval() method, you can give it a mathematical expression in the form of a string and it will do the the math for you... (Handles BODMAS too).
Example:
ScriptEngineManager mgr = new.ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2/3*45";
System.out.println(engine.eval(foo));
Outputs: 70
Include the following imports , if the IDE doesn't suggest them :
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
Check the documentation here ScriptEngine
I hope it helps.
The solution can be achieved by using the shunting yard algorithm. We start by creating an infix notation of the equation then follows the postfix notation. Here is a description of the algorithm https://en.wikipedia.org/wiki/Shunting_yard_algorithm. This solution solves for any nested number of brackets on the string expression.
public static double evaluate(String exp) {
char[] tokens = exp.toCharArray();
Queue<Object> values = new LinkedList<>();
// Stack for Operators: 'ops'
Stack<Character> ops = new Stack<Character>();
for (int i = 0; i < tokens.length; i++) {
//infix
// Current token is a whitespace, skip it
if (tokens[i] == ' ') {
continue;
}
// Current token is a number, push it to stack for numbers
else if (tokens[i] >= '0' && tokens[i] <= '9') {
StringBuffer sbuf = new StringBuffer();
// There may be more than one digits in number
while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9') {
sbuf.append(tokens[i]);
if ((i+1)<tokens.length &&tokens[i + 1] >= '0' && tokens[i + 1] <= '9') {
i++;
} else {
break;
}
}
values.add(Double.parseDouble(sbuf.toString()));
} else if (tokens[i] == '*' || tokens[i] == '-' || tokens[i] == '/' || tokens[i] == '+') {
if (ops.isEmpty()) {
ops.push(tokens[i]);
continue;
}
char op1 = ops.peek();
boolean hasHighPrecedence = hasPrecedence(op1, tokens[i]);
if (hasHighPrecedence) {
char op = ops.pop();
values.add(op);
ops.push(tokens[i]);
} else {
ops.push(tokens[i]);
}
} else if (tokens[i] == '(') {
ops.push(tokens[i]);
} else if (tokens[i] == ')') {
while (ops.peek() != '(') {
values.add(ops.pop());
}
ops.pop();
}
}
while (!ops.isEmpty()) {
values.add(ops.pop());
}
//post fix
Stack<Double> numStack = new Stack<>();
while (!values.isEmpty()) {
Object val = values.poll();
if (val instanceof Character) {
char v = (Character) val;
if (v == '*' || v == '-' || v == '/' || v == '+') {
double num2, num1;
num1 = numStack.pop();
num2 = numStack.pop();
double ans = applyOp(v, num1, num2);
numStack.push(ans);
}
} else {
double num = (double) val;
numStack.push(num);
}
}
return numStack.pop();
}
public static double applyOp(char op, double b, double a) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
IllegalArgumentException("Cannot divide by zero");
return a / b;
}
return 0;
}
public static boolean hasPrecedence(char op1, char op2) {
if (op1 == '*' && op2 == '/') {
return false;
} else if (op1 == '/' && op2 == '*') {
return true;
} else if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return true;
} else if (op1 == '+' && op2 == '-') {
return true;
} else {
return false;
}
}
"BODMAS" is a not very operational rule. Especially addition and subtraction have the same precedence and are calculated from left to right 1-2+3-4+5 = (((1-2)+3)-4)+5.
The rule is for a nested loop.
Loop
replaceAll ( number ) --> number
replaceAll number `[*/]' number --> number op number
replaceAll number `[+-]' number --> number op number
Until nothing is replaced.
This ensures that 3-4/26+5 -2-> 3-26+5 -2-> 3-12+5 -3-> -9+5 -3-> -4

Undefined char in output of bracket-matching program using custom stack

My Java Program is below. It's my training exercise. The one implements stack stucture for special type of string parsing(string with delimiter).
This delimiter-matching program works by reading characters from the string one at
a time and placing opening delimiters when it finds them, on a stack. When it reads
a closing delimiter from the input, it pops the opening delimiter from the top of the
stack and attempts to match it with the closing delimiter. If they’re not the same
type (there’s an opening brace but a closing parenthesis, for example), an error
occurs. Also, if there is no opening delimiter on the stack to match a closing one, or
if a delimiter has not been matched, an error occurs. A delimiter that hasn’t been
matched is discovered because it remains on the stack after all the characters in the
string have been read.
I use Eclipse. My output is here:
Please enter String:
{}
ch0 = {
ch1 = }
chLabel1 = **UNDEFINED CHAR(SQUARE WITH QUESTION MARK INSIDE IT)**
Error at }**
Could you explain value of chLabel?
As I understand operator "|" (here, cause two operands have boolean type) - is "lazy", shortcut version of "||" operator. I've tested the program after substitution "|" for "||"-result is the same.
public class MyStack {
private int top=0;
private int maxSize=0;
private char[] charArray=null;
public MyStack(int size){
maxSize=size;
top=0;
charArray=new char[maxSize];
}
public void push(char ch){
charArray[top++]=ch;
}
public char pop(){
return charArray[top--];
}
public boolean isEmpty(){
if(top==0)
return true;
else return false;
}
public boolean isFull(){
if(top==(maxSize-1))
return true;
else return false;
}
}
class StringParse {
private String stringForParsing = null;
public StringParse(String string) {
this.stringForParsing = string;
}
public void parser() {
char[] chArr = stringForParsing.toCharArray();
MyStack mySt = new MyStack(chArr.length);
for (int i = 0; i < chArr.length; i++) {
char ch = chArr[i];
switch (ch) {
case '{':
case '(':
case '[':
mySt.push(ch);
System.out.println("ch" + i + " = " + ch);
break;
case '}':
case ')':
case ']':
if (mySt.isEmpty())
System.out.println("Error at" + ch);
else {
char chLabel = mySt.pop();
System.out.println("ch" + i + " = " + ch);
System.out.println("chLabel" + i + " = " + chLabel);
if ((chLabel == '{') && (ch == '}') | (chLabel == '(') && (ch == ')') | (chLabel == '[') && (ch == ']'))
break;
else {
System.out.println("Error at " + ch);
break;
} // end of second else
} //end of first else
default:
break;
} //end of switch
} //end of parser method
}
} //end of class
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
System.out.println("Please enter String:");
String s = br.readLine();
StringParse strP = new StringParse(s);
strP.parser();
}
}
There are two problems:
There's an error with the pop function.
Consider doing one push and then one pop:
top = 0
push
insert at position 0
set top to 1
pop
get position 1 (not set yet!)
set top to 0
You need to use pre-decrement instead of post-decrement, so charArray[top--] should be charArray[--top].
With this change I get chLabel1 = {.
Reiterating what I said in the comments...
| has higher precendence than &&(as opposed to || which has lower precedence) (see this)‌​,
thus a && b | c && d is the same as a && (b | c) && d,
as opposed to a && b || c && d which would be (a && b) || (c && d).
When changing the |'s to ||'s, I no longer get Error at }.
There may be a problem with your MyStack class
Using java.util.Stack gives me no error, just a "chLabel1 = {"
Error at } can be resolved by following Dukeling's advice and using || instead of |:
(chLabel == '{') && (ch == '}') || (chLabel == '(') && (ch == ')') || (chLabel == '[') && (ch == ']')
So, it looks like your code in MyStack.pop() doesn't return a valid char. I'll need to see your MyStack code to help further.

Categories