求解如何利用C#实现计算器的乘方、开方功能?

本人技术渣,小问题钻研了半天没结果,求大佬指点迷津!
附上相关代码:

public partial class MainWindow : Window
    {
        double sum = 0;
        bool blnClear = false;
        string strOper = "+";

        private void Check_button(object sender)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;
                    case "-":
                        sum -= dbSecond;
                        break;
                    case "*":
                        sum *= dbSecond;
                        break;
                    case "/":
                        sum /= dbSecond;
                        break;
                    case "SQ":
                        sum += dbSecond;
                        sum = sum * sum;
                        break;
                    case "sqt":
                        sum += dbSecond;
                        sum = Math.Sqrt(sum);
                        break;
                }
            if (sender == Button_add)
                strOper = "+";
            if (sender == Button_equ)
                strOper = "=";
            if (sender == Button_sub)
                strOper = "-";
            if (sender == Button_mul)
                strOper = "*";
            if (sender == Button_div)
                strOper = "/";
            if (sender == Button_square)
                strOper = "SQ";
            if (sender == Button_sqt)
                strOper = "sqt";
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_equ_Click(object sender, RoutedEventArgs e)
        {
            Check_button(sender);
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }

        private void Button_C_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "0";
            sum = 0;
            blnClear = false;
            strOper = "+";
        }

        private void Button_sqt_Click(object sender, RoutedEventArgs e)
        {
            Check_button(sender);
            //if (textBox1.Text == Convert.ToString(sum))
            //    textBox1.Text = Convert.ToString(sum);
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }

        private void Button_SQ_Click(object sender, RoutedEventArgs e)
        {
            Check_button(sender);
            if (textBox1.Text == Convert.ToString(sum))
                textBox1.Text = Convert.ToString(sum);
            blnClear = true;

        }
    }
}

加减乘除都已经成功实现,就是乘方、开方这两个单元操作符一直没成功,请问上述代码问题出在哪里?

c++
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐