In: Computer Science
Write a program that performs the following two tasks in java
Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
code
solution
//output
/copyable code
// Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
String format = "";
do {
System.out.print("enter formula be in (infix/postfix):");
format = s1.nextLine().toLowerCase();
}while(!(format.equals("infix") || format.equals("postfix")));
System.out.print("please Enter your formula:");
String formula = s1.nextLine().replaceAll(" ", "");
String infix, postfix;
switch(format) {
case "infix":
infix = formula;
postfix = arthimetic.infix_To_Postfix(infix);
System.out.println("Infix:"+infix);
System.out.println("Postfix:"+postfix);
System.out.println("Answer:"+arthimetic.solvePostfix(postfix));
break;
case "postfix":
postfix = formula;
infix = arthimetic.postfixToInfix(postfix);
System.out.println("Infix:"+infix);
System.out.println("Postfix:"+postfix);
System.out.println("Answer:"+arthimetic.solvePostfix(postfix));
break;
}
}
}
//arithmetic.java
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class arthimetic {
public static boolean precedence(char firstval, char secondval) {
if (secondval == '(' || secondval == ')')
return false;
if ((firstval == '*' || firstval == '/') && (secondval == '+' || secondval == '-'))
return false;
else
return true;
}
public static java.awt.Point highest_point( List<java.awt.Point> points){
Iterator < java.awt.Point> pointIterator = points.iterator();
java.awt.Point highest_point = null;
while(pointIterator.hasNext()) {
java.awt.Point p = pointIterator.next();
if(highest_point == null)
highest_point = p;
else if(highest_point != null && p.getY() > highest_point.getY() )
highest_point = p;
}
return highest_point;
}
public class Outer{
int value = 5;
public void displayString() {
class InnerClass1{
int value2 = 10;
public String getString() {
return "Outer:" + value + ", Inner:" + value2;
}
}
InnerClass1 ic = new InnerClass1();
System.out.println(ic.getString());
}
int all_Even_Bits(int x) {
int maskdata = 0xAAAAAAAA;
int answer = x | maskdata ;
return ~answer;
}
void Test(){
displayString();
}
}
public static String infix_To_Postfix(String infix) {
char character[] = infix.toCharArray();
Stack<Character> st = new Stack<Character>();
for (int i = 0; i < character.length; i++) {
if (character[i] == '(')
st.push(character[i]);
if (character[i] == ')') {
if (st.isEmpty()) {
return "No matching open parenthesis error";
}
st.pop();
}
}
if (!st.isEmpty())
return "No matching error";
// initialize
String postfixString = "";
// initialize the stack
Stack<Character> stack1 = new Stack<Character>();
// index
for (int i = 0; i < infix.length(); i++) {
char ch = infix.charAt(i);
if (Character.isLetter(ch) || Character.isDigit(ch))
postfixString = postfixString + ch;
else if (ch == '(') {
stack1.push(ch);
} else if (ch == ')') {
while (stack1.peek() != '(') {
postfixString = postfixString + stack1.pop();
}
stack1.pop();
} else {
while (!stack1.isEmpty() && !(stack1.peek() == '(') && precedence(ch, stack1.peek()))
postfixString = postfixString + stack1.pop();
stack1.push(ch);
}
}
while (!stack1.isEmpty())
postfixString = postfixString + stack1.pop();
return postfixString;
}
public static boolean isch(char ch) {
return (ch >= 'a' && ch <= 'z') ||
(ch>= 'A' && ch <= 'Z') || Character.isDigit(ch);
}
public static String postfixToInfix(String postfix) {
Stack<String> stack = new Stack<String>();
for (int i = 0; i < postfix.length(); i++)
{
if (isch(postfix.charAt(i)))
stack.push(postfix.charAt(i) + "");
else
{
String op1 = stack.peek();
stack.pop();
String op2 = stack.peek();
stack.pop();
stack.push("(" + op2 + postfix.charAt(i) +
op1 + ")");
}
}
return stack.peek();
}
public static double solvePostfix(String s) {
Stack<Double> postfix = new Stack<Double>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c))
postfix.push((double) (c - '0'));
else {
double val1 = postfix.peek();
postfix.pop();
double val2 = postfix.peek();
postfix.pop();
switch (c) {
case '+':
postfix.push(val2 + val1);
break;
case '-':
postfix.push(val2 - val1);
break;
case '/':
postfix.push(val2 / val1);
break;
case '*':
postfix.push(val2 * val1);
break;
}
}
}
return postfix.peek();
}
}