//***********************************************************************
//*Beschreibung			: Gauss-Algorithmus			 					*
//*Autor				: Thomas Tardy									*
//*Datum				: 23.01.03										*
//*Fach					: Mathematik									*
//***********************************************************************

public class GaussProg
{
	static public double [][] matrix;
	static public double [] x;

	static public void main (String [] args)
	{
		int n;

		n = einlesen();
		System.out.println("\n\n\nIhre eingegebene Matrix lautet:\n");
		ausgabeMatrix(n);
		berechnung(n);
		System.out.println("\n\n\nDie Berechnete Matrix lautet:\n");
		ausgabeMatrix(n);
		System.out.println("\n\n\nDas Resultat lautet:\n");
		ausgabeX(n);
	}

	static public int einlesen()
	{
		double wert;
		int width = 0, height = 0;
		String zeile;
		String dumy;
		Double bla;
		boolean Exit;

		System.out.println("***GAUSS-ALGORITHMUS***\n\n");
		System.out.print("Definieren Sie die Zeilenanzahl n der Matrix: ");
		int n = InOut.getInt();
		InOut.getLine();
		System.out.println("\n\nDie Faktoren auf einer Zeile werden durch Leerschlaege getrennt.");
		System.out.println("Kommt ein Faktor nicht vor, also x = 0, geben Sie 0 ein.\n");
		System.out.println("Der letzte Wert entschpricht dem Resultat.\n\n");
		matrix = new double [n] [n+1];

		for (; height<n; height++)
		{
			System.out.println("Geben Sie die "+(height+1)+". Zeile ein");
			zeile = InOut.getLine();

			for(int i=0;i<zeile.length();i++)
			{
				dumy = "";
				Exit = false;
				for(int k=i; Exit == false && k < zeile.length();k++)
				{
					if (zeile.charAt(k)!=' ')
					{
						dumy = dumy + zeile.charAt(k);
					}
					else
					{
						Exit = true;
					}
					i=k;
				}

				if(dumy.equals("") == false)
				{
					bla = Double.valueOf(dumy);
					wert = bla.doubleValue();

					matrix [height] [width] = wert;
					width++;
				}
			}
			width = 0;
		}
		return n;
	}

	static public void ausgabeMatrix(int n)
	{
		int height, width;
		for(height = 0;height<n;height++)
		{
			for(width = 0;width<n;width++)
			{
				System.out.print("n"+(height+1)+(width+1)+"= ");
				InOut.print((matrix[height][width]),5,3);
				System.out.print("       ");
			}
			System.out.print("b"+(height+1)+"= ");
			InOut.print((matrix[height][n]),5,3);
			System.out.println("");
		}
	}

	static public void berechnung(int n)
	{
		x = new double [n];
		double k1, k2;
		double q;
		int height, width;
		for(height=0; height<n-1; height++)
		{
			for(int i = 1; i<n-height; i++)
			{
				k1 = matrix[height+i][height];
				k2 = matrix[height][height];
				for(width = 0; width<=n; width++)
				{
					matrix[height+i][width]=(k1*matrix[height][width])-(k2*matrix[height+i][width]);
				}
			}
		}
		for(int j = 1; j<=n; j++)
		{
			if(j == 1)
			{
				if(matrix[n-j][n-j]==0)
				{
					if(matrix[n-j][n]==0)
					{
						System.out.println("\n\n\n!!!x"+(n-j)+" kann beliebig sein. Das heisst die Resultate sind Vielfache von Lambda!!!");
						x[n-j] = 1;
					}
					else
					{
						System.out.println("\n\n\n!!!Das Gleichungssystem hat keine Loesung!!!");
						for(int bla = 1; bla<=n; bla++)
						{
							x[n-j] = 0;
						}
						break;
					}
				}
				else
				{
					x[n-j] = matrix[n-j][n]/matrix[n-j][n-j];
				}
			}
			else
			{
				q = 0;
				for(int l = 1; l<j; l++)
				{
					q += matrix[n-j][n-j+l]*x[n-j+l];
				}
				x[n-j] = ((matrix[n-j][n])-q)/matrix[n-j][n-j];
			}
		}
	}

	static public void ausgabeX(int n)
	{
		int width;
		for(width = 0;width<n;width++)
		{
			System.out.print("x"+(width+1)+"= ");
			InOut.print(x[width],6,3);
			System.out.print("       ");
		}
		System.out.println("");
	}
}