⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tikrinimas.java

📁 Cubic spline algorithm approximating function y=f(x) - ((-2)*x*(2*x+3))/(x*x+4*x+5) in interval [-2
💻 JAVA
字号:
import java.io.*;
import java.lang.String.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.util.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;

public class Tikrinimas {
	static int length = 3;
	static String[] x = new String[length];
	static String[] y = new String[length];
	static double[] xD = new double[length];
	static double[] yD = new double[length];
	static double[] h = new double[length];
	static double[] f = new double[length];
	static double[] a = new double[length-1];
	static double[] b = new double[length];
	static double[] c = new double[length-1];
	static double[] d = new double[length];
	static double[] C = new double[length];
	static double[] D = new double[length];
	static double[] g = new double[length];
	static double[] e = new double[length];
	static double[] G = new double[length];
	static double[] H = new double[length];
	static double[] Sa = new double[length];
	static double[] Sb = new double[length];
	static double[] Sc = new double[length];
	static double[] Sd = new double[length];

	static double[] an = new double[length];
	static double[] bn = new double[length];
	static double[] cn = new double[length];
	

	public static void main (String[] args) {
		double tmp = 0;
		int tmp2 = 0;
		int availibility = 0;

		System.out.println("------------------------------");
		System.out.println("Kubinio splaino skaiciavimas");
		System.out.println("Uzduoties numeris - 17");
		System.out.println("Autorius - Vaidas Zidanavicius");
		System.out.println("------------------------------\n");

		dataInitialize();

		System.out.println("-------------------------------------------------------------------------------");
		System.out.println("Pradiniai duomenys:");
		startUpData2();
		System.out.println("\n-------------------------------------------------------------------------------");

		for (tmp2 = 0; tmp2 < (length - 1); tmp2++) {
			if (a[tmp2] < 0) {
				an[tmp2] = -(a[tmp2]);
			}
			else {
				an[tmp2] = a[tmp2];
			}
			if (b[tmp2] < 0) {
				bn[tmp2] = -(b[tmp2]);
			}
			else {
				bn[tmp2] = b[tmp2];
			}
			if (c[tmp2] < 0) {
				cn[tmp2] = -(c[tmp2]);
			}
			else {
				cn[tmp2] = c[tmp2];
			}

			if (bn[tmp2] >= (an[tmp2] + cn[tmp2])) {
				availibility = 1;
			}
		}

		if (availibility == 1) {
			System.out.println("Dalyba is nulio arba labai mazo skaiciaus perkelties metodo eigoje negalima");
			calculate_C();
			calculate_D();
			calculate_g();
		}
		else {
			System.out.println("Negalioja triistrizaines vyravimo salyga");
		}
    }

    public static void dataInitialize() {
		int buffer = 1;
		int pos = 0;
		int spaces = 0;
		char ch;
		String element = "";

		try {
			DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("dataX.txt")));
			while (in.available() != 0) {
				ch = (char)in.readByte();
				if (buffer == 1) {
					if (ch != '\n') {
						if (ch != ' ') {
							element = element + ch;
						}
						else {
							spaces++;
							if (spaces == 1) {
								b[0] = Double.parseDouble(element);
								element = "";
							}
							else if (spaces == 2){
								c[0] = Double.parseDouble(element);
								element = "";
							}
						}
					}
					else {
						buffer++;
						d[0] = Double.parseDouble(element);
						element = "";
						pos = 0;
						spaces = 0;
					}
				}
				else if (buffer == 2) {
					if (ch != '\n') {
						if (ch != ' ') {
							element = element + ch;
						}
						else {
							spaces++;
							if (spaces == 1) {
								a[0] = Double.parseDouble(element);
								element = "";
							}
							else if (spaces == 2) {
								b[1] = Double.parseDouble(element);
								element = "";
							}
							else {
								c[1] = Double.parseDouble(element);
								element = "";
							}
						}
					}
					else {
						buffer++;
						d[1] = Double.parseDouble(element);
						element = "";
						pos = 0;
						spaces = 0;
					}
				}
				else {
					if (ch != '\n') {
						if (ch != ' ') {
							element = element + ch;
						}
						else {
							spaces++;
							if (spaces == 1) {
								a[1] = Double.parseDouble(element);
								element = "";
							}
							else {
								b[2] = Double.parseDouble(element);
								element = "";
							}
						}
					}
					else {
						buffer++;
						d[2] = Double.parseDouble(element);
						element = "";
						pos = 0;
					}
				}
			}
			d[2] = Double.parseDouble(element);
			element = "";
			pos = 0;
		} catch (IOException e) {
			System.out.println("Klaida! Nepavyko nuskaityti is failo (data.txt)");
			System.exit(1);
		}
	}

	public static void convertToDouble() {
		int pos = 0;

		for (pos = 0; pos < length; pos++) {
			xD[pos] = Double.parseDouble(x[pos]);
			yD[pos] = Double.parseDouble(y[pos]);
		}
	}

	public static void startUpData() {
		int pos = 0;

		System.out.print("X = ");
		for (pos = 0; pos < length; pos++) {
			if (pos != (length - 1)) {
				if ((xD[pos] * 100) % 10 == 0.0) {
					System.out.print("  " + xD[pos] + "| ");
				}
				else {
					System.out.print(" " + xD[pos] + "| ");
				}
			}
			else {
				if ((xD[pos] * 100) % 10 == 0.0) {
					System.out.println("  " + xD[pos]);
				}
				else {
					System.out.print(" " + xD[pos]);
				}
			}
		}
		System.out.print("\nY = ");
		for (pos = 0; pos < length; pos++) {
			if (pos != (length - 1)) {
				if ((yD[pos] * 1000) % 10 == 0.0) {
					System.out.print(" " + yD[pos] + "| ");
				}
				else {
					System.out.print(yD[pos] + "| ");
				}
			}
			else {
				if ((yD[pos] * 1000) % 10 == 0.0) {
					System.out.println(" " + yD[pos]);
				}
				else {
					System.out.print(yD[pos]);
				}
			}
		}
	}

	public static void startUpData2() {
		int pos = 0;

		System.out.println("############ a ############");
		for (pos = 0; pos < (length - 1); pos++) {
			System.out.println("a" + pos + " = " + a[pos]);
		}
		System.out.println("############ b ############");
		for (pos = 0; pos < length; pos++) {
			System.out.println("b" + pos + " = " + b[pos]);
		}
		System.out.println("############ c ############");
		for (pos = 0; pos < (length - 1); pos++) {
			System.out.println("c" + pos + " = " + c[pos]);
		}
		System.out.println("############ d ############");
		for (pos = 0; pos < length; pos++) {
			System.out.println("d" + pos + " = " + d[pos]);
		}
		System.out.println("###########################");
	}

	public static void calculate_h() {
		int pos = 0;

		System.out.println("\n--------------- h ---------------");

		for (pos = 0; pos < (length - 1); pos++) {
			h[pos] = xD[pos + 1] - xD[pos];
			System.out.println("h" + pos + " = " + h[pos]);
		}
	}

	public static void calculate_f() {
		int pos = 0;

		System.out.println("\n--------------- f ---------------");

		for (pos = 0; pos < (length - 1); pos++) {
			f[pos] = (yD[pos + 1] - yD[pos])/h[pos];
			System.out.println("f(x" + pos + ", x" + (pos + 1) + ") = " + f[pos]);
		}
	}

	public static void calculate_a() {
		int pos = 0;

		a[0] = 0;
		a[length - 1] = 0;

		for (pos = 1; pos < (length - 1); pos++) {
			a[pos] = h[pos - 1];
		}

		System.out.println("\n--------------- a ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("a" + pos + " = " + a[pos]);
		}
	}

	public static void calculate_b() {
		int pos = 0;

		b[0] = 1;
		b[length - 1] = 1;

		for (pos = 1; pos < (length - 1); pos++) {
			b[pos] = 2 * (h[pos] + h[pos - 1]);
		}

		System.out.println("\n--------------- b ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("b" + pos + " = " + b[pos]);
		}		
	}

	public static void calculate_c() {
		int pos = 0;

		c[0] = 0;
		c[length - 1] = 0;

		for (pos = 1; pos < (length - 1); pos++) {
			c[pos] = h[pos];
		}

		System.out.println("\n--------------- c ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("c" + pos + " = " + c[pos]);
		}
	}

	public static void calculate_d() {
		int pos = 0;

		d[0] = 0;
		d[length - 1] = 0;

		for (pos = 1; pos < (length - 1); pos++) {
			d[pos] = 6 * (f[pos] - f[pos - 1]);
		}

		System.out.println("\n--------------- d ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("d" + pos + " = " + d[pos]);
		}
	}

	public static void calculate_C() {
		int pos = 0;

		C[0] = -(c[0] / b[0]);
		C[1] = -(c[1] / ((a[0] * C[0]) + b[1]));

		System.out.println("C0 = " + C[0]);
		System.out.println("C1 = " + C[1]);

		System.out.println("\n--------------- C ---------------");
		for (pos = 0; pos < 2; pos++) {
			System.out.println("C" + pos + " = " + C[pos]);
		}
		//System.out.println("\n\n");

		/*C[0] = -(c[0] / b[0]);

		for (pos = 1; pos < (length - 1); pos++) {
			C[pos] = -(c[pos] / (a[pos] * C[pos - 1] + b[pos]));
		}

		System.out.println("\n--------------- C ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("C" + pos + " = " + C[pos]);
		}*/
	}

	public static void calculate_D() {
		int pos = 0;

		D[0] = d[0] / b[0];
		D[1] = (d[1] - (a[0] * D[0])) / ((a[0] * C[0]) + b[1]);
		D[2] = (d[2] - (a[1] * D[1])) / ((a[1] * C[1]) + b[2]);

		System.out.println("\n--------------- D ---------------");
		for (pos = 0; pos < 3; pos++) {
			System.out.println("D" + pos + " = " + D[pos]);
		}
		//System.out.println("\n");

		/*D[0] = d[0] / b[0];
		D[length - 1] = (d[length - 1] - a[length - 1] * D[length - 2]) / (a[length - 1] * C[length - 2] + b[length - 1]);

		for (pos = 1; pos < (length - 1); pos++) {
			D[pos] = (d[pos] - a[pos] * D[pos - 1]) / (a[pos] * C[pos - 1] + b[pos]);
		}

		System.out.println("\n--------------- D ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("D" + pos + " = " + D[pos]);
		}*/
	}

	public static void calculate_g() {
		int pos = 0;

		g[length - 1] = D[length - 1];

		for (pos = (length - 2); pos >= 0; pos--) {
			g[pos] = C[pos] * g[pos + 1] + D[pos];
		}

		System.out.println("\n--------------- x ---------------");
		for (pos = 0; pos < length; pos++) {
			System.out.println("x" + pos + " = " + g[pos]);
		}
	}

	public static void calculate_e() {
		int pos = 0;

		for (pos = 0; pos < (length - 1); pos++) {
			e[pos] = f[pos] - g[pos + 1] * (h[pos] / 6) - g[pos] * (h[pos] / 3);
		}

		System.out.println("\n--------------- e ---------------");
		for (pos = 0; pos < (length - 1); pos++) {
			System.out.println("e" + pos + " = " + e[pos]);
		}
	}
	
	public static void calculate_G() {
		int pos = 0;

		for (pos = 0; pos < (length - 1); pos++) {
			G[pos] = g[pos] / 2;
		}

		System.out.println("\n--------------- G ---------------");
		for (pos = 0; pos < (length - 1); pos++) {
			System.out.println("G" + pos + " = " + G[pos]);
		}
	}

	public static void calculate_H() {
		int pos = 0;

		for (pos = 0; pos < (length - 1); pos++) {
			H[pos] = (g[pos + 1] - g[pos]) / (6 * h[pos]);
		}

		System.out.println("\n--------------- H ---------------");
		for (pos = 0; pos < (length - 1); pos++) {
			System.out.println("H" + pos + " = " + H[pos]);
		}
	}

	public static void showSplineMatrix() {
		int pos = 0;

		System.out.println("\n");

		for (pos = 0; pos < (length - 1); pos++) {
			Sa[pos] = yD[pos] - e[pos] * xD[pos] + G[pos] * xD[pos] * xD[pos] - H[pos] * xD[pos] * xD[pos] * xD[pos];
			Sb[pos] = e[pos] - 2 * G[pos] * xD[pos] + 3 * H[pos] * xD[pos] * xD[pos];
			Sc[pos] = G[pos] - 3 * H[pos] * xD[pos];
			Sd[pos] = H[pos];
		}

		System.out.println("                            x                x^2               x^3");

		for (pos = 0; pos < (length - 1); pos++) {
			System.out.println(Sa[pos] + " " + Sb[pos] + " " + Sc[pos] + " " + Sd[pos]);
		}

		System.out.println("\n");
	}

	public static double Spline(double x1) {
		int buffer = 0;
		double Spl = 0;
		if ((x1 >= xD[0]) && (x1 <= xD[length - 1])) {
			for (buffer = 0; buffer <= (length - 2); buffer++) {
				if ((x1 >= xD[buffer]) && (x1 <= xD[buffer + 1])) {
					Spl = yD[buffer] + e[buffer] * (x1 - xD[buffer]) + G[buffer] * (x1 - xD[buffer]) * (x1 - xD[buffer]) + H[buffer] * (x1 - xD[buffer]) * (x1 - xD[buffer]) * (x1 - xD[buffer]);
				}
			}
        }
        else {
			System.out.println("X'as nepriklauso intervalui");
			System.exit(1);
		}

        return Spl;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -