Question
Java Programming: Using the dictionary class , modify your infix expression evaluator so that it can use variables. Create a dictionary. Add some variable names
Java Programming: Using the dictionary class , modify your infix expression evaluator so that it can use variables. Create a dictionary. Add some variable names and associated values to it, then modify your evaluator so that the text string can include the names of variables as well as constants. When you encounter a variable name, look it up in the dictionary and use the value you find there. Treat undefined variables as an error.
Dictionary class:
public class Dictionary { private HashClass theDict[]; private int size; private int used; public Dictionary(int maxSize) { this.theDict = new HashClass[maxSize]; this.size = maxSize; this.used = 0; } private int hash(String s) { int retval = 0; for(int i = 0; i < s.length(); i++) { retval += (int)s.charAt(i); } return retval % size; } public boolean isFull() { return used >= size-1; } private int findWhere(String s) // finds s or where it should be { int where = this.hash(s); while (theDict[where] != null && !theDict[where].getKey().equals(s)) { System.out.println("Ouch"); where = (where + 1) % size; } return where; } public void delete(String s) throws HashException { int where = findWhere(s); if(theDict[where] != null) { theDict[where] = null; used--; } else { throw new HashException("Missing Key" + s); } } public double lookup(String s) throws HashException { int where = findWhere(s); if(theDict[where] != null) return theDict[where].getValue(); else throw new HashException("Missing Key" + s); } public void insert(String s, double val) throws HashException { if(this.isFull()) { throw new HashException("Hash table overflow"); } int where = findWhere(s); if (theDict[where] == null) { theDict[where] = new HashClass(s, val); used++; } else theDict[where].setValue(val); } public String toString() { String retval = ""; for(int i = 0; i < size; i++) { if (theDict[i] != null) retval += i + " " + theDict[i] + " "; } return retval; } public static void main(String args[]) throws Exception { String words[] = {"cow", "aardvark", "axolotl", "hardware", "software", "tick", "waterbug", "tiger", "caribou", "dog", "clavicle", "patella", "appendix", "liver", "gizzard"}; Dictionary dict = new Dictionary(10); for(int i = 0; i < words.length; i++) { dict.insert(words[i], i*2.5); } System.out.println(dict); } }
Infix expression evaluttor (modify this one):
import java.util.Stack;
public class EvaluateExpression {
public static float solveExpression(String expression) {
char[] tokens = expression.toCharArray(); //Number Stack Stack
for (int i = 0; i < tokens.length; i++) { if (tokens[i] >= '0' && tokens[i] <= '9') { StringBuffer sbuf = new StringBuffer();
while (i < tokens.length && ((tokens[i] >= '0' && tokens[i] <= '9') || (tokens[i] == '.'))) { sbuf.append(tokens[i++]); }
valueStack.push(Float.parseFloat(sbuf.toString()));
} else if (tokens[i] == '(') { opsStack.push(tokens[i]); } else if (tokens[i] == ')') {
while (opsStack.peek() != '(') { valueStack.push(performOperation(opsStack.pop(), valueStack.pop(), valueStack.pop())); } opsStack.pop(); } else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*' || tokens[i] == '/') {
while (!opsStack.empty() && isPrecedence(tokens[i], opsStack.peek())) { valueStack.push(performOperation(opsStack.pop(), valueStack.pop(), valueStack.pop())); } opsStack.push(tokens[i]); } }
while (!opsStack.empty()) { valueStack.push(performOperation(opsStack.pop(), valueStack.pop(), valueStack.pop())); } return valueStack.pop();
}
public static boolean isPrecedence(char op1, char op2) { if (op2 == '(' || op2 == ')') { return false; } if ((op1 == '/' || op1 == '*') && (op2 == '-' || op2 == '+')) { return false; } else { return true; } } public static float performOperation(char op, float a, float b) {
switch (op) { case '+': return b + a;
case '-': return b - a;
case '*': return b * a;
case '/': if (a == 0) { throw new UnsupportedOperationException("Cannot divide by zero"); } return b / a; } return 0.0f; }
public static void main(String[] args) {
System.out.println("10.0 + 3 * 45.0 = " + EvaluateExpression.solveExpression("10.0 + 3 * 45.0")); System.out.println("100 * ( 2 + 12 ) = " + EvaluateExpression.solveExpression("100 * ( 2 + 12 )")); System.out.println("100 * ( 2 + 12 ) / 14 = " + EvaluateExpression.solveExpression("100 * ( 2 + 12 ) / 14"));
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started