示例#1
0
        public Complex Calculate(string equation, string variablestring)
        {
            equation = DeCleanUp(equation);

            if (variablestring.Length > 0)
            {
                foreach (
                    var split in
                        variablestring.Split(';')
                            .Select(varstring => varstring.Split('='))
                            .Where(split => split.Length != 1 || split[0].Trim().Length != 0))
                {
                    if (split.Length != 2)
                        throw new ParseException(variablestring, 0,
                            "La liste doit être séparée par des points-virgules, ex. 'x=10; y=12'");
                    var varname = split[0].Trim();
                    var vareq = split[1].Trim();
                    var v = new Term();
                    v.Parse(vareq);
                    m_term.SetVar(varname, v.Value);
                }
            }
            m_term.Parse(equation);
            return m_term.Value;
        }
示例#2
0
        public Function(string src, int start, EquationElement root)
        {
            m_startIndex = start;
            m_func = Term.ExtractName(src, start).ToLower();
            start += m_func.Length;
            // sauter tous les espaces, mais le prochain caractère doit être une parenthèse ouvrante
            if (start == src.Length)
                throw new ParseException(src, m_startIndex, "La fonction doit commencer par '('");
            while (src[start] == ' ')
                start++;
            if (src[start] != '(')
                throw new ParseException(src, m_startIndex, "La fonction doit commencer par '('");
            var termstart = start;
            var end = Term.FindMatchingEnd('(', src, termstart);
            while (end < termstart)
            {
                src += ')';

                end = Term.FindMatchingEnd('(', src, termstart);
            }
            /*if (end < termstart)
            {
                throw new ParseException(src, m_startIndex, "Pas de parenthèse fermante correspondante trouvée");
            }*/

            m_endindex = end;
            var allterms = src.Substring(termstart + 1, end - termstart - 1);
            //string[] terms = allterms.Split(',');
            var terms = GetTerms(allterms);
            m_terms = new List<Term>();
            foreach (var term in terms)
            {
                var newterm = new Term();
                newterm.Parse(term, root);
                newterm.Parent = this;
                m_terms.Add(newterm);
            }
        }
示例#3
0
 public void SetVar(string varname, string value)
 {
     var term = new Term();
     term.Parse(value);
     SetVar(varname, term.Value);
 }
示例#4
0
        private void CombineTerms(string[] operators)
        {
            foreach (var element in Stack)
            {
                (element as Term)?.CombineTerms(operators);
            }
            if (NeedSubTerms(operators) == false)
                return;

            var startIndex = 0;
            while (startIndex < Stack.Count)
            {
                startIndex = FindOperator(startIndex, operators);
                if (startIndex < 0)
                    return;

                var newterm = new Term {Parent = this};
                startIndex--;
                var startpoint = startIndex;

                while (startIndex < Stack.Count)
                {
                    var item = Stack[startIndex];
                    if (item is EquationValue)
                    {
                        newterm.Add(item);
                        startIndex++;
                    }
                    if (item is Operator)
                    {
                        var op = item as Operator;
                        if (op == null || operators.Contains(op.Value) == false)
                        {
                            Stack.RemoveRange(startpoint, newterm.Stack.Count);
                            Stack.Insert(startpoint, newterm);
                            break;
                        }
                        newterm.Add(item);
                        startIndex++;
                    }

                    if (startIndex >= Stack.Count)
                    {
                        Stack.RemoveRange(startpoint, newterm.Stack.Count);
                        Stack.Insert(startpoint, newterm);
                        return;
                    }
                }
            }
        }
示例#5
0
        private void CombineTopDown()
        {
            // traitement spécial de l'opérateur de puissance. Si un opérateur est élevé à la puissance d'une puissance, la puissance doit être calculée en premier.
            // ex. 2^2^3 = 2^(2^3) = 256. Ceci est réalisé en combinant de droite à gauche (ou de haut en bas)
            var operatorCh = "^";
            foreach (var element in Stack)
            {
                (element as Term)?.CombineTopDown();
            }
            var index = Stack.Count - 1;
            while (index > 0)
            {
                var op = Stack[index] as Operator;
                index--;
                if (op == null || op.Value != operatorCh)
                    continue;

                var left = Stack[index];
                var right = Stack[index + 2];

                var newterm = new Term {Parent = this};
                newterm.Add(left);
                newterm.Add(op);
                newterm.Add(right);

                if (((EquationValue) left).Signed)
                {
                    ((EquationValue) left).Signed = false;
                    newterm.Signed = true;
                }

                Stack.RemoveRange(index, newterm.Stack.Count);
                Stack.Insert(index, newterm);
            }
        }