структура данных графа для сетей сопротивления в c # (я делаю это правильно?) - PullRequest
0 голосов
/ 16 октября 2018

Я пытаюсь получить сопротивление между точками на схеме сопротивления.обратите внимание, что это может быть любая схема (почти) каждой конфигурации резисторов.

это то, что я пытался сделать сам, но не знаю, как продвигаться дальше:

-объектное представлениеa resitor:

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);
            }
        }

это работает, и я могу распечатать все соединения.

основной вопрос: как мне найти соединение между двумя терминалами с любым номеромрезисторов между ними?

другой вопрос: правильно ли я все делаю?

заранее спасибо, любая помощь будет признательна.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...