当前位置: 首页 > article >正文

C#桌面应用制作计算器进阶版02

基于C#桌面应用制作计算器进阶版01做出了少量改动,其主要改动为label1显示所有输入的字符和运算符;且当数字为正数数时,点击“+/-”按键数字转化为负数并为其加上括号,再次点击数字转化为正数并去掉其括号;点击“Del”按键时,若label1中等式已经含有运算符且最后一个运算符后已经输入数字,则每次点击删除最后一个运算符后所有数字,若不含有运算符或末尾字符为运算符,则每次点击删除末尾数字。

修改后运行效果

修改后全篇代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp计算器
{
    public partial class Form1 : Form
    {
        string symbol, num1, num2,Num1,Num2;
        double rt;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = null;
            label2.Text = null;
            num1=null;
            num2=null;
            symbol = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            char lastchar = label1.Text[label1.Text.Length - 1];
            string lastChar=lastchar.ToString();
            if (label1.Text.Length == 0)
            {
                label1.Text = null;
            }else if (lastChar == "+" || lastChar == "-" || lastChar == "×" || lastChar == "÷")
            {
                string del = label1.Text;
                label1.Text = del.Substring(0, del.Length - 1);
                symbol = null;
            }
            else
            {
                string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                string result_2 = result_1.Replace(")", "");
                int k1 = label1.Text.IndexOf("(");
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                int min=sy[2];
                if (label2.Text.Length > 0&&max!=-1)
                    {
                        num2 = result_2.Substring(max+1);
                    if (k1 == -1)
                    {
                        char lastsymbol = result_2[max];
                        string last = lastsymbol.ToString();
                        if (last == "+")
                        {
                            rt = double.Parse(label2.Text) - double.Parse(num2);
                            label2.Text = rt.ToString();
                            symbol = last;
                        }
                        else if (last == "-")
                        {
                            rt = double.Parse(label2.Text) + double.Parse(num2);
                            label2.Text = rt.ToString();
                            symbol = last;
                        }
                        else if (last == "×")
                        {
                            rt = double.Parse(label2.Text) / double.Parse(num2);
                            label2.Text = rt.ToString();
                            symbol = last;
                        }
                        else if (last == "÷")
                        {
                            rt = double.Parse(label2.Text) * double.Parse(num2);
                            label2.Text = rt.ToString();
                            symbol = last;
                        }
                    }
                    else
                    {
                        string t=label1.Text.Substring(k1 - 1,1);
                        if (t == "-")
                        {
                            rt = double.Parse(label2.Text) - double.Parse(num2);
                            label2.Text = rt.ToString();
                            symbol = t;
                        }
                        else
                        {
                            char lastsymbol = result_2[min];
                            string last = lastsymbol.ToString();
                            if (last == "+")
                            {
                                rt = double.Parse(label2.Text) + double.Parse(num2);
                                label2.Text = rt.ToString();
                                symbol = last;
                            }
                            else if (last == "×")
                            {
                                rt = double.Parse(label2.Text) / (-double.Parse(num2));
                                label2.Text = rt.ToString();
                                symbol = last;
                            }
                            else if (last == "÷")
                            {
                                rt = double.Parse(label2.Text) * (-double.Parse(num2));
                                label2.Text = rt.ToString();
                                symbol = last;
                            }
                        }
                    }
                        string del = label1.Text;
                        int n = num2.Length;
                        if (k1 == -1)
                        {
                            label1.Text = del.Substring(0, del.Length - n);
                        }
                        else
                        {
                            label1.Text = del.Substring(0, del.Length - n-3);
                        }
                    }
                    else
                    {
                        if (label1.Text.Length == 1)
                        {
                            symbol=null;
                            num1=null;
                            num2=null;
                            label2.Text = null;
                            string del = label1.Text;
                            label1.Text = del.Substring(0, del.Length - 1);
                    }
                        else
                        {
                        string del = label1.Text;
                        label1.Text = del.Substring(0, del.Length - 1);
                        result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                        result_2 = result_1.Replace(")", "");
                        label2.Text = result_2;
                        }
                    }
                    if (label1.Text.Length < 30)
                    {
                        if (label1.Text.Length > 16)
                        {
                            label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                        }
                        else
                        {
                            label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                        }
                    }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length == 0)
            {
                MessageBox.Show("error");
            }
            else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
            {
                MessageBox.Show("error");
            }
            else
            {
                double reserve;
                if (symbol == null)
                {
                    string result_1=label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                    string result_2=result_1.Replace(")","");
                    reserve = double.Parse( result_2);
                    reserve = -reserve;
                    if (reserve > 0)
                    {
                        label1.Text = reserve.ToString();
                    }
                    else
                    {
                        label1.Text="("+reserve+")";
                    }
                    label2.Text = reserve.ToString();
                }
                else
                {
                    int r1=label1.Text.IndexOf("(");
                    int r2=label1.Text.IndexOf(")");
                    int s1 = label1.Text.LastIndexOf("+");
                    int s2 = label1.Text.LastIndexOf("-");
                    int s3 = label1.Text.LastIndexOf("×");
                    int s4 = label1.Text.LastIndexOf("÷");
                    int[] sy = { s1, s2, s3, s4 };
                    Array.Sort(sy);
                    int max = sy[3];
                    if (r1 > 0)
                    {
                        string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                        string result_2 = result_1.Replace(")", "");
                        string max1 = result_2.Substring(r1);
                        reserve = double.Parse(max1);
                        reserve = -reserve;
                        label1.Text = result_2.Substring(0, max-1) + reserve;
                    }
                    else
                    {
                        string max1 = label1.Text.Substring(max + 1);
                        reserve = double.Parse(max1);
                        reserve = -reserve;
                        label1.Text = label1.Text.Substring(0, max + 1) + "(" + reserve + ")";
                    }
                }
                if (num1 != null && symbol != null)
                {
                    num2 = reserve.ToString();
                    if (symbol == "+")
                    {
                        rt = double.Parse(num1) + double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                    else if (symbol == "-")
                    {
                        rt = double.Parse(num1) - double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                    else if (symbol == "×")
                    {
                        rt = double.Parse(num1) * double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                    else if (symbol == "÷")
                    {
                        rt = double.Parse(num1) / double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string divide = "÷";
            if (symbol == null && label1.Text.Length<1&&num1==null)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                string result_2 = result_1.Replace(")", "");
                if (symbol == null)
                {
                    if(num1 == null)
                    {
                        num1 = result_2;
                    }
                    symbol = divide;
                    label1.Text = label1.Text+symbol;
                    label2.Text = result_2;
                }
                else
                {
                    int last = label1.Text.Length - 1;
                    string Last=label1.Text.Substring(last);
                    if (Last == "+" || Last == "-" || Last == "×" || Last == "÷") 
                    {
                        symbol=divide;
                        string del = label1.Text;
                        label1.Text = del.Substring(0, del.Length - 1)+symbol;
                    }
                    else
                    {
                        int s1 = result_2.LastIndexOf("+");
                        int s2 = result_2.LastIndexOf("-");
                        int s3 = result_2.LastIndexOf("×");
                        int s4 = result_2.LastIndexOf("÷");
                        int[] sy = { s1, s2, s3, s4 };
                        Array.Sort(sy);
                        int max = sy[3];
                        num2 = result_2.Substring(max + 1);
                        num1 = rt.ToString();
                        symbol = divide;
                        label1.Text = label1.Text + symbol;
                        label2.Text = rt.ToString();
                        num2 = null;
                    }
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string seven = "7";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + seven;//将数字投射到屏幕上
                result_2 = result_2 + seven;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string eight = "8";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + eight;//将数字投射到屏幕上
                result_2 = result_2 + eight;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string nine = "9";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + nine;//将数字投射到屏幕上
                result_2 = result_2 + nine;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string multiply = "×";
            if (symbol == null && label1.Text.Length < 1 && num1 == null)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                string result_2 = result_1.Replace(")", "");
                if (symbol == null)
                {
                    if (num1 == null)
                    {
                        num1 = result_2;
                    }
                    symbol = multiply;
                    label1.Text = label1.Text + symbol;
                    label2.Text = result_2;
                }
                else
                {
                    int last = label1.Text.Length - 1;
                    string Last = label1.Text.Substring(last);
                    if (Last == "+" || Last == "-" || Last == "×" || Last == "÷") 
                    {
                        symbol = multiply;
                        string del = label1.Text;
                        label1.Text = del.Substring(0, del.Length - 1) + symbol;
                    }
                    else
                    {
                        int s1 = result_2.LastIndexOf("+");
                        int s2 = result_2.LastIndexOf("-");
                        int s3 = result_2.LastIndexOf("×");
                        int s4 = result_2.LastIndexOf("÷");
                        int[] sy = { s1, s2, s3, s4 };
                        Array.Sort(sy);
                        int max = sy[3];
                        num2 = result_2.Substring(max + 1);
                        num1 = rt.ToString();
                        symbol = multiply;
                        label1.Text = label1.Text + symbol;
                        label2.Text = rt.ToString();
                        num2 = null;
                    }
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string four = "4";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + four;//将数字投射到屏幕上
                result_2 = result_2 + four;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            string five = "5";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + five;//将数字投射到屏幕上
                result_2 = result_2 + five;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string six = "6";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + six;//将数字投射到屏幕上
                result_2 = result_2 + six;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string subtract = "-";
            if (symbol == null && label1.Text.Length < 1 && num1 == null)
            {
                num1 = "0";
                symbol = subtract;
                label1.Text = label1.Text + symbol;
            }
            else
            {
                string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                string result_2 = result_1.Replace(")", "");
                if (symbol == null)
                {
                    if (num1 == null)
                    {
                        num1 = result_2;
                    }
                    symbol = subtract;
                    label1.Text = label1.Text + symbol;
                    label2.Text = result_2;
                }
                else
                {
                    int last = label1.Text.Length - 1;
                    string Last = label1.Text.Substring(last);
                    if (Last == "+" || Last == "-" || Last == "×" || Last == "÷") 
                    {
                        symbol = subtract;
                        string del = label1.Text;
                        label1.Text = del.Substring(0, del.Length - 1) + symbol;
                    }
                    else
                    {
                        int s1 = result_2.LastIndexOf("+");
                        int s2 = result_2.LastIndexOf("-");
                        int s3 = result_2.LastIndexOf("×");
                        int s4 = result_2.LastIndexOf("÷");
                        int[] sy = { s1, s2, s3, s4 };
                        Array.Sort(sy);
                        int max = sy[3];
                        num2 = result_2.Substring(max + 1);
                        num1 = rt.ToString();
                        symbol = subtract;
                        label1.Text = label1.Text + symbol;
                        label2.Text = rt.ToString();
                        num2 = null;
                    }
                }
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            string one = "1";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + one;//将数字投射到屏幕上
                result_2 = result_2 + one;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            string two = "2";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + two;//将数字投射到屏幕上
                result_2=result_2 + two;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            string three = "3";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + three;//将数字投射到屏幕上
                result_2 = result_2 + three;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string add = "+";
            if (symbol == null && label1.Text.Length < 1 && num1 == null)
            {
                num1 = "0";
                symbol = add;
                label1.Text = label1.Text + symbol;
            }
            else
            {
                string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
                string result_2 = result_1.Replace(")", "");
                if (symbol == null)
                {
                    if (num1 == null)
                    {
                        num1 = result_2;
                    }
                    symbol = add;
                    label1.Text = label1.Text + symbol;
                    label2.Text = result_2;
                }
                else
                {
                    int last = label1.Text.Length - 1;
                    string Last = label1.Text.Substring(last);
                    if (Last == "+" || Last == "-" || Last == "×" || Last == "÷") 
                    {
                        symbol = add;
                        string del = label1.Text;
                        label1.Text = del.Substring(0, del.Length - 1) + symbol;
                    }
                    else
                    {
                        int s1 = result_2.LastIndexOf("+");
                        int s2 = result_2.LastIndexOf("-");
                        int s3 = result_2.LastIndexOf("×");
                        int s4 = result_2.LastIndexOf("÷");
                        int[] sy = { s1, s2, s3, s4 };
                        Array.Sort(sy);
                        int max = sy[3];
                        num2 = result_2.Substring(max + 1);
                        num1 = rt.ToString();
                        symbol = add;
                        label1.Text = label1.Text + symbol;
                        label2.Text = rt.ToString();
                        num2 = null;
                    }
                }
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button20_Click(object sender, EventArgs e)
        {
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length == 0)
            {
                MessageBox.Show("error");
            }
            else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
            {
                MessageBox.Show("error");
            }
            else
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int r1 = label1.Text.LastIndexOf("+");
                int r2 = label1.Text.LastIndexOf("-");
                int r3 = label1.Text.LastIndexOf("×");
                int r4 = label1.Text.LastIndexOf("÷");
                int k1=label1.Text.IndexOf(")");
                int[] sy = { s1, s2, s3, s4 };
                int[] ry = { r1, r2, r3, r4 };
                Array.Sort(sy);
                Array.Sort(ry);
                int max = sy[3];
                int max1 = ry[3];
                double reserve = double.Parse(result_2.Substring(max + 1));
                reserve = reserve / 100;
                if (k1==-1)
                {
                    label1.Text = label1.Text.Substring(0, max1 + 1) + reserve;
                }
                else
                {
                    label1.Text = label1.Text.Substring(0, max1 + 1) + reserve+")";
                }
                if (num1 != null && symbol != null)
                {
                    num2 = reserve.ToString();
                    if (symbol == "+")
                    {
                        rt = double.Parse(label2.Text) + double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                    else if (symbol == "-")
                    {
                        rt = double.Parse(label2.Text) - double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                    else if (symbol == "×")
                    {
                        rt = double.Parse(label2.Text) * double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                    else if (symbol == "÷")
                    {
                        rt = double.Parse(label2.Text) / double.Parse(num2);
                        label2.Text = rt.ToString();
                    }
                }
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            string zero = "0";
            string result_1 = label1.Text.Replace("(", "");//替换字符串中特定字符:string.Replace("原字符","替换后的字符")
            string result_2 = result_1.Replace(")", "");
            if (label1.Text.Length < 30)
            {
                label1.Text = label1.Text + zero;//将数字投射到屏幕上
                result_2 = result_2 + zero;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
            if (num1 != null && symbol != null)
            {
                int s1 = result_2.LastIndexOf("+");
                int s2 = result_2.LastIndexOf("-");
                int s3 = result_2.LastIndexOf("×");
                int s4 = result_2.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = result_2.Substring(max + 1);
                if (symbol == "+")
                {
                    rt = double.Parse(label2.Text) + double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(label2.Text) - double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(label2.Text) * double.Parse(num2);
                    label2.Text = rt.ToString();
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(label2.Text) / double.Parse(num2);
                    label2.Text = rt.ToString();
                }
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            string dot = ".";
            int f=label1.Text.IndexOf(".");
            int last = label1.Text.Length - 1;
            string Last = label1.Text.Substring(last);
            if (symbol == null)
            {
                if (f == -1)
                {
                    if (label1.Text.Length == 0)
                    {
                        MessageBox.Show("error1");
                    }
                    else if (Last == "+" || Last == "-" || Last == "×" || Last == "÷")
                    {
                        MessageBox.Show("error2");
                    }
                    else
                    {
                        if (label1.Text.Length < 30)
                        {
                            label1.Text = label1.Text + dot;
                            if (label1.Text.Length > 16)
                            {
                                label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                            }
                            else
                            {
                                label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                            }
                        }
                        else
                        {
                            MessageBox.Show("字符长度超出范围");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("error3");
                }
            }
            else
            {
                int s1 = label1.Text.LastIndexOf("+");
                int s2 = label1.Text.LastIndexOf("-");
                int s3 = label1.Text.LastIndexOf("×");
                int s4 = label1.Text.LastIndexOf("÷");
                int[] sy = { s1, s2, s3, s4 };
                Array.Sort(sy);
                int max = sy[3];
                num2 = label1.Text.Substring(max + 1);
                int f2 = num2.IndexOf(".");
                if (f2 == -1)
                {
                    if (Last == "+" || Last == "-" || Last == "×" || Last == "÷")
                    {
                        MessageBox.Show("error4");
                    }
                    else
                    {
                        if (label1.Text.Length < 30)
                        {
                            label1.Text = label1.Text + dot;
                            if (label1.Text.Length > 16)
                            {
                                label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                            }
                            else
                            {
                                label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                            }
                        }
                        else
                        {
                            MessageBox.Show("字符长度超出范围");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("error5");
                }
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (symbol == null && label1.Text.Length < 1)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                if (symbol == null) {
                    label1.Text = label1.Text;
                }
                else
                {
                    label1.Text =label2.Text;
                    label2.Text=null;
                }
            }
        }
    }
}


http://www.kler.cn/a/404114.html

相关文章:

  • 小熊派Nano接入华为云
  • 学习GENTOO系统中的emerge -uDNavg @world命令
  • 现在转前端怎么样?
  • 阿里云IIS虚拟主机部署ssl证书
  • JDBC使用p6spy记录实际执行SQL方法【解决SQL打印两次问题】
  • 不能打开网页,但能打开QQ、微信(三种方式)
  • Stable Diffusion中U-Net的前世今生与核心知识
  • 【Ubuntu】如何在Ubuntu系统中查看端口是否可用
  • VIM的下载使用与基本指令【入门级别操作】
  • Java基础终章篇(10)容器类与集合操作
  • 小熊派Nano接入华为云
  • Linux环境开启MongoDB的安全认证
  • 实验室管理自动化:Spring Boot技术的应用
  • 【PostgreSQL使用pg_filedump工具解析数据文件以恢复数据】
  • springboot基于Spring Boot的古城景区管理系统的设计与实现docx
  • C# IO文件操作
  • litepal proguardFiles android studio
  • java.nio.charset.MalformedInputException: Input length = 1
  • el-input绑定点击回车事件意外触发页面刷新
  • Python数据分析NumPy和pandas(四十、Python 中的建模库statsmodels 和 scikit-learn)
  • 【数据结构】用四个例子来理解动态规划算法
  • 一天速转golang!
  • docker 相关组成
  • 网络中的TCP协议详解
  • vxe-table 打印出货单、自定义打印单据
  • 【2024APMCM亚太杯B题】空调形状优化 模型+代码+论文