Beispiel #1
0
        /// <summary>
        /// Retorna uma string com a expressão separando cada elemento por '.'
        /// </summary>
        /// <param name="msg">Expressão informada</param>
        /// <param name="pil">Pilha para as operações serem inseridas</param>
        /// <returns></returns>
        public static string BuildExpression(string msg)
        {
            if (Regex.IsMatch(msg, @"^[a-zA-Z]+$"))
            {
                return("");                                    //verifica se há letras
            }
            Pilha  pil    = new Pilha();
            string result = "";
            int    last   = 0;

            for (int i = 0; i < msg.Length; i++)
            {
                string ch = msg.Substring(i, 1);
                try
                {
                    int aux = Convert.ToInt32(ch);
                    result += aux;
                    for (int i1 = i + 1; i1 < msg.Length; i1++) // Usado para quando o número for maior q 9
                    {
                        string nextchar = msg.Substring(i1, 1);
                        try
                        {
                            aux     = Convert.ToInt32(nextchar);
                            result += aux;
                            i++;
                        }
                        catch
                        {
                            if (nextchar == ",")
                            {
                                result += nextchar;
                                i++;
                            }
                            else
                            {
                                result += ".";
                                break;
                            }
                        }
                    }
                    if (i - 1 != -1)
                    {
                        if (result.Substring(result.Length - 1, 1) != ".")
                        {
                            result += ".";
                        }
                    }
                }
                catch
                {
                    if (ch == "+" || ch == "-" || ch == "/" || ch == "*" || ch == "^" || ch == ")" || ch == "(" || ch == "√")
                    {
                        if (CalcWeight(ch) > last || CalcWeight(ch) == 0)
                        {
                            pil.push(ch);
                        }
                        else if (ch == ")") //Desempilhamento das operações para quando uma expressão em parênteses for fechada
                        {
                            string aux = "";
                            while ((aux = pil.pop()) != "(" && aux != null)
                            {
                                result += aux + ".";
                            }
                            string oldval = pil.pop();
                            if (i + 1 < msg.Length)
                            {
                                if (CalcWeight(oldval) >= CalcWeight(msg.Substring(i + 1, 1)))
                                {
                                    result += oldval + ".";
                                }
                                else
                                {
                                    pil.push(oldval);
                                }
                            }
                            else
                            {
                                pil.push(oldval);
                            }
                        }
                        else //Desempilha enquanto as operações forem menores da que foi informada
                        {
                            string getval = "";
                            while (CalcWeight(getval = pil.pop()) >= CalcWeight(ch))
                            {
                                result += getval + ".";
                            }
                            pil.push(ch);
                        }
                        last = CalcWeight(ch);
                    }
                }
            }
            while (pil.fundo != pil.topo)
            {
                result += pil.pop() + ".";
            }
            result = result.Replace(")", null);
            result = result.Replace("(", null);
            string resultaux = "";

            for (int i = 0; i < result.Length; i++)
            {
                if (i + 1 < result.Length)
                {
                    if (result.Substring(i, 1) == result.Substring(i + 1, 1))
                    {
                        int count = 1;
                        while (i + count < result.Length && result.Substring(i + count, 1) == ".")
                        {
                            count++;
                        }
                        if (i + count < result.Length)
                        {
                            resultaux += result.Substring(i + count, 1);
                        }
                    }
                    else
                    {
                        resultaux += result.Substring(i, 1);
                    }
                }
            }
            if (!String.IsNullOrEmpty(resultaux))
            {
                return(resultaux);
            }
            else
            {
                return(result);
            }
        }
Beispiel #2
0
        private static double CalcExpression(string aux)
        {
            if (aux == "")
            {
                return(0);
            }
            double sum = 0, val1 = 0, val2 = 0;

            string[] spt = aux.Split('.');
            if (spt.Length == 1)
            {
                return(int.Parse(aux));
            }
            Pilha old = new Pilha();

            foreach (string str in spt)
            {
                try
                {
                    double convertaux;
                    convertaux = Convert.ToDouble(str);
                    if (val1 != 0 && val2 != 0)
                    {
                        old.push(val1.ToString());
                    }
                    val1 = val2;
                    val2 = convertaux;
                }
                catch
                {
                    if (str == "+")
                    {
                        sum = val2 + val1;
                    }
                    else if (str == "-")
                    {
                        sum = val1 - val2;
                    }
                    else if (str == "/")
                    {
                        sum = val1 / val2;
                    }
                    else if (str == "*")
                    {
                        sum = val1 * val2;
                    }
                    else if (str == "^")
                    {
                        sum = Math.Pow(val1, val2);
                    }
                    else if (str == "√")
                    {
                        sum = Math.Sqrt(val2);
                    }
                    else
                    {
                        return(val2);
                    }
                    //checar casas decimais
                    val2 = sum;
                    val1 = Convert.ToDouble(old.pop());
                }
            }
            return(sum);
        }