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;
}
}
}
}
}