📜  单纯形法的 c# 代码 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:14.987000             🧑  作者: Mango

代码示例1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static string[] variables = { "a", "b", "c", "d", "e", "f", "g" };
        static string[] input = 
        {
            "max 4 5",
            "9 8 <= 45",
            "5 1 <= 29",
            "1 7 <= 15"
        };
        static  List> coeifficiants1 = new List>();
        static  List> coeifficiants2 = new List>();
        static List operations = new List();
        static string command;
        static List constraints = new List();
        static List output = new List();
        static void Main(string[] args)
        {
            ReadInput();
            CreateCommand();
        }
        static void ReadInput()
        {
            string[] fields;
            double newDouble;
            string operation = "";
            //read input lines
            foreach (string line in input)
            {
                fields = line.Split(' ');
                //check if line is command - not starting with a number
                if (double.TryParse(fields[0], out newDouble))
                {
                    Boolean firstMatrix = true;
                    List matrix1row = new List();
                    List matrix2row = new List();
                    //line contains coeifficants
                    foreach (string field in fields)
                    {
                        if (!double.TryParse(field, out newDouble))
                        {
                            operation = field;
                            firstMatrix = false;
                        }
                        else
                        {
                            newDouble = double.Parse(field);
                            if (firstMatrix == true)
                                matrix1row.Add(newDouble);
                            else
                                matrix2row.Add(newDouble);
                        }
                    }
                    coeifficiants1.Add(matrix1row);
                    coeifficiants2.Add(matrix2row);
                    operations.Add(operation);
                }
                else
                {
                    command = fields[0];
                    for (int i = 1; i < fields.Length; i++)
                    {
                        newDouble = double.Parse(fields[i]);
                        constraints.Add(newDouble);
                    }
                }
            }
        }
        static void CreateCommand()
        {
            string outputline = "";
            switch (command)
            {
                case "max":
                    outputline = "Maximize p = ";
                    break;
            }
            for(int i = 0; i < constraints.Count; i++)
            {
                if (i == 0)
                {
                    outputline += constraints[i].ToString();
                }
                else
                {
                    outputline += "+ ";
                    outputline += constraints[i].ToString();
                }
                outputline += variables[i];
             
            }
            outputline += " subject to";
            output.Add(outputline);
            
            for (int row = 0; row < coeifficiants1.Count; row++)
            {
                double[] coeifficiants = coeifficiants1[row].ToArray();
                outputline = "";
                for (int i = 0; i < coeifficiants.Length; i++)
                {
                    if (i == 0)
                    {
                        outputline += coeifficiants[i].ToString();
                    }
                    else
                    {
                        outputline += " + ";
                        outputline += coeifficiants[i].ToString();
                    }
                    outputline += variables[i];
                }
                outputline += " " + operations[row] + " "  +coeifficiants1[row][0];
                output.Add(outputline);
            }
 
        }
    }
}