📄 lu_decompound_for_c++.cpp
字号:
#include <iostream>
#include <iomanip>
using namespace std;
#define N 100
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
int n;
int i,j,r,k;
float a[N][N],b[N],l[N][N]={0},u[N][N]={0},y[N],x[N];
//通过键盘初始化方程
Status InitEquation() {
//设定系数矩阵A阶数
cout <<"Please input how many Line/Colmun your Matrix need:";
cin >>n;
if (n>100) {
cout <<endl<<"Sorry,Your Matrix is too large,please input anotherone";
return ERROR;
}
//初始化系数矩阵A
cout <<endl<<"Ok,Now you have a"<<n<<"*"<<n<<"Matrix."<<endl<<"please input the element of the Matrix:";
for (i=0;i<n;i++) {
for (j=0;j<n;j++) cin >>a[i][j];
}
//初始化常向量b
cout <<endl<<"Now please input Constent Constant Vector b:";
for (i=0;i<n;i++) cin >>b[i];
return OK;
}
//LU分解
Status LUDecompound() {
//1.求U的第一行
for (j=0;j<n;j++) u[0][j]=a[0][j];
//2.求L的第一列
for (i=0;i<n;i++) l[i][0]=a[i][0]/u[0][0];
//求L&U
for (r=1;r<n;r++) {
//求U的第R行
for (j=r;j<n;j++) {
u[r][j]=a[r][j];
for (k=0;k<r;k++) u[r][j]=u[r][j]-l[r][k]*u[k][j];
}
//求L的第R列
for (i=r+1;i<n;i++) {
l[i][r]=a[i][r];
for (k=0;k<r;k++) l[i][r]=l[i][r]-l[i][k]*u[k][r];
l[i][r]=l[i][r]/u[r][r];
}
}
return OK;
}
//回代求解
Status Bback_Substitution_Solve () {
//解Ly=b
y[0]=b[0];
for (i=0;i<n;i++) {
y[i]=b[i];
for (k=0;k<i;k++) y[i]=y[i]-l[i][k]*y[k];
}
//解Ux=y
x[n-1]=y[n-1]/u[n-1][n-1];
for (i=n-1;i>=0;i--) {
x[i]=y[i];
for (k=i+1;k<n;k++) x[i]=x[i]-u[i][k]*x[k];
x[i]=x[i]/u[i][i];
}
return OK;
}
//打印方程;
inline Status printEquation () {
cout <<endl<<"The Equation is:";
for (i=0;i<n;i++) {
cout <<endl;
for (j=0;j<n;j++) cout <<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<a[i][j]<<"X"<<j;
cout <<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<b[i];
}
return OK;
}
//打印L/U矩阵
inline Status printLU () {
cout <<endl<<"Lij is";
for (i=0;i<n;i++) {
cout <<endl;
for (j=0;j<n;j++) cout <<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<l[i][j];
}
cout <<endl<<"Uij is";
for (i=0;i<n;i++) {
cout <<endl;
for (j=0;j<n;j++) cout <<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<u[i][j];
}
return OK;
}
//打印解
inline Status printSolution() {
cout <<endl<<"Y is:";
for (i=0;i<n;i++) cout <<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<y[i];
cout<<endl<<"The answer X is:";
for (i=0;i<n;i++) cout <<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<x[i];
return OK;
}
Status main(){
int op;
cout<<endl<<"1.Initial the Equation.";
cout<<endl<<"2.Print the Equation.";
cout<<endl<<"3.Decompound the L/U Matrix.";
cout<<endl<<"4.Print the L/U Matrix.";
cout<<endl<<"5.Solve the Equation";
cout<<endl<<"6.print the Answer.";
cout<<endl<<"0.Exit";
cout<<endl<<"Please input what your want to do :";
cin >>op;
while (op!=0) {
switch (op) {
case 1 : InitEquation(); break;
case 2 : printEquation(); break;
case 3 : LUDecompound(); break;
case 4 : printLU(); break;
case 5 : Bback_Substitution_Solve(); break;
case 6 : printSolution(); break;
default : printf ("\nWrong operation!"); break;
}
cout<<endl<<"1.Initial the Equation.";
cout<<endl<<"2.Print the Equation.";
cout<<endl<<"3.Decompound the L/U Matrix.";
cout<<endl<<"4.Print the L/U Matrix.";
cout<<endl<<"5.Solve the Equation";
cout<<endl<<"6.print the Answer.";
cout<<endl<<"0.Exit";
cout<<endl<<"Please input what your want to do :";
cin >>op;
}
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -