postfix notation - InFixToPostFix java with equations -
using 1 input file map variable value , file equations. problem equation such ((a+b)-(c-d))/(e-f)
calculates correctly until (a+b)-(c-d)
know has map.put('z', temp)
, as.push('z')
don't know how else make work properly.
input file one:
a = 8.0 b = -1.0 c = 7.0 d = -4.0 e = 11.0 f = 9.0 g = 3.2 h = 6.325 = 8 j = -3.333894
input file two:
(((a + b) - (c - d)) / (e - f)) (((a))) (a) ((a (b d) d))
program:
import java.util.*; import java.io.*; public class infixtopostfix { static final string p_error = "parenthesis match error"; static final string e_error = "empty infix expression error"; static map<character, integer> out_stack = new hashmap<character, integer>(); static map<character, integer> in_stack = new hashmap<character, integer>(); public static void main(string[] args) { map<character, double> map = null; try { map = populate(); }catch(ioexception e) { e.printstacktrace(); } generate(); system.out.print("load infix file: "); try { bufferedreader in = new bufferedreader(new filereader(getfile())); string line = ""; while((line = in.readline()) != null) { if(!match(line)) { system.out.println(line + " --> " + p_error); continue; } string expression = convert(line, map); if(expression.equals("")) { system.out.println(line + " --> " + e_error); continue; } system.out.println(line + " --> " + expression + " --> " + evaluate(expression, map)); } in.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch(ioexception e) { e.printstacktrace(); } } static boolean isoperand(char operand, map<character, double> map) { if(map.containskey(operand)) return true; return false; } static string convert(string infix, map<character, double> map) { arraystack<character> = new arraystack<character>(infix.length()); string post_expression = ""; int index = 0; while(index < infix.length()) { char current = infix.charat(index); if(current == ' ') { index++; continue; } if(isoperand(current, map)) { post_expression += current; } else if(current == '(') { as.push(current); } else if(current == ')') { while(as.top() != '(') { post_expression += as.pop(); } as.pop(); } else { while(!as.isempty() && in_stack.get(as.top()) > out_stack.get(current)) { post_expression += as.pop(); } as.push(current); } index++; } while(!as.isempty()) { post_expression += as.pop(); } return post_expression; } static double evaluate(string postfix, map<character, double> map) { arraystack = new arraystack(postfix.length()); int index = 0; double temp = 0.00; while(index < postfix.length()) { char current = postfix.charat(index); if(current == ' ') { index++; continue; } if(isoperand(current, map)) { as.push(current); temp = map.get(current); } else { char = (char) as.pop(); char b = (char) as.pop(); switch(current) { case '+' : temp = map.get(b) + map.get(a); break; case '-' : temp = map.get(b) - map.get(a); break; case '*' : temp = map.get(b) * map.get(a); break; case '/' : temp = map.get(b) / map.get(a); break; case '%' : temp = map.get(b) % map.get(a); break; } map.put('z', temp); as.push('z'); } index++; } if(as.size() == 1) { as.pop(); } else { system.out.println("there error"); } return temp; } static map<character, double> populate() throws ioexception{ map<character, double> map = new hashmap<character, double>(); system.out.print("load map file: "); bufferedreader in = new bufferedreader(new filereader(getfile())); string line = ""; while((line = in.readline()) != null) { string parts[] = line.split(" = "); char c = parts[0].charat(0); map.put(c, double.parsedouble(parts[1].trim())); } in.close(); return map; } static void generate() { out_stack.put('(', 100); out_stack.put('(', 0); out_stack.put('^', 6); out_stack.put('*', 3); out_stack.put('/', 3); out_stack.put('%', 3); out_stack.put('+', 1); out_stack.put('-', 1); in_stack.put('(', 0); in_stack.put(')', null); in_stack.put('^', 5); in_stack.put('*', 4); in_stack.put('/', 4); in_stack.put('%', 4); in_stack.put('+', 2); in_stack.put('-', 2); } static string getfile() { scanner kb = new scanner(system.in); return kb.nextline(); } static boolean match(string s) { parenmatch p = new parenmatch(); return p.parenmatch(s); } }
Comments
Post a Comment