package ru.ifmo.ctddev.meynster.calc;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class FileCalc {
public static String ENCODING = "UTF8";
public static void main(String[] args) {
if (args.length != 2) {
System.out.println(
"Usage: java FileCalc [input file] [output file]");
return;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(args[0]), ENCODING));
PrintStream ps = new PrintStream(new FileOutputStream(args[1]),
true, ENCODING);
String expression;
List<Map<List<Integer>, String>> allResults
= new ArrayList<Map<List<Integer>, String>>();
while ((expression = reader.readLine()) != null) {
Function function = new Function(expression);
allResults.add(function.results());
}
ps.format("%3s%3s%3s", "x", "y", "z");
for (int i = 0; i < allResults.size(); i++) {
ps.format("%12s", "f" + (i + 1));
}
ps.println();
for (List<Integer> variables : Variables.getTuples()) {
for (Integer variable : variables) {
ps.format("%3s", variable);
}
for (int j = 0; j < allResults.size(); j++) {
ps.format("%12s", allResults.get(j).get(variables));
}
ps.println();
}
ps.close();
if (ps.checkError()) {
throw new IOException();
}
reader.close();
} catch (ExpressionException e) {
System.out.println(
"One of expressions in input file was incorrect.");
} catch (IOException e) {
System.out.println(
"Sorry, unexpected problems with files occured.");
}
}
}