Exemplo n.º 1
0
        static string CalcSent(string opState)
        {
            string postfix;

            try
            {
                if ("错误" == (postfix = InfixToPostfix(opState)))
                {
                    return(postfix);
                }
            }
            catch (Exception)
            {
                return("错误");
            }
            Stack <double> optNum = new Stack <double>();

            for (int i = 0; i < postfix.Length; ++i)
            {
                if (postfix[i] == ' ')
                {
                    continue;
                }
                else if (1 == isOpt(postfix[i]))
                {
                    try
                    {
                        switch (postfix[i])
                        {
                        case 's':
                            optNum.Push(Math.Sin(optNum.Pop()));
                            break;

                        case 'S':
                            optNum.Push(Math.Asin(optNum.Pop()));
                            break;

                        case 'c':
                            optNum.Push(Math.Cos(optNum.Pop()));
                            break;

                        case 'C':
                            optNum.Push(Math.Acos(optNum.Pop()));
                            break;

                        case 't':
                            optNum.Push(Math.Tan(optNum.Pop()));
                            break;

                        case 'T':
                            optNum.Push(Math.Atan(optNum.Pop()));
                            break;

                        case 'l':
                            optNum.Push(Math.Log10(optNum.Pop()));
                            break;

                        case 'L':
                            optNum.Push(Math.Log(optNum.Pop()));
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        return("错误");
                    }
                }
                else if (2 == isOpt(postfix[i]) && (i + 1 == postfix.Length || postfix[i + 1] == ' '))
                {
                    double opn1, opn2;
                    try
                    {
                        opn2 = optNum.Pop();
                        opn1 = optNum.Pop();
                    }
                    catch (Exception)
                    {
                        return("错误");
                    }
                    if (postfix[i] == 'q')
                    {
                        optNum.Push(Math.Pow(opn2, 1D / opn1));
                    }
                    else if (postfix[i] == '^')
                    {
                        optNum.Push(Math.Pow(opn1, opn2));
                    }
                    else
                    {
                        optNum.Push(CalcStandard(opn1, opn2, postfix[i]));
                    }
                }
                else
                {
                    StringBuilder singleNum = new StringBuilder();
                    while (i < postfix.Length && ('0' <= postfix[i] && postfix[i] <= '9' || postfix[i] == '.' || postfix[i] == '-' && '0' <= postfix[i + 1] && postfix[i + 1] <= '9'))
                    {
                        singleNum.Append(postfix[i++]);
                    }
                    try
                    {
                        optNum.Push(Convert.ToDouble(singleNum.ToString()));
                    }
                    catch (Exception)
                    {
                        return("错误");
                    }
                    --i;
                }
            }
            if (optNum.Count != 1)
            {
                return("错误");
            }
            else
            {
                return(Convert.ToString(optNum.Pop()));
            }
        }
Exemplo n.º 2
0
        private void function_click(object sender, EventArgs e)
        {
            Button btn = sender as Button;

            first_number = double.Parse(Input.Text);
            label.Text   = btn.Text + "(" + first_number + ")";
            if (!Degree.Checked)
            {
                switch (btn.Text)
                {
                case "log":
                    res3 = Math.Log10(first_number);
                    break;

                case "ln":
                    res3 = Math.Log(first_number);
                    break;

                case "sin":
                    res3 = Math.Sin(first_number * Math.PI / 180);
                    break;

                case "cos":
                    res3 = Math.Cos(first_number * Math.PI / 180);
                    break;

                case "tan":
                    res3 = Math.Tan(first_number * Math.PI / 180);
                    break;

                case "ctg":
                    res3 = Math.Cos(first_number * Math.PI / 180) / Math.Sin(first_number * Math.PI / 180);
                    break;

                case "asin":
                    res3 = Math.Asin(first_number);
                    break;

                case "acos":
                    res3 = Math.Acos(first_number);
                    break;

                case "atan":
                    res3 = Math.Atan(first_number);
                    break;

                case "actg":
                    res3 = Math.Atan(1 / first_number);
                    break;
                }
                Input.Text = res3.ToString();
                i          = 0;
            }
            else
            {
                switch (btn.Text)
                {
                case "log":
                    res3 = Math.Log10(first_number);
                    break;

                case "ln":
                    res3 = Math.Log(first_number);
                    break;

                case "sin":
                    res3 = Math.Sin(first_number);
                    break;

                case "cos":
                    res3 = Math.Cos(first_number);
                    break;

                case "tan":
                    res3 = Math.Tan(first_number);
                    break;

                case "ctg":
                    res3 = Math.Cos(first_number) / Math.Sin(first_number);
                    break;

                case "asin":
                    res3 = Math.Asin(first_number);
                    break;

                case "acos":
                    res3 = Math.Acos(first_number);
                    break;

                case "atan":
                    res3 = Math.Atan(first_number);
                    break;

                case "actg":
                    res3 = Math.Atan(1 / first_number);
                    break;
                }
                Input.Text = res3.ToString();
                i          = 0;
            }
        }