//执行环境是VC 6.0
//通过高斯消元法求解方程的解
//input:
// 2 5 8
// 9 2 12
//output:
// x1:1.073171
// x2:1.170732
//方程如下:
//{2x + 5y = 8
//{9x + 2y = 12
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
int n, i, j, k;
double client, temp = 0.0;
double **a;
printf("输入方式如下(系数以0表示无),最后一排是B的值:\n");
printf("4 5 2 3 2 5\n");
printf("4 6 2 1 0 2\n");
printf("4 5 2 1 3 2\n");
printf("1 2 1 2 3 2\n");
printf("0 2 5 1 1 3\n");
printf("请输入未知量的个数:");
scanf("%d", &n);
printf("\n请输入系数矩阵和右端向量\n");
//分配内存空间
a = new double *[n];
for (i =0 ; i < n; i++)
a[i]= new double[n + 1];
//输入数据
for (i = 0; i < n; i++)
for (j = 0; j <= n; j++)
scanf("%lf", (*(a + i) + j));
for(k = 0; k < n - 1; k++)
for(i = k + 1; i < n; i++)
{
client = a[i][k]/a[k][k];
for(j = k + 1; j < n; j++)
a[i][j] = a[i][j] - client * a[k][j];
a[i][n] = a[j - 1][n] - client * a[k][n];
}
a[n - 1][n] = a[n - 1][n]/a[n - 1][n - 1];
for(i = n - 2; i >= 0; i--)
{
for (j = i + 1; j < n; j++)
temp += a[i][j] * a[j][n];
a[i][n] = (a[i][n] - temp) / a[i][i];
}
for(i = 0; i < n; i++)
printf("X%d = %lf\n", i + 1, a[i][n]);
}