C#高斯完全主元法

本文介绍了一种使用C#实现的高斯完全主元法,该方法用于求解线性方程组。程序首先接收用户输入的方程阶数和系数,然后通过查找最大值、行列交换和消元过程逐步简化方程,最后通过回代求得方程的解。整个过程详细展示了高斯消元法的步骤及其在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Program类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 高斯完全主元法
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----------高斯完全主元法----------");
            Console.WriteLine();
            Gauss g = new Gauss();
            g.OutPutSolution();
        }
    }
}

Gauss类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 高斯完全主元法
{
    class Gauss
    {
        //输入参数
        static void InPut(ref int n, ref double[,] a, ref int[] lz)
        {
            Console.WriteLine("请输入方程的阶数:");
            Console.WriteLine();
            n = int.Parse(Console.ReadLine());
            lz = new int[n];
            for (int k = 0; k < n; k++)
            {
                lz[k] = k + 1;
            }
            //开辟内存,指定下标
            a = new double[n, n + 1];
            Console.WriteLine();
            Console.WriteLine("请输入方程的系数A[i,j]");
            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                string r = Console.ReadLine();
                //Split函数:将字符串r拆分
                string[] rs = r.Split(' ');
                for (int j = 0; j < n + 1; j++)
                {
                    a[i, j] = double.Parse(rs[j]);
                }
            }
            Console.WriteLine();
            Console.WriteLine("————A[i,j]输入完毕————");
            Console.WriteLine();
        }

        //查找最大值所在的行列
        static void FindMax(int k, int n, double[,] a, ref int[] IJ)
        {
            double Max = a[k, k];
            int x = k;
            int y = k;
            for (int i = k; i <= n - 1; i++)
            {
                for (int j = k; j <= n - 1; j++)
                {
                    if (a[i, j] > Max)
                    {
                        x = i;
                        y = j;
                        Max = a[i, j];
                    }
                }
            }
            IJ[0] = x;
            IJ[1] = y;
        }

        //行列交换
        static void Exchange(int n, int k, ref double[,] a, int[] IJ, ref int[] lz)
        {
            double teap;
            if (IJ[0] != k)
            {
                for (int j = k; j <= n; j++)
                {
                    //交换行
                    teap = a[k, j];
                    a[k, j] = a[IJ[0], j];
                    a[IJ[0], j] = teap;
                }
            }
            if (IJ[1] != k)
            {
                for (int i = k; i <= n - 1; i++)
                {
                    //交换列
                    teap = a[i, k];
                    a[i, k] = a[i, IJ[1]];
                    a[i, IJ[1]] = teap;
                }
                //更改数组 lz 
                int col = lz[k];
                lz[k] = lz[IJ[1]];
                lz[IJ[1]] = col;
            }
        }

        //消元
        static void Elimination(int n, ref double[,] a, ref int[] IJ, int[] lz)
        {
            for (int k = 0; k <= n - 2; k++)
            {
                FindMax(k, n, a, ref IJ);
                Exchange(n, k, ref a, IJ, ref lz);
                for (int i = k + 1; i <= n - 1; i++)
                {
                    double lik = a[i, k] / a[k, k];
                    for (int j = k + 1; j <= n; j++)
                    {
                        a[i, j] = a[i, j] - lik * a[k, j];
                    }
                    a[i, k] = 0;
                }
                Console.WriteLine($"\n——————————第{k + 1}次消元结果——————————\n");
                OutPut(n, a);
            }
        }

        //回代
        static void BackSubstitution(int n, double[,] a, ref double[] x)
        {
            x = new double[n];
            for (int i = n - 1; i >= 0; i--)
            {
                double sum = 0;
                for (int j = i + 1; j <= n - 1; j++)
                {
                    sum = sum + a[i, j] * x[j];
                }
                x[i] = (a[i, n] - sum) / a[i, i];
            }

        }
        //方程系数输出
        static void OutPut(int n, double[,] a)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    Console.Write("{0,8:F2}", a[i, j]);
                }
                //控制换行
                Console.WriteLine();
            }
        }
        //结果输出
        static void OutPut(double[] x, int[] lz)
        {
            for (int i = 1; i <= x.Length; i++)
            {
                int k = Array.IndexOf(lz, i);//直接进行搜索
                Console.WriteLine("X{0}={1,6:f3}", i, x[k]);
            }
        }

        public void OutPutSolution()
        {
            int n = 0;
            double[,] a = null;
            int[] lz = null;//记录下标
            int[] IJ = new int[2];//记录最大值的行列值
            double[] x = null;
            //输入数据
            InPut(ref n, ref a, ref lz);
            Console.WriteLine("\n——————————方程系数如下——————————\n");
            OutPut(n, a);
            //消元
            Elimination(n, ref a, ref IJ, lz);
            Console.WriteLine("\n————————方程最终消元结果如下————————\n");
            OutPut(n, a);
            BackSubstitution(n, a, ref x);
            Console.WriteLine("\n——————————方程的解如下——————————\n");
            OutPut(x, lz);
            Console.WriteLine();
            Console.WriteLine("按任意键继续…………");
            Console.Read();
        }
    }
}

结果输出
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值