예제 #1
0
        public virtual int ToDecimal()
        {
            if (!IsValid())
            {
                throw new Exception("Invalid number: " + this);
            }

            int result = 0;

            for (int i = 0; i < symbols.Length; i++)
            {
                RomanSymbol currentSymbol = symbols[i];
                if (i < symbols.Length - 1)
                {
                    RomanSymbol nextSymbol = symbols[i + 1];
                    if (nextSymbol.Value() > currentSymbol.Value())
                    {
                        result -= currentSymbol.Value();
                    }
                    else
                    {
                        result += currentSymbol.Value();
                    }
                }
                else
                {
                    result += currentSymbol.Value();
                }
            }
            return(result);
        }
예제 #2
0
        private bool IsValidSubtraction(int i, RomanSymbol currentSymbol)
        {
            if (i < symbols.Length - 1)
            {
                //V,D,L can't subtract nothing
                if (new[] { "V", "D", "L" }.Contains(currentSymbol.Literal))
                {
                    for (int j = i + 1; j < symbols.Length; j++)
                    {
                        if (symbols[j].Value() > currentSymbol.Value())
                        {
                            return(false);
                        }
                    }
                }
                //Nothing can subtract and add right after : IXI, IXII, IXIII is invalid
                if (i < symbols.Length - 2 && symbols[i + 1].Value() > currentSymbol.Value() && symbols[i + 2].Value() >= currentSymbol.Value())
                {
                    return(false);
                }

                //Can't add to subtract. Ex: IIX,IIIX,XXC,XXXC ...
                if (symbols[i + 1].Equals(currentSymbol))
                {
                    for (int j = i + 1; j < symbols.Length; j++)
                    {
                        if (symbols[j].Value() > currentSymbol.Value())
                        {
                            return(false);
                        }
                    }
                }
            }

            //I can only subtract X
            if (currentSymbol.Literal.Equals("I"))
            {
                if (i < symbols.Length - 1 && new[] { "D", "C", "M", "L" }.Contains(symbols[i + 1].Literal))
                {
                    return(false);
                }
            }
            //X can only subtract C
            if (currentSymbol.Literal.Equals("X"))
            {
                if (i < symbols.Length - 1 && new[] { "D", "M" }.Contains(symbols[i + 1].Literal))
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #3
0
        public bool IsValid()
        {
            if (!HasValidSums())
            {
                return(false);
            }
            if (!HasValidConsecutiveSymbols())
            {
                return(false);
            }

            for (int i = 0; i < symbols.Length; i++)
            {
                RomanSymbol currentSymbol = symbols[i];

                if (!IsValidSymbol(currentSymbol) || !IsValidSubtraction(i, currentSymbol))
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #4
0
 private bool IsValidSymbol(RomanSymbol currentSymbol)
 {
     return(ValidSymbols.Contains(currentSymbol.Literal));
 }