Created
November 27, 2019 17:08
-
-
Save wagfim/aa0c55e4b3d1b4ca12caa9da5f2fc928 to your computer and use it in GitHub Desktop.
Gauss Jordan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Main { | |
static int M = 10; //?? | |
// funcao principal | |
public static void main(String[] args) { | |
Scanner leitor = new Scanner(System.in); | |
int n = 3, flag = 0; | |
String pares; | |
int contador = 0; | |
/* | |
float a[][] = {{0, 2, 1, 4}, | |
{1, 1, 2, 6}, | |
{2, 1, 1, 7}}; | |
*/ | |
// | |
System.out.println("Informe os pares de pontos, utilize apenas espaços. Pressione Enter ao terminar:"); | |
pares = leitor.nextLine(); | |
//System.out.println(pares); | |
for (int i = 0; i < pares.length(); i++) { | |
if(!Character.toString(pares.charAt(i)).equals(" ")) { | |
contador++; | |
} | |
} | |
int pontos[] = new int[contador]; | |
int k = 0; | |
for (int i = 0; i < pares.length(); i++) { | |
String aux = Character.toString(pares.charAt(i)); | |
if(!aux.equals(" ")) { | |
pontos[k++] = Integer.parseInt(aux); | |
} | |
} | |
int ordemMatriz = contador / 2; | |
System.out.println("Qual a ordem da matriz"); | |
// n = Integer.parseInt(leitor.next()); | |
n = 3; | |
float a[][] = new float[ordemMatriz][ordemMatriz+1]; | |
k = 0; | |
int linhas = a.length - 1; | |
int colunas = a[0].length - 1; | |
for (int i = 0; i <= linhas; i++) { | |
for (int j = 0; j <= colunas; j++) { | |
///* | |
if(j != colunas) { | |
a[i][j] = pontos[k]; | |
} else { | |
k++; | |
a[i][j] = pontos[k]; | |
k++; | |
} | |
System.out.print(a[i][j] + " "); | |
//*/ | |
/* | |
if (j == colunas) { | |
k++; | |
} | |
a[i][j] = pontos[k]; | |
System.out.print(a[i][j] + " "); | |
*/ | |
} | |
System.out.println(); | |
} | |
leitor.nextLine(); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a[i].length; j++) { | |
System.out.println("Informe o binomio " + (j+1) + " da linha " + (i + 1)); | |
a[i][j] = leitor.nextFloat(); | |
} | |
} | |
// ordem da matriz | |
// executa operacao | |
flag = executaOperacoes(a, n); | |
if (flag == 1) { | |
flag = checarConsistencia(a, n, flag); | |
} | |
// exibe a matriz final | |
System.out.println("\nMatriz final: "); | |
exibeMatriz(a, n); | |
System.out.println(""); | |
// soluções, caso se aplique | |
PrintResult(a, n, flag); | |
} | |
// exibe a matriz | |
static void exibeMatriz(float a[][], int n) { | |
String vermelhoCiano = "\033[31;46m"; | |
String reset = "\033[0m"; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j <= n; j++) { | |
if (i == j) { | |
System.out.print(vermelhoCiano + a[i][j] + reset +" | "); | |
} else { | |
System.out.print(a[i][j] + " | "); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
//funcao para reduzir a matriz | |
// escalonação por linha | |
static int executaOperacoes(float a[][], int n) { | |
int i, j, k = 0, c, flag = 0, m = 0; | |
float pro = 0; | |
// operações elementares | |
for (i = 0; i < n; i++) { | |
if (a[i][i] == 0) { | |
c = 1; | |
while (a[i + c][i] == 0 && (i + c) < n) { | |
c++; | |
} | |
if ((i + c) == n) { | |
flag = 1; | |
break; | |
} | |
for (j = i, k = 0; k <= n; k++) { | |
float temp = a[j][k]; | |
a[j][k] = a[j + c][k]; | |
a[j + c][k] = temp; | |
} | |
} | |
for (j = 0; j < n; j++) { | |
// exclui diagonal principal | |
if (i != j) { | |
// converte matriz para linha reduzida | |
// escalonamento (diagonal ) | |
float p = a[j][i] / a[i][i]; | |
for (k = 0; k <= n; k++) { | |
a[j][k] = a[j][k] - (a[i][k]) * p; | |
} | |
} | |
} | |
} | |
return flag; | |
} | |
//funcao que exibe o resultado desejado se soluções unicas existem | |
//caso contrario exibe sem solução ou infinitas soluções | |
//a depender da entrada | |
static void PrintResult(float a[][], int n, int flag) { | |
System.out.print("Resultado: "); | |
if (flag == 2) { | |
System.out.println("Infinitas soluções"); | |
} else if (flag == 3) { | |
System.out.println("Não existe solução"); | |
} | |
//exibe a solução exibindo as constante por suas respectivas diagonais | |
//carece de correção para 1 | |
else { | |
char letra = 'x'; | |
for (int i = 0; i < n; i++) { | |
System.out.print(letra++ + ": "+ a[i][n] / a[i][i] + " "); | |
} | |
} | |
} | |
//checa infinatas soluções ou sem solução | |
static int checarConsistencia(float a[][], int n, int flag) { | |
int i, j; | |
float soma; | |
// flag == 2 solução infinita | |
// flag == 3 sem solução | |
flag = 3; | |
for (i = 0; i < n; i++) { | |
soma = 0; | |
for (j = 0; j < n; j++) { | |
soma = soma + a[i][j]; | |
} | |
if (soma == a[i][j]) { | |
flag = 2; | |
} | |
} | |
return flag; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment