示例#1
0
 private void preAppendX()
 {
     if (textBoxCalcInput.Text.CompareTo("") == 0)
     {
         return;
     }
     if (MathProcessor.isOperand(textBoxCalcInput.Text.Last().ToString()))
     {
         textBoxCalcInput.Text += "x";
     }
 }
示例#2
0
        private void btnAns_Click(object sender, RoutedEventArgs e)
        {
            foreach (char c in textUserData.Text)
            {
                if (!MathProcessor.isOperand(c.ToString()))
                {
                    return;
                }
            }

            textBoxCalcInput.Text += textUserData.Text;
        }
示例#3
0
        /// <summary>
        /// Decomposes the input string into it's components for an infix expression
        /// </summary>
        /// <param name="expression">Mathematical expression in string format</param>
        /// <returns></returns>
        private Queue <string> decomposeString(string expression)
        {
            Queue <string> infix = new Queue <string>();

            string trimmed = textBoxCalcInput.Text.Trim();

            trimmed = trimmed.ToLower();
            char[] chars = trimmed.ToCharArray();

            bool negative = false;

            int i = 0;

            while (i < chars.Length)
            {
                if (Char.IsWhiteSpace(chars[i]))
                {
                    i++;
                    continue;
                }
                if (MathProcessor.isOperand(chars[i].ToString()))
                {
                    string number = "";
                    while (MathProcessor.isOperand(chars[i].ToString()))
                    {
                        number += chars[i].ToString();
                        i++;

                        if (i >= chars.Length)
                        {
                            break;
                        }
                    }
                    if (number.CompareTo(".") == 0)
                    {
                        throw new Exception(number + " is not a valid number");
                    }
                    number = ((negative) ? "-" : "") + number;
                    infix.Enqueue(number);
                    negative = false;
                }
                else
                {
                    switch (chars[i].ToString())
                    {
                    case "s":
                        infix.Enqueue("sin");
                        infix.Enqueue("(");
                        i += 4;
                        break;

                    case "c":
                        try
                        {
                            if (MathProcessor.isOperand(chars[i + 1].ToString()))
                            {
                                infix.Enqueue("C");
                                i++;
                            }
                            else
                            {
                                infix.Enqueue("cos");
                                infix.Enqueue("(");
                                i += 4;
                            }
                            break;
                        }
                        catch (IndexOutOfRangeException e)
                        {
                            throw new Exception("Syntax Error" + e.Message);
                        }

                    case "t":
                        infix.Enqueue("tan");
                        infix.Enqueue("(");
                        i += 4;
                        break;

                    case "a":
                        try
                        {
                            switch (chars[i + 1].ToString())
                            {
                            case "s":
                                infix.Enqueue("asin");
                                infix.Enqueue("(");
                                i += 5;
                                break;

                            case "c":
                                infix.Enqueue("acos");
                                infix.Enqueue("(");
                                i += 5;
                                break;

                            case "t":
                                infix.Enqueue("atan");
                                infix.Enqueue("(");
                                i += 5;
                                break;

                            default:
                                throw new Exception("Unknown Operation");
                            }
                            break;
                        }
                        catch (IndexOutOfRangeException e)
                        {
                            throw new Exception("Syntax Error" + e.Message);
                        }

                    case "p":
                        infix.Enqueue("P");
                        i++;
                        break;

                    case "e":
                        infix.Enqueue("exp");
                        infix.Enqueue("(");
                        i += 4;
                        break;

                    case "l":
                        try
                        {
                            if (chars[i + 1].ToString().CompareTo("o") == 0)
                            {
                                infix.Enqueue("log");
                                i += 4;
                            }
                            else
                            {
                                infix.Enqueue("ln");
                                i += 3;
                            }
                            infix.Enqueue("(");
                            break;
                        }
                        catch (IndexOutOfRangeException e)
                        {
                            throw new Exception("Syntax Error" + e.Message);
                        }

                    case MathProcessor.SUPERSCRIPTN:
                        infix.Enqueue("rt");
                        infix.Enqueue("(");
                        i += 3;
                        break;

                    case MathProcessor.SQUAREROOT:
                    case MathProcessor.CUBEROOT:
                    case "^":
                        infix.Enqueue(chars[i].ToString());
                        infix.Enqueue("(");
                        i += 2;
                        break;

                    case MathProcessor.SQUARED:
                        infix.Enqueue("^");
                        infix.Enqueue("(");
                        infix.Enqueue("2");
                        infix.Enqueue(")");
                        i++;
                        break;

                    case MathProcessor.CUBED:
                        infix.Enqueue("^");
                        infix.Enqueue("(");
                        infix.Enqueue("3");
                        infix.Enqueue(")");
                        i++;
                        break;

                    case "x":
                    case "/":
                    case "(":
                    case ")":
                        infix.Enqueue(chars[i].ToString());
                        i++;
                        break;

                    case "-":
                        if (infix.Count == 0)
                        {
                            negative = true;
                            i++;
                            break;
                        }

                        try
                        {
                            Double.Parse(infix.Last());
                            infix.Enqueue(chars[i].ToString());
                            i++;
                            negative = false;
                        }
                        catch (FormatException)
                        {
                            i++;
                            negative = true;
                        }
                        break;

                    case "+":
                        if (infix.Count == 0)
                        {
                            i++;
                            break;
                        }

                        try
                        {
                            Double.Parse(infix.Last());
                            infix.Enqueue(chars[i].ToString());
                            i++;
                        }
                        catch (FormatException)
                        {
                            i++;
                        }
                        break;

                    case MathProcessor.PI:
                        infix.Enqueue(Math.PI.ToString());
                        i++;
                        break;

                    default: throw new Exception("Currently Unsupported");
                    }
                }
            }

            return(infix);
        }