I needHelp for these two java, please 1) Post Fix Calculator (PostFix.java) Crea
ID: 3816296 • Letter: I
Question
I needHelp for these two java, please
1) Post Fix Calculator (PostFix.java)
Create a post fix calculator (34+ vs 3+4) using a Stack. The calculator should be able to handle all basic mathematical operations (+,-,/,*) You will not use parenthesis, but the program would work like a traditional infix notation -> see example below.
Post fix example
1 2 + 3 * 6 + 2 3 + /
This means "take 1 and 2, add them, take 3 and multiply, take 6 and add, take 2 and 3, add them, and divide". In contrast, the equivalent expression in Infix
(((1 + 2) * 3) + 6) / (2 + 3)
This may seem more familiar, but note the need for parentheses to control the order of evaluation.
The user should be able to enter each number/operator into the Stack one at a time and type = (equals) to see their result:
3 (return/enter)
4 (return/enter)
+ (return/enter)
= (return/enter)
Your total is: 7
2) Bracket Checking (Brackets.java)
Write a program that examines a string of characters and determines if all opening bracket characters such as (, [, or { are matched with their correct closing characters. Use a stack to do this. The basic algorithm is as follows:
Read the string, character by character.
If the character is an opening bracket character, push its equivalent closing character onto the stack.
If the character is a closing bracket character, pop the stack, and see if the character you popped matches the current character. If not, the brackets in the string are not paired correctly.
If the character is not a bracket character, ignore it.
If you get through the whole string without failing, check to see if the stack is empty. If it is, the brackets are all matched correctly. If not, the brackets were not matched correctly.
Your program should print a message back to the user if the brackets are correctly matched or not
Test your program on some small strings. Once it is working properly, it should work regardless of the string. Try using it on an entire Java source code file.
Explanation / Answer
1. Postfix program
import java.util.*;
public class Postfix
{
public static void main(String args[])
{
String str;
System.out.println("Please enter postfix expression.");
Scanner scan = new Scanner(System.in);
str = scan.nextLine();
System.out.println("Result = " +evalPf(str));
}
public static int evalPf(String str)
{
Scanner sc = new Scanner(str);
int result = 0;
String op;
Stack<Integer> stack = new Stack<Integer>();
while (sc.hasNext())
{
if (sc.hasNextInt())
{
stack.push(sc.nextInt());
continue;
}
else
{
op = sc.next();
if(op.equals("+") || op.equals("-") || op.equals("*") || op.equals("/") || op.equals("%"))
{
int b = stack.pop();
int a = stack.pop();
if(op.equals("+")) stack.push(a + b);
else if (op.equals("-")) stack.push(a - b);
else if (op.equals("*")) stack.push(a * b);
else if (op.equals("/")) stack.push(a / b);
else if (op.equals("%")) stack.push(a % b);
}
else if (op.equals("=")) result = stack.pop();
}
}
return result;
}
}
2. Brackets checking Program
import java.util.*;
public class Brackets{
public static void main(String args[]) {
String str;
System.out.println("Please enter some expression with brackets.");
Scanner scan = new Scanner(System.in);
str = scan.nextLine();
if(balancedBrackets(str) == true)
System.out.println("Brackets are correctly matched.");
else
System.out.println("Brackets are not correctly matched.");
}
public static boolean balancedBrackets(String s) {
Stack<Character> stack = new Stack<Character>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
//Checking if the character is an opening bracket character, push its equivalent closing character onto the stack.
if(c == '[' || c == '(' || c == '{' ) {
stack.push(c);
}
//Checking if the character is a closing bracket character, pop the stack, and see if the character you popped matches the current character.
else if(c == ']') {
if(stack.isEmpty()) return false;
if(stack.pop() != '[') return false;
}
else if(c == ')') {
if(stack.isEmpty()) return false;
if(stack.pop() != '(') return false;
}
else if(c == '}') {
if(stack.isEmpty()) return false;
if(stack.pop() != '{') return false;
}
}
return stack.isEmpty();
}
}