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

📄 linearequations3.java

📁 原创的线性方程组求解程序
💻 JAVA
字号:
/**
 * @(#)LinearEquations.java
 * @xiong 
 * @version 1.00 2008/7/2
 */

import java.util.Scanner;
import java.io.*;

public class LinearEquations3   
{
	private int n;
	private double matrix[][];
	private double precision;
	//private double x[];
	
	public LinearEquations3() //Constructor
	{
		Scanner input = new Scanner(System.in);	
		System.out.print("Enter the number of linear equations: ");
		n = input.nextInt();
		matrix = new double[n+1][n+2];
		Scanner input1 = new Scanner(System.in);	
		System.out.print("\nEnter precision you need: ");
		precision= input1.nextDouble();
		//x=new double[n];
		//System.out.println(n);
		//System.out.println(matrix[n-1][n]);	
	}
	
	public void inputAugmentedMatrix() //Input Augmented Matrix
	{
		System.out.println();
        for(int k=1;k<n+1;k++)
        {
            String str="";   
        	InputStreamReader stdin = new InputStreamReader(System.in);  //Entered by keyboard   
        	BufferedReader bufin = new BufferedReader(stdin);  
        	StringBuffer buffer = new StringBuffer();    
        	try   
        	{   
            	System.out.printf ("Please input line %d of augmented matrix(Coefficients should be separated by blanks!): ",k);   
     			str = bufin.readLine(); 
     			buffer.append(" "+str);  
      			//	System.out.println   ("Line you input is:   "+str);   
        	}   
        	catch(IOException E)   
        	{   
            	System.out.println("I/O errors!!!");   
        	} 
        	String[] temp = buffer.toString().replaceFirst(" ","").split("\\s+"); 
        	if(temp.length!=n+1)
        		System.out.println("Number of coefficients is wrong!");
	    	//int[] number = new int[temp.length]; 
	    	for(int i=0;i<temp.length;i++)
	    	{ 
				try
				{ 
					matrix[k][i+1] = Double.parseDouble(temp[i]); 
		    		//System.out.printf("matrix[%d][%d]=%f  ",k,i,matrix[k][i]); 
				}
				catch(Exception e)
				{ 
					System.out.println("Some illegal characters exist in your input!"); 
				}
	    	}
	    	System.out.println();
        }          
	}
	
	
	
		public void matrixOneColumnOutput()  //Output the result
		{
   			int i;
   			for(i=1;i<=n;++i)
	   			System.out.printf("\n x[%d]=%f\n", i, matrix[i][n+1]);
		}

		private boolean upTriangle(double U[][],int n,double precision)     //Return to calculate result
		{
    		int i, j;
			for(i=n; i>0;--i)
			{
	   			if (Math.abs(U[i][i])<precision)
		   			return true;
	   			for(j=i+1;j<=n;++j)
		   			U[i][n+1]-=U[i][j]*U[j][n+1];
	      		U[i][n+1]/=U[i][i];
			}
			return false;
		}

		private void swap(double a,double b)    //Swap value of two numbers
		{
   			double ftmp;
   			ftmp=a;
   			a=b;
   			b=ftmp;
		}

		public boolean gaussElimination_column_select() // Gauss column select algorithm
		{
   			int i,j,k;
   			for(i=1;i<n;++i)
   			{
 				for(k=i,j=i+1;j<=n;++j)			  			
 					if(Math.abs(matrix[j][i])>Math.abs(matrix[k][i]))  
				  		k=j;
			  	for(j=i;j<=n+1;++j)  //Swap row i and row k
				  	swap(matrix[i][j],matrix[k][j]);
	  			//Elimination using central element
			  	if(Math.abs(matrix[i][i])<precision)
	            	return true;
              	for (j=i+1;j<=n;++j)
	            	for(k=i+1;k<=n+1;++k)
	             		matrix[j][k]-=matrix[i][k]*matrix[j][i]/matrix[i][i];
   			}
	   		upTriangle(matrix,n,precision);
       		return false;
		}
	
}

⌨️ 快捷键说明

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