?? trecia.java
字號:
/*
* Varianto numeris - 5
* Metodai: Zeidelio ir Didziausio nuolydzio
*/
import java.io.*;
import java.lang.*;
public class Trecia {
public static int TASK_ID = 0;
public static double PRECISION = 0;
public static int N = 0;
public static double[][] matrixA; //matricos
public static double[] matrixB;
public static double[][] matrixC;
public static double[][] matrixD;
public static double[][] matrixFull;
public static void dataInitialize() {
int row = 0;
int column = 0;
String line = "";
char[] charLine;
int pos = 0;
String element = "";
matrixA = new double[4][4];
matrixB = new double[4];
matrixC = new double[4][4];
matrixD = new double[4][4];
matrixFull = new double[4][5];
int checker = 0;
try {
BufferedReader in = new BufferedReader(new FileReader("data.txt"));
while ((line = in.readLine()) != null) {
if (line.equals("N")) {
N = Integer.parseInt(in.readLine());
}
else if (line.equals("TASK_ID")) {
TASK_ID = Integer.parseInt(in.readLine());
}
else if (line.equals("PRECISION")) {
PRECISION = Double.parseDouble(in.readLine());
}
else if (line.equals("MATRIX_B")) {
row = -1;
column = -1;
pos = 0;
line = in.readLine();
while (pos < line.length()) {
if (line.charAt(pos) != ' ') {
if (line.charAt(pos) != '\n') {
element = element + line.charAt(pos);
}
else {
row++;
matrixB[row] = Double.parseDouble(element);
element = "";
}
}
else {
row++;
matrixB[row] = Double.parseDouble(element);
element = "";
}
pos++;
}
row++;
matrixB[row] = Double.parseDouble(element);
element = "";
}
else if (line.equals("MATRIX_C")) {
row = -1;
column = -1;
pos = 0;
checker = 0;
while (row < 3) {
row++;
pos = 0;
column = -1;
element = "";
line = in.readLine();
while (pos < line.length()) {
if (line.charAt(pos) == ' ') {
column++;
matrixC[row][column] = Double.parseDouble(element);
element = "";
}
else {
element = element + line.charAt(pos);
}
pos++;
}
column++;
matrixC[row][column] = Double.parseDouble(element);
}
row++;
element = "";
line = in.readLine();
}
else if (line.equals("MATRIX_D")) {
row = -1;
column = -1;
pos = 0;
checker = 0;
while (row < 3) {
row++;
pos = 0;
column = -1;
element = "";
line = in.readLine();
while (pos < line.length()) {
if (line.charAt(pos) == ' ') {
column++;
matrixD[row][column] = Double.parseDouble(element);
element = "";
}
else {
element = element + line.charAt(pos);
}
pos++;
}
column++;
matrixD[row][column] = Double.parseDouble(element);
}
row++;
element = "";
line = in.readLine();
}
else {
}
}
in.close();
} catch (IOException e) {
System.out.println("Klaida! Nepavyko nuskaityti is failo (data.txt)");
System.exit(1);
}
for (row = 0; row < N; row++) {
for (column = 0; column < N; column++){
matrixA[row][column] = (matrixD[row][column] + TASK_ID * matrixC[row][column]);
}
}
for (row = 0; row < N; row++) {
for (column = 0; column < 5; column++){
if (column == 4) {
matrixFull[row][column] = matrixB[row];
}
else {
matrixFull[row][column] = matrixA[row][column];
}
}
}
//tikrinam ar simetrine A matrica
for (row = 0; row < N; row++) {
for (column = 0; column < N; column++) {
if (matrixA[row][column] != matrixA[column][row]) {
System.out.println("\nNESIMETRINE MATRICA!");
System.exit(1);
}
}
}
}
public static void StartUpData() {
int row = 0;
int column = 0;
System.out.println("\n");
System.out.println("--------------------- PRADINIAI DUOMENYS ---------------------");
System.out.println("------ A ------");
for (row = 0; row < N; row++) {
for (column = 0; column < N; column++){
System.out.print(matrixA[row][column] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("------ B ------");
for (row = 0; row < N; row++) {
System.out.println(matrixB[row] + " ");
}
System.out.println();
System.out.println("------ C ------");
for (row = 0; row < N; row++) {
for (column = 0; column < 4; column++){
System.out.print(matrixC[row][column] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("------ D ------");
for (row = 0; row < N; row++) {
for (column = 0; column < 4; column++){
System.out.print(matrixD[row][column] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("------ Duota sistema (matrixFull) ------");
for (row = 0; row < N; row++) {
for (column = 0; column < 5; column++){
if (column == 4) {
matrixFull[row][column] = matrixB[row];
System.out.print("| " + matrixFull[row][column] + " ");
}
else {
matrixFull[row][column] = matrixA[row][column];
System.out.print(matrixFull[row][column] + " ");
}
}
System.out.println();
}
System.out.println();
System.out.println("------ TASK_ID -----");
System.out.println(TASK_ID);
System.out.println("\n------ PRECISION -----");
System.out.println(PRECISION);
System.out.println("\n------ N -----");
System.out.println(N);
System.out.println("-------------------------------------------------------------\n\n\n");
}
public static double Round(double number, int degree) {
double p = (double)Math.pow(10,degree);
number = number * p;
double tmp = Math.round(number);
return (double)tmp/p;
}
public static void ReorganizedMatrix() {
int row = 0;
int column = 0;
int buffer = 0;
String doubleToString = "";
int deg = 7;
System.out.println("\n------------------- pertvarkyta sistema ------------------");
for (row = 0; row < N; row++) {
for (column = 0; column < N; column++) {
if (row != column) {
matrixFull[row][column] = matrixFull[row][column] / matrixFull[row][row];
}
doubleToString = "" + Round(matrixFull[row][column], deg);
System.out.print(doubleToString);
for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 3); buffer++) {
System.out.print(" ");
}
}
matrixFull[row][N] = matrixFull[row][N] / matrixFull[row][row];
System.out.print(" | " + Round(matrixFull[row][N], deg) + "\n");
}
}
public static void JacobiMethod() {
int row = 0;
int column = 0;
int iteration = 0;
double[][] x;
double maximum = 0.0;
double difference = 0.0;
double round = 0.0;
int buffer = 0;
String doubleToString = "";
int deg = 11;
dataInitialize();
//StartUpData();
x = new double[200][4];
System.out.println("\n\n\n\n\n\n");
System.out.println("###############################################################################");
System.out.println(" JAKOBIO METODAS");
System.out.println("###############################################################################");
ReorganizedMatrix();
iteration = 0;
for (row = 0; row < N; row++) {
x[iteration][row] = 0;
}
System.out.println("\n\n\n\n---------------------------");
System.out.println("Sprendinio artiniu lentele:");
do {
System.out.print((iteration + 1) + " iter.: ");
for (row = 0; row < N; row++) {
x[iteration + 1][row] = matrixFull[row][N];
for (column = 0; column < N; column++) {
if (row != column) {
x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration][column];
}
}
if (((iteration + 1) < 10) && (row == 0)) {
System.out.print(" ");
}
round = Round(x[iteration][row], deg);
doubleToString = "" + round;
System.out.print(doubleToString);
for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 7); buffer++) {
System.out.print(" ");
}
}
System.out.println();
maximum = 0;
for (row = 0; row < N; row++) {
difference = Math.abs(x[iteration + 1][row] - x[iteration][row]);
if (difference < 0) {
difference = (-1) * difference;
}
if (maximum < difference) {
maximum = difference;
}
}
iteration++;
} while (maximum > PRECISION);
System.out.println();
System.out.println("###############################################################################");
System.out.println("\n\n");
}
public static void ZeidelMethod() {
int row = 0;
int column = 0;
int iteration = 0;
double[][] x;
double maximum = 0.0;
double difference = 0.0;
double round = 0.0;
int buffer = 0;
String doubleToString = "";
int deg = 11;
dataInitialize();
x = new double[200][4];
System.out.println("\n\n\n\n\n\n");
System.out.println("###############################################################################");
System.out.println(" ZEIDELIO METODAS");
System.out.println("###############################################################################");
ReorganizedMatrix();
iteration = 0;
for (row = 0; row < N; row++) {
x[iteration][row] = 0;
}
System.out.println("\n\n\n\n---------------------------");
System.out.println("Sprendinio artiniu lentele:\n");
do {
System.out.print((iteration + 1) + " iter.: ");
for (row = 0; row < N; row++) {
x[iteration + 1][row] = matrixFull[row][N];
for (column = 0; column < N; column++) {
if (row != column) {
if (row == 1) {
if (column == 0) {
x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration + 1][column];
}
else {
x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration][column];
}
}
else if (row == 2) {
if ((column == 0) || (column == 1)) {
x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration + 1][column];
}
else {
x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration][column];
}
}
else if (row == 3) {
if ((column == 0) || (column == 1) || (column == 2)) {
x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration + 1][column];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -