Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
5.7 kB
1
Indexable
Never
using Flee.PublicTypes;

string ChangeRomanNumbersToArabic(string s)
{
    var ans = "";
    var i = 0;
    while (i < s.Length)
    {
        int j = 0;
        if (Char.IsLetter(s[i]))
        {
            j = i + 1;
            while (Char.IsLetter(s[j]))
                j++;
            int num;
            if (j > 0)
            {
                num = ToArabic(s.Substring(i, j - i));
                ans += num.ToString();
                i = j - 1;
            }
        }
        else
        {
            ans += s[i];
        }

        i++;
    }

    return ans;
}

int ToArabic(string romanNumber)
{
    Dictionary<char, int> RomanMap = new Dictionary<char, int>()
    {
        { 'I', 1 },
        { 'V', 5 },
        { 'X', 10 },
        { 'L', 50 },
        { 'C', 100 },
        { 'D', 500 },
        { 'M', 1000 }
    };
    int number = 0;
    for (int i = 0; i < romanNumber.Length; i++)
    {
        if (i + 1 < romanNumber.Length && RomanMap[romanNumber[i]] < RomanMap[romanNumber[i + 1]])
        {
            number -= RomanMap[romanNumber[i]];
        }
        else
        {
            number += RomanMap[romanNumber[i]];
        }
    }

    return number;
}

string SolveMath(string taskResponse)
{
    var str = SolveFunctionInMath(taskResponse);
    var array = new List<string>();
    var start = 0;
    var oneExp = "";
    while (start < str.Length)
    {
        if (str[start] != '\"')
        {
            while (start < str.Length && str[start] != ' ')
            {
                oneExp += str[start];
                start++;
            }

            array.Add(oneExp);
            oneExp = "";
            start++;
        }
        else if (str[start] == '"')
        {
            start++;
            while (start < str.Length && str[start] != '"')
            {
                oneExp += str[start];
                start++;
            }

            array.Add(oneExp);
            oneExp = "";
            start += 2;
        }
    }

    var ans = "";
    foreach (var expression in array)
    {
        ExpressionContext context = new ExpressionContext();
        context.Imports.AddType(typeof(Math));
        var newString = ChangeRomanNumbersToArabic(expression);
        newString = AddLInMathString(newString);
        IDynamicExpression eDynamic = context.CompileDynamic(newString);
        long result = Convert.ToInt64(eDynamic.Evaluate());
        ans += "\""+ result+"\"" + " ";
    }

    return ans.Trim();
}

string AddLInMathString(string s)
{
    var newString = s[0].ToString();
    for (int i = 1; i < s.Length; i++)
    {
        if (Char.IsDigit(s[i - 1]) && !Char.IsDigit(s[i]))
            newString += "L" + s[i];
        else
            newString += s[i];
    }

    return newString;
}

string SolveFunctionInMath(string s)
{
    if (s.Contains("right") || s.Contains("left") || s.Contains("max") || s.Contains("min") || s.Contains("sum") ||
        s.Contains("mul"))
    {
        var start = 0;
        var functions = new string[]
        {
            "right", "left", "mul", "max", "min", "right", "sum", "right", "left", "mul", "max", "min", "right", "sum",
            "right",
            "left", "mul", "max", "min", "right", "sum"
        };
        foreach (var func in functions)
        {
            start = 0;
            while (s.IndexOf(func, start) != -1)
            {
                var ind = s.IndexOf(func, start);
                start = ind + func.Length + 1;
                var a = "";
                var b = "";

                while (Char.IsDigit(s[start]))
                {
                    a += s[start];
                    start++;
                }

                if (s[start] == ')' && s[start + 1] == '(' && Char.IsDigit(s[start + 2]))
                {
                    start += 2;
                    while (Char.IsDigit(s[start]))
                    {
                        b += s[start];
                        start++;
                    }

                    if (func == "right")
                        s = ReplaceFirst(s, "right(" + a + ")(" + b + ")", b);
                    else if (func == "left")
                        s = ReplaceFirst(s, "left(" + a + ")(" + b + ")", a);
                    else if (func == "mul")
                        s = ReplaceFirst(s, "mul(" + a + ")(" + b + ")", (int.Parse(a) * int.Parse(b)).ToString());
                    else if (func == "max")
                        s = ReplaceFirst(s, "max(" + a + ")(" + b + ")",
                            Math.Max(int.Parse(a), int.Parse(b)).ToString());
                    else if (func == "min")
                        s = ReplaceFirst(s, "min(" + a + ")(" + b + ")",
                            Math.Min(int.Parse(a), int.Parse(b)).ToString());
                    else if (func == "sum")
                        s = ReplaceFirst(s, "sum(" + a + ")(" + b + ")", (int.Parse(a) + int.Parse(b)).ToString());
                    start = 0;
                }
                else
                {
                    start = start - func.Length + 1;
                }
            }
        }
    }

    return s;
}

string ReplaceFirst(string text, string search, string replace)
{
    int pos = text.IndexOf(search);
    if (pos < 0)
    {
        return text;
    }

    return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}

var s = Console.ReadLine();
Console.WriteLine();
Console.WriteLine(SolveMath(s));
Leave a Comment