C++实现有分数结果的高斯消元法
2020-08-09 本文已影响0人
一路向后
1.程序源码
#include <iostream>
using namespace std;
#define MAX 100
//定义分数类
class Fractional
{
public:
int getstd();
int putstd();
int iszero();
friend Fractional operator+(const Fractional &a, const Fractional &b);
friend Fractional operator-(const Fractional &a, const Fractional &b);
friend Fractional operator*(const Fractional &a, const Fractional &b);
friend Fractional operator/(const Fractional &a, const Fractional &b);
private:
int a;
int b;
static int gcd(int c, int d);
static int reduce(int *c, int *d);
};
int Fractional::iszero()
{
int c = (a == 0);
return c;
}
int Fractional::gcd(int c, int d)
{
if(d == 0)
return c;
else
return gcd(d, c%d);
}
int Fractional::reduce(int *c, int *d)
{
int flag = 0;
int x = 0;
if(*d < 0)
{
*c = -*c;
*d = -*d;
flag = 1;
}
if(*c > 0)
{
x = gcd(*c, *d);
if(x > 1)
{
*c /= x;
*d /= x;
flag = 1;
}
}
else if(*c < 0)
{
x = gcd(-*c, *d);
if(x > 1)
{
*c /= x;
*d /= x;
flag = 1;
}
}
else
*d = 1;
return flag;
}
int Fractional::getstd()
{
cin >> a;
b = 1;
return 0;
}
int Fractional::putstd()
{
if(b == 1)
{
cout << a;
}
else
{
cout << a << "/" << b;
}
return 0;
}
Fractional operator+(const Fractional &a, const Fractional &b)
{
Fractional c;
int x;
int y;
int z;
if(a.b != b.b)
{
x = Fractional::gcd(a.b, b.b);
y = a.b / x;
z = b.b / x;
c.a = a.a * z + b.a * y;
c.b = y * b.b;
}
else
{
c.a = a.a + b.a;
c.b = a.b;
}
Fractional::reduce(&c.a, &c.b);
return c;
}
Fractional operator-(const Fractional &a, const Fractional &b)
{
Fractional c;
int x;
int y;
int z;
if(a.b != b.b)
{
x = Fractional::gcd(a.b, b.b);
y = a.b / x;
z = b.b / x;
c.a = a.a * z - b.a * y;
c.b = y * b.b;
}
else
{
c.a = a.a - b.a;
c.b = a.b;
}
Fractional::reduce(&c.a, &c.b);
return c;
}
Fractional operator*(const Fractional &a, const Fractional &b)
{
Fractional c;
c.a = a.a * b.a;
c.b = a.b * b.b;
Fractional::reduce(&c.a, &c.b);
return c;
}
Fractional operator/(const Fractional &a, const Fractional &b)
{
Fractional c;
c.a = a.a * b.b;
c.b = a.b * b.a;
Fractional::reduce(&c.a, &c.b);
return c;
}
template <class T>
int input_matrix(T **A, T *b)
{
int n = 0;
int i, j;
cout << "系数矩阵A的阶数: ";
cin >> n;
for(i=0; i<n; i++)
{
cout << "系数矩阵第" << i+1 << "行元素" << endl;
for(j=0; j<n; j++)
A[i][j].getstd();
}
cout << "等式右端项b: ";
for(i=0; i<n; i++)
b[i].getstd();
return n;
}
//代模板的高斯消元法
template <class T>
int gauss_sovle(T **A, T *b, T *X)
{
T mik;
T s;
int n;
int i, j, k;
n = input_matrix(A, b);
//消元
for(k=0; k<n-1; k++)
{
if(A[k][k].iszero())
return -1;
for(i=k+1; i<n; i++)
{
mik = A[i][k] / A[k][k];
for(j=k; j<n; j++)
{
A[i][j] = A[i][j] - mik * A[k][j];
}
b[i] = b[i] - mik * b[k];
}
}
cout << "消元后的矩阵A: " << endl;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
A[i][j].putstd();
cout << " ";
}
cout << endl;
}
//回代
X[n-1] = b[n-1] / A[n-1][n-1];
for(k=n-2; k>=0; k--)
{
s = b[k];
for(j=k+1; j<n; j++)
{
s = s - A[k][j] * X[j];
}
X[k] = s / A[k][k];
}
cout << "结果X: ";
for(i=0; i<n; i++)
{
X[i].putstd();
cout << " ";
}
cout << endl;
return 0;
}
//主函数
int main()
{
Fractional *A[MAX];
Fractional B[MAX][MAX];
Fractional b[MAX];
Fractional X[MAX];
int i = 0;
for(i=0; i<MAX; i++)
A[i] = B[i];
gauss_sovle(A, b, X);
return 0;
}
2.编译源码
$ g++ -o gauss_frac gauss_frac.cpp
3.运行程序
./gauss_frac
系数矩阵A的阶数: 3
系数矩阵第1行元素
1 3 4
系数矩阵第2行元素
1 4 7
系数矩阵第3行元素
9 3 2
等式右端项b: 5 3 2
消元后的矩阵A:
1 3 4
0 1 3
0 0 38
结果X: -37/38 197/38 -91/38