Advertisement
damoncard

Polynomial

Apr 3rd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.PrintWriter;
  3. import java.io.File;
  4.  
  5. public class PlotPolynomial
  6. {
  7.   public static double calValue(double[] values, double raw, int degree)
  8.   {
  9.    double result = 0;
  10.    for (int q = degree ; q >= 0 ; q--)
  11.    {
  12.     result += values[degree-q] * Math.pow(raw, q);
  13.    }
  14.    return result;
  15.   }
  16.  
  17.   public static void main (String [] args) throws Exception
  18.   {
  19.    Scanner in = new Scanner(System.in);
  20.    File f = new File ("plot.txt");
  21.    PrintWriter pw = new PrintWriter(f);
  22.    int count = Integer.parseInt(in.next());
  23.    double [] values = new double [count+1];
  24.    for (int q = 0 ; q < values.length ; q++)
  25.    {
  26.     values[q] = Double.parseDouble(in.next());
  27.    }
  28.    pw.println("  x\t  f(x)");
  29.    for (double p = -10 ; p != 10.5 ; p += 0.5)
  30.    {
  31.     double result = calValue(values, p, count);
  32.     String v = String.valueOf(p);
  33.     if (v.charAt(v.length()-1) == '0') v = v.substring(0, v.length()-2);
  34.     pw.printf("%4s\t%.2f", v, result);
  35.     pw.println();
  36.    }
  37.    pw.close();
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement