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

devtools/2024/11/26 4:43:59/

基于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.ppmy.cn/devtools/137023.html

相关文章

5、AI测试辅助-生成测试用例思维导图

AI测试辅助-生成测试用例思维导图 创建测试用例两种方式1、Plantuml思维导图版本 (不推荐&#xff09;2、Markdown思维导图版本&#xff08;推荐&#xff09; 创建测试用例两种方式 完整的测试用例通常需要包含以下的元素&#xff1a; 1、测试模块 2、测试标题 3、前置条件 4、…

webpack配置和打包性能优化

文章目录 webpack基础配置loaderpluginloader 和 plugin 的区别devServer打包性能优化1、按需引入组件2、externals 属性3、给定文件匹配范围4、noParse 属性5、cacheDirectory 缓存属性6、happyPack 多个子进程并行 webpack基础配置 mode:development&#xff1a;设置webpack…

推荐文章:FLUI Framework——打造流畅的微软界面体验

推荐文章&#xff1a;FLUI Framework——打造流畅的微软界面体验 FluiFrameworkBringing standardization to Fluent Design by providing easy-to-use styles and controls项目地址:https://gitcode.com/gh_mirrors/fl/FluiFramework 在追求极致用户体验的今天&#xff0c;开…

Github工作流

GitHub 工作流 是一种专门为 GitHub 上的代码协作和版本控制而设计的工作流&#xff0c;它强调通过 **拉取请求&#xff08;Pull Request&#xff0c;PR&#xff09;** 来管理代码的合并和审查。GitHub 工作流通常涉及到使用 **分支** 来进行功能开发和修复&#xff0c;并通过 …

基于物联网设计的人工淡水湖养殖系统(华为云IOT)_253

文章目录 一、前言1.1 项目介绍【1】项目开发背景【2】设计实现的功能【3】项目硬件模块组成【4】设计意义【5】国内外研究现状【6】摘要1.2 设计思路1.3 系统功能总结1.4 开发工具的选择【1】设备端开发【2】上位机开发1.5 参考文献1.6 系统框架图1.7 系统原理图1.8 实物图1.9…

开源动态表单form-create-designer 扩展个性化配置的最佳实践教程

在开源低代码表单设计器 form-create-designer 的右侧配置面板里&#xff0c;field 映射规则为开发者提供了强大的工具去自定义和增强组件及表单配置的显示方式。通过这些规则&#xff0c;你可以简单而高效地调整配置项的展示&#xff0c;提升用户体验。 源码地址: Github | G…

JMeter监听器与压测监控之 InfluxDB

1. 简介 在本文中&#xff0c;我们将介绍如何在 Kali Linux 上通过 Docker 安装 InfluxDB&#xff0c;并使用 JMeter 对其进行性能监控。InfluxDB 是一个高性能的时序数据库&#xff0c;而 JMeter 是一个开源的性能测试工具&#xff0c;可以用于对各种服务进行负载测试和性能监…

神经网络(系统性学习三):多层感知机(MLP)

相关文章&#xff1a; 神经网络中常用的激活函数 神经网络&#xff08;系统性学习一&#xff09;&#xff1a;入门篇 神经网络&#xff08;系统性学习二&#xff09;&#xff1a;单层神经网络&#xff08;感知机&#xff09; 多层感知机&#xff08;MLP&#xff09; 多层感…