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

📄 gauss1.cpp

📁 这事高斯法解方程组实例
💻 CPP
字号:
//the simplest gauss-elimination method
#include <iostream.h>
#include <iomanip.h>

const int N=4;

void main()
{
	double a[N][N+1],x[N];

	cout<<"input coefficient matrix!"<<endl;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			cin>>a[i][j];	//input coefficient matrix
	cout<<"input constant of right side!"<<endl;
	for(i=0;i<N;i++)
		cin>>a[i][N];		//input constant of right side

	//消元过程
	cout<<"the steps of elimination:"<<endl;
	for(int k=0;k<N-1;k++)
	{
		//消元
		for(int i=k+1;i<N;i++)
		{
			double temp=a[i][k]/a[k][k];	//消元系数
			for(int j=0;j<=N;j++)			//第i 行消元, from 0 to k column is meaningless!
				a[i][j]-=a[k][j]*temp;
		}

		//消元后输出
		for(i=0;i<N;i++)
		{
			for(int j=0;j<N+1;j++)
				cout<<setw(5)<<a[i][j];
			cout<<endl;
		}
		cout<<endl;
	}

	//回代求解
	x[N-1]=a[N-1][N]/a[N-1][N-1];
	for(k=N-2;k>=0;k--)
	{
		double c=0.0;
		for(int j=k+1;j<N;j++)
			c+=a[k][j]*x[j];
		x[k]=(a[k][N]-c)/a[k][k];
	}

	//输出解
	cout<<"The Key to Equations: "<<endl;
	for(k=0;k<N;k++)
		cout<<"x["<<k<<"]= "<<x[k]<<endl;
	cout<<endl;
}

⌨️ 快捷键说明

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