0

I'm handling with Roman numbers in WinForms.

I need transform them to Arabic.

Example: XCIV is 94. I'm a beginner in c#. I use Doubly-linked list to handle the task. I've not studied anything higher in my education for now.

  1. So should i use doubly-linked list or it's not the best solution?
  2. How can I extract from a loop:

    for (int i = 0; i < Check.Length; i++)
    {
        l.Add(Check[i], source);
    }
    

the Listelement with entire list and save to var.

Here is the code:

Form1.cs

private void runBtn_Click(object sender, EventArgs e)
{
    string source = inputBox.Text;
    RomanNumbers rn = new RomanNumbers();

    if(rn.CheckRomanNumber(source))
    {
        OutputBox.Text = RomanNumbers.Calculate();
    }
    else
    {
        OutputBox.Text = "Wrong format!";
    }
}

RomanNumbers.cs

using System;


namespace RomanNumbersToArabic
{
    public class RomanNumbers
    {

        public static int ArabicValue { get; private set; }

        public static bool TransformResult { get; private set; }

        static RomanNumbers()
        {

        }


        public bool CheckRomanNumber(string source)
        {
            bool result = false;
            List l = new List();
            CheckExept[] Check = l.CreateList(source);

            if (Check == null)
            {
                return false;
            }

            for (int i = 0; i < Check.Length; i++)
            {
                l.Add(Check[i], source);
            }



            return result;
        }

        public static string Calculate()
        {
            string result = "";

            // code //

            return result;
        }

        public static bool ValidateExpression()
        {
            bool result = false;
            string[] exceptionsWithValuesArray = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "DM", "M" };

            // code //

            return result;
        }
    }
}

List.cs

using System;

namespace RomanNumbersToArabic
{
    class List
    {
        private ListElement head;
        private ListElement tail;

        public List()
        {

        }

        public int Count { get; private set; }


        public CheckExept[] CreateList(string source)
        {
            source = StringFormat(source);

            CheckExept[] Check = new CheckExept[source.Length];
            int checkCounter = 0;
            for (int i = 0; i < source.Length; i++)
            {
                Check[checkCounter] = new CheckExept();
                Check[checkCounter] = Validate(i, source);

                if (Check[checkCounter] == null)
                {
                    return null;
                }

                if (Check[checkCounter].IsException)
                {
                    i++;
                }
                checkCounter++;
            }
            Array.Resize(ref Check, checkCounter);

            return Check;
        }

        private CheckExept Validate(int index, string stringValue)
        {
            Number[] listOfNumbers = Number.CreateNumbersList();
            Exeption[] ListOfException = Exeption.CreateExeptionsList();
            CheckExept Check = new CheckExept();
            Check.IsLetter = false;
            Check.IsException = true;
            bool isLastElement = false;

            if (CheckWrongItems(listOfNumbers, stringValue))
            {
                return null;
            }

            if (index == stringValue.Length - 1)
            {
                isLastElement = true;
                Check.IsLetter = true;
                Check.IsException = false;
            }
            else
            {
                stringValue = stringValue.Substring(index, 2);
            }

            if(!isLastElement)
                for (int i = 0; i < ListOfException.Length; i++)
                {
                    if (stringValue == ListOfException[i].Letter)
                    {
                        Check.IsLetter = false;
                        Check = new CheckExept(stringValue, ListOfException[i].Value, Check.IsLetter, Check.IsException);
                        break;
                    }
                    else
                    {
                        Check.IsLetter = true;
                    }
                }

            if (Check.IsLetter)
            {
                Check.IsException = false;
                Check = GetLetter(listOfNumbers, Check, stringValue.Substring(index, 1));
            }

            return Check;
        }

        private CheckExept GetLetter(Number[] listOfNumbers, CheckExept Check, string stringValue)
        {

            for (int i = 0; i < listOfNumbers.Length; i++)
            {
                if (stringValue == listOfNumbers[i].Letter)
                {
                    Check.IsLetter = true;
                    Check.StringValue = stringValue; 
                    Check.IntValue = listOfNumbers[i].Value;
                    return Check;
                }
                else
                {
                    Check.IsLetter = false;
                }
            }

            return null;
        }

        private bool CheckWrongItems(Number[] listOfNumbers, string stringValue)
        {
            bool isWrong = true;

            for (int i = 0; i < stringValue.Length; i++)
            {
                isWrong = true;
                for (int j = 0; j < listOfNumbers.Length; j++)
                {
                    if (stringValue[i].ToString() == listOfNumbers[j].Letter)
                    {
                        isWrong = false;
                        break;
                    }
                }
                if (isWrong)
                {
                    return isWrong;
                }
            }

            return isWrong;
        }

        public void Add(CheckExept check, string source)
        {
            ListElement newElement = new ListElement();

            newElement = new ListElement(check.StringValue, check.IntValue, check.IsLetter, check.IsException);

            if (head == null)
            {
                head = newElement;
                tail = newElement;
                return;
            }

            tail.Next = newElement;
            newElement.Prev = tail;
            tail = newElement;
            Count++;
        }

        private string StringFormat(string value)
        {
            value = value.Trim().ToUpper();
            value = value.Replace(" ", "");

            return value;
        }
    }
}

0 Answers0