Трекерный список Token Ring C# - PullRequest
0 голосов
/ 12 апреля 2020

Я пытаюсь реализовать Token Ring в C#. Я думал о проблеме следующим образом: я добавляю числа p c в список (называемый PcSubList) и p c Ip в другом списке (IpSubList). Теперь, когда я ввожу источник P c 4, например, и назначение p c 2, программа работает нормально, но я ввожу источник p c 1, а затем я ввожу p c destination 5, он попадает в бесконечное время l oop.

Этот код пересекает список:

 public void traverseList()
        {
            int pcSourceNumber = Int32.Parse(source);
            int pcDestinationNumber = Int32.Parse(destination);
            int i = pcSourceNumber;
            int f = pcDestinationNumber - 1;
              while (!String.IsNullOrEmpty(message))
            {
                for(int j = pcSourceNumber; j < pc.PcSubList.Count; j++) { 
                    Console.WriteLine("PC number {0} ", pc.PcSubList[j]);
                    Console.WriteLine("Source IP: {0} ", pc.IpSubList[j]);
                    Console.WriteLine("Destination IP: {0} ", pc.IpSubList[pcDestinationNumber]);
                }

                if (destination.Equals(pc.PcSubList[i]))
                {
                    //Console.WriteLine("PC number {0} ", pc.PcSubList[i]);
                    Console.WriteLine("The message is : {0} ", message);
                }

                if (i == pc.PcSubList.Count - 1)
                {
                    i = 0; // if the pc index gets to the final element of the list, start reiterating
                }

                if (destination.Equals(pc.PcSubList[i]) && i != pcSourceNumber)
                {
                    while (!String.IsNullOrEmpty(message))
                    {
                        if (i != pcSourceNumber)
                        {
                            Console.WriteLine("PC number {0} ", pc.PcSubList[i]);
                            Console.WriteLine("Source IP: {0} ", pc.IpSubList[i]);
                            Console.WriteLine("Destination IP: {0} ", pc.IpSubList[pcDestinationNumber]);
                        }

                        if (i == pcSourceNumber)
                        {
                            Console.WriteLine("------------------------");
                            Console.WriteLine("REACHED SOURCE AGAIN. PC : {0} ", pc.PcSubList[i]);
                            Console.WriteLine("Source PC IP : -");
                            Console.WriteLine("Destination PC IP : -");
                            Console.WriteLine("Message : -");
                            message = null;
                        }
                        i++;
                    }
                    i++;

                }
            }


        }
    }

Не могли бы вы мне помочь? Я просто не могу понять, что я делаю неправильно, или, может быть, мне нужен другой подход?

Спасибо!

1 Ответ

0 голосов
/ 12 апреля 2020

Попробуйте этот код, хотя и не идеально, но он поможет вам начать с некоторого рабочего кода

class PC
{

    public List<String> PcSubList = new List<string>();
    public List<String> IpSubList = new List<string>();
}



static void Main(string[] args)
{
    PC pc = new PC();
    pc.IpSubList.Add("1");
    pc.IpSubList.Add("2");
    pc.IpSubList.Add("3");
    pc.IpSubList.Add("4");
    pc.IpSubList.Add("5");
    pc.IpSubList.Add("6");
    pc.IpSubList.Add("7");

    Boolean foundStart = false;
    Boolean foundEnd = false;
    Boolean foundReturn = false;
    int i = 0;
    int j = 0;
    int startIndex = 0;
    int endIndex = 0;
    while (!foundStart || !foundEnd || !foundReturn)
    {

        if (!foundStart) {
            Console.WriteLine("F1 " + pc.IpSubList[i] + " " + i);
            if (pc.IpSubList[i].Equals("3"))
            {
                foundStart = true;
                startIndex = i;
                Console.WriteLine();
            }
        }

        if (foundStart && !foundEnd) {
            Console.WriteLine("F2 " + pc.IpSubList[i] + " " + i);
            if (pc.IpSubList[i].Equals("6"))
            {
                foundEnd = true;
                endIndex = i + 1;
                j = endIndex;
                Console.WriteLine();
            }
        }

        if(foundStart && foundEnd && j >= startIndex) {
            j--;
            Console.WriteLine("F3 " + pc.IpSubList[j] + " " + j);
            if (j == startIndex) foundReturn = true;
        }

        if (!foundStart || !foundEnd)
            i++;
        if (i >= pc.IpSubList.Count) break;
    }
}

3 и 6 имеют фиксированный старт и конец, жестко закодированные внутри кода, вы можете изменить этот код и заставить его работать для вашей цели

F1 - это путь между zero и start F2 - это путь между start и end F3 - это путь между end и start

вы можете удалить Console.WriteLine для F1, если не хотите видеть эту часть

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