Package ru.ifmo.ctddev.tsarev.task1

Source Code of ru.ifmo.ctddev.tsarev.task1.Function

package ru.ifmo.ctddev.tsarev.task1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Function {
  private MultivariableExpression expr;
  private String func;

  public Function(File input) throws FileNotFoundException, CalcException {
    Scanner sc = new Scanner(input);
    try {
      init(sc);
    } finally {
      if (sc.ioException() != null) {
        System.out.println("Error when trying to read from file \"" + input.getName() + "\"");
        return;
      }
      sc.close();
    }
  }

  public Function(Scanner sc) throws ParsingException, CalcException {
    init(sc);
  }
 
  public Function() {
    expr = null;
    func = "";
  }
 
  private void init(Scanner sc) throws ParsingException, CalcException {
    func = sc.nextLine();
    Pattern whithoutOperator = Pattern.compile("(\\d|x)\\s+(\\d|x)");
    Matcher matches = whithoutOperator.matcher(func);
    if (matches.find()) {
      throw new ParsingException("Operator is ommited");
    }

    func = func.replaceAll("\\s", "");
    expr = new MultivariableExpression(func);
  }
 
  public double compute(Map<String, Double> points) throws CalcDivisionByZeroException, CalculatingException {
    if (expr == null) {
      throw new CalculatingException("Wrong Input");
    }
    return expr.compute(points);
  }

  public void write(File output) throws FileNotFoundException, CalcException {
    List<Function> oneFunction = new ArrayList<Function>();
    oneFunction.add(this);
    OutputGenerator.printTable(output, oneFunction);
  }
}
TOP

Related Classes of ru.ifmo.ctddev.tsarev.task1.Function

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.