C#中电阻网络的图形数据结构(我做对了吗?)

时间:2018-10-16 10:50:28

标签: data-structures weighted-graph

我正在尝试使电阻示意图中的点之间达到电阻。 请注意,它可以是(几乎)每种电阻配置中的任何原理图。

这是我尝试做的自己,但不知道如何进一步发展:

中继器的对象表示形式

class Resistor
{
    public string name;
    public int value;
    //Dictionary<string, List<Tuple<string,string, int>>> connecties = new Dictionary<string, List<Tuple<string, string, int>>>();
    public List<Tuple<string, string, int>> A = new List<Tuple<string, string, int>>();
    public List<Tuple<string, string, int>> B = new List<Tuple<string, string, int>>();
    public Resistor(string name,int value)
    {
        this.name = name;
        this.value = value;
        A.Add(Tuple.Create(name, "B", value));
        B.Add(Tuple.Create(name, "A", value));
    }
    public void AddConnectionA(Resistor resistor,string poot)
    {
        if (poot.Equals("A"))
        {
            A.Add(Tuple.Create(resistor.name, "A", 0));
            A.Add(Tuple.Create(resistor.name, "B", resistor.value));
            B.Add(Tuple.Create(resistor.name, "A", value));
            B.Add(Tuple.Create(resistor.name, "B", resistor.value+value));
            resistor.A.Add(Tuple.Create(name, "A", 0));
            resistor.A.Add(Tuple.Create(name, "B", value));
            resistor.B.Add(Tuple.Create(name, "A", resistor.value));
            resistor.B.Add(Tuple.Create(name, "B", resistor.value + value));
        }
        if (poot.Equals("B"))
        {
            A.Add(Tuple.Create(resistor.name, "B", 0));
            A.Add(Tuple.Create(resistor.name, "A", resistor.value));
            B.Add(Tuple.Create(resistor.name, "B", value));
            B.Add(Tuple.Create(resistor.name, "A", resistor.value + value));
            resistor.A.Add(Tuple.Create(name, "A", resistor.value));
            resistor.A.Add(Tuple.Create(name, "B", resistor.value + value));
            resistor.B.Add(Tuple.Create(name, "A", 0));
            resistor.B.Add(Tuple.Create(name, "B",value));
        }
    }
    public void AddConnectionB(Resistor resistor,string poot)
    {
        if (poot.Equals("A"))
        {
            A.Add(Tuple.Create(resistor.name, "A", value));
            A.Add(Tuple.Create(resistor.name, "B", resistor.value + value));
            B.Add(Tuple.Create(resistor.name, "A", 0));
            B.Add(Tuple.Create(resistor.name, "B", resistor.value));
            resistor.A.Add(Tuple.Create(name, "A", value));
            resistor.A.Add(Tuple.Create(name, "B", 0));
            resistor.B.Add(Tuple.Create(name, "A", resistor.value + value));
            resistor.B.Add(Tuple.Create(name, "B", resistor.value));
        }
        if (poot.Equals("B"))
        {
            A.Add(Tuple.Create(resistor.name, "B", value));
            A.Add(Tuple.Create(resistor.name, "A", resistor.value+value));
            B.Add(Tuple.Create(resistor.name, "B", 0));
            B.Add(Tuple.Create(resistor.name, "A", resistor.value));
            resistor.A.Add(Tuple.Create(name, "A", resistor.value + value));
            resistor.A.Add(Tuple.Create(name, "B", resistor.value));
            resistor.B.Add(Tuple.Create(name, "A", value));
            resistor.B.Add(Tuple.Create(name, "B", 0));
        }
    }
}

这使我可以在电阻器的两侧添加连接(“ poot” A或B,您可以将它们视为端子)。

-用于打印所有当前连接的代码:

        Random random = new Random();
        List<Resistor> resistorList = new List<Resistor>();
        for (int i =0; i < 3; i++)
        {
            Resistor resistor = new Resistor("R"+i.ToString(),random.Next(0,10000));
            resistorList.Add(resistor);
            Console.WriteLine(resistor.name +"\t"+ resistor.value);
        }

        resistorList[0].AddConnectionA(resistorList[1], "A"); //add connection to R0 on terminal A to R1 on terminal A
        resistorList[1].AddConnectionB(resistorList[2], "A"); //add connection to R1 on terminal B to R2 on terminal A

        foreach (Resistor resistor in resistorList)
        {
            foreach(Tuple<string,string,int> tuple in resistor.A)
            {
                Console.WriteLine(resistor.name + " heeft op terminal A connectie met " + tuple.Item1 + " op terminal " + tuple.Item2 + " met totale weerstand van " + tuple.Item3);
            }
            foreach (Tuple<string, string, int> tuple in resistor.B)
            {
                Console.WriteLine(resistor.name + " heeft op terminal B connectie met " + tuple.Item1 + " op terminal " + tuple.Item2 + " met totale weerstand van " + tuple.Item3);
            }
        }

这可行,我可以打印所有连接。

主要问题是: 如何在两个端子之间使用任意数量的电阻器来搜索它们之间的连接?

另一个问题是: 我什至在这样做对吗?

在此先感谢您,我们将不胜感激。

0 个答案:

没有答案