目录
一 原型
二 模仿效果
三 后台代码
namespace 模仿windows自带计算器
{
public partial class Prefix : Form
{
public Prefix()
{
InitializeComponent();
}
string OperandsA = ""; //操作数A
string Operator = ""; //操作符
private void PerCent_Click(object sender, EventArgs e)
{
Operator = PerCent.Text;
OperandsA += Operator;
Display.Text = OperandsA;
}
private void CE_Click(object sender, EventArgs e)
{
Operator = "";
Display.Text = "";
OperandsA = "";
}
private void C_Click(object sender, EventArgs e)
{
int index = OperandsA.Length;
if (index > 0)
{
OperandsA = OperandsA.Remove(index - 1, 1);
Display.Text = OperandsA;
}
}
private void Clear_Click(object sender, EventArgs e)
{
Operator = "";
Display.Text = "";
OperandsA = "";
}
private void CentOne_Click(object sender, EventArgs e)
{
try
{
decimal res = 1 / decimal.Parse(Display.Text);
Display.Text = res.ToString();
OperandsA = Display.Text;
}
catch (Exception)
{
Operator = "";
Display.Text = "";
OperandsA = "";
MessageBox.Show("算式有误,请检查");
}
}
private void Square_Click(object sender, EventArgs e)
{
try
{
double res = double.Parse(Display.Text) * double.Parse(Display.Text);
Display.Text = res.ToString();
OperandsA = Display.Text;
}
catch (Exception)
{
Operator = "";
Display.Text = "";
OperandsA = "";
MessageBox.Show("算式有误,请检查");
}
}
private void SquareRoot_Click(object sender, EventArgs e)
{
try
{
double res = Math.Sqrt(double.Parse(Display.Text));
Display.Text = res.ToString();
OperandsA = Display.Text;
}
catch (Exception)
{
Operator = "";
Display.Text = "";
OperandsA = "";
MessageBox.Show("算式有误,请检查");
}
}
//除
private void Except_Click(object sender, EventArgs e)
{
Operator = Except.Text;
OperandsA += Operator;
Display.Text = OperandsA;
}
private void Ride_Click(object sender, EventArgs e)
{
Operator = Ride.Text;
OperandsA += Operator;
Display.Text = OperandsA;
}
private void Reduce_Click(object sender, EventArgs e)
{
Operator = Reduce.Text;
OperandsA += Operator;
Display.Text = OperandsA;
}
private void Add_Click(object sender, EventArgs e)
{
Operator = Add.Text;
OperandsA += Operator;
Display.Text = OperandsA;
}
private void ETC_Click(object sender, EventArgs e)
{
//分割符号,根据符号进行计算
Display.Text = sum(Display.Text).ToString();
OperandsA = "";
Operator = Display.Text;
}
int clickCount = 0;
private void PrefixAddOrReduce_Click(object sender, EventArgs e)
{
clickCount++;
Operator = PrefixAddOrReduce.Text;
if (clickCount % 2 == 0)
{
OperandsA = OperandsA.Replace("+", "").Replace("-", "");
OperandsA = "+" + OperandsA;
}
else
{
OperandsA = OperandsA.Replace("+", "").Replace("-", "");
OperandsA = "-" + OperandsA;
}
Display.Text = OperandsA;
}
private void Point_Click(object sender, EventArgs e)
{
OperandsA += Point.Text;
Display.Text = OperandsA;
}
private void Zero_Click(object sender, EventArgs e)
{
OperandsA += Zero.Text;
Display.Text = OperandsA;
}
private void One_Click(object sender, EventArgs e)
{
OperandsA += One.Text;
Display.Text = OperandsA;
}
private void Two_Click(object sender, EventArgs e)
{
OperandsA += Two.Text;
Display.Text = OperandsA;
}
private void Three_Click(object sender, EventArgs e)
{
OperandsA += Three.Text;
Display.Text = OperandsA;
}
private void Four_Click(object sender, EventArgs e)
{
OperandsA += Four.Text;
Display.Text = OperandsA;
}
private void Five_Click(object sender, EventArgs e)
{
OperandsA += Five.Text;
Display.Text = OperandsA;
}
private void Six_Click(object sender, EventArgs e)
{
OperandsA += Six.Text;
Display.Text = OperandsA;
}
private void Seven_Click(object sender, EventArgs e)
{
OperandsA += Seven.Text;
Display.Text = OperandsA;
}
private void Eight_Click(object sender, EventArgs e)
{
OperandsA += Eight.Text;
Display.Text = OperandsA;
}
private void Nine_Click(object sender, EventArgs e)
{
OperandsA += Nine.Text;
Display.Text = OperandsA;
}
private double sum(string str)
{
string[] arr = { "0", "0" };
double sum = 0;
try
{
if (str.Contains("%"))
{
arr = str.Split("%");
sum = double.Parse(arr[0]) % double.Parse(arr[1]);
}
if (str.Contains("÷"))
{
arr = str.Split("÷");
sum = double.Parse(arr[0]) / double.Parse(arr[1]);
}
if (str.Contains("×"))
{
arr = str.Split("×");
sum = double.Parse(arr[0]) * double.Parse(arr[1]);
}
if (str.Contains("-"))
{
arr = str.Split("-");
sum = double.Parse(arr[0]) - double.Parse(arr[1]);
}
if (str.Contains("+"))
{
arr = str.Split("+");
sum = double.Parse(arr[0]) + double.Parse(arr[1]);
}
}
catch (Exception)
{
Operator = "";
Display.Text = "";
OperandsA = "";
MessageBox.Show("算式有误,请检查");
}
return sum;
}
}
}
设计器自动生成代码:
namespace 模仿windows自带计算器
{
partial class Prefix
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
Display = new RichTextBox();
PerCent = new Button();
Clear = new Button();
C = new Button();
CE = new Button();
Square = new Button();
SquareRoot = new Button();
Except = new Button();
CentOne = new Button();
Eight = new Button();
Nine = new Button();
Ride = new Button();
Seven = new Button();
Five = new Button();
Six = new Button();
Reduce = new Button();
Four = new Button();
Two = new Button();
Three = new Button();
Add = new Button();
One = new Button();
Zero = new Button();
Point = new Button();
ETC = new Button();
PrefixAddOrReduce = new Button();
SuspendLayout();
//
// Display
//
Display.Font = new Font("Microsoft YaHei UI", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point, 134);
Display.Location = new Point(12, 24);
Display.Name = "Display";
Display.ReadOnly = true;
Display.Size = new Size(525, 154);
Display.TabIndex = 0;
Display.Text = "";
//
// PerCent
//
PerCent.Location = new Point(12, 196);
PerCent.Name = "PerCent";
PerCent.Size = new Size(94, 29);
PerCent.TabIndex = 1;
PerCent.Text = "%";
PerCent.UseVisualStyleBackColor = true;
PerCent.Click += PerCent_Click;
//
// Clear
//
Clear.Location = new Point(441, 196);
Clear.Name = "Clear";
Clear.Size = new Size(94, 29);
Clear.TabIndex = 2;
Clear.Text = "清除";
Clear.UseVisualStyleBackColor = true;
Clear.Click += Clear_Click;
//
// C
//
C.Location = new Point(298, 196);
C.Name = "C";
C.Size = new Size(94, 29);
C.TabIndex = 3;
C.Text = "C";
C.UseVisualStyleBackColor = true;
C.Click += C_Click;
//
// CE
//
CE.Location = new Point(155, 196);
CE.Name = "CE";
CE.Size = new Size(94, 29);
CE.TabIndex = 4;
CE.Text = "CE";
CE.UseVisualStyleBackColor = true;
CE.Click += CE_Click;
//
// Square
//
Square.Location = new Point(155, 249);
Square.Name = "Square";
Square.Size = new Size(94, 29);
Square.TabIndex = 8;
Square.Text = "x^2";
Square.UseVisualStyleBackColor = true;
Square.Click += Square_Click;
//
// SquareRoot
//
SquareRoot.Location = new Point(298, 249);
SquareRoot.Name = "SquareRoot";
SquareRoot.Size = new Size(94, 29);
SquareRoot.TabIndex = 7;
SquareRoot.Text = "2√x";
SquareRoot.UseVisualStyleBackColor = true;
SquareRoot.Click += SquareRoot_Click;
//
// Except
//
Except.Location = new Point(441, 249);
Except.Name = "Except";
Except.Size = new Size(94, 29);
Except.TabIndex = 6;
Except.Text = "÷";
Except.UseVisualStyleBackColor = true;
Except.Click += Except_Click;
//
// CentOne
//
CentOne.Location = new Point(12, 249);
CentOne.Name = "CentOne";
CentOne.Size = new Size(94, 29);
CentOne.TabIndex = 5;
CentOne.Text = "1/x";
CentOne.UseVisualStyleBackColor = true;
CentOne.Click += CentOne_Click;
//
// Eight
//
Eight.Location = new Point(155, 304);
Eight.Name = "Eight";
Eight.Size = new Size(94, 29);
Eight.TabIndex = 12;
Eight.Text = "8";
Eight.UseVisualStyleBackColor = true;
Eight.Click += Eight_Click;
//
// Nine
//
Nine.Location = new Point(298, 304);
Nine.Name = "Nine";
Nine.Size = new Size(94, 29);
Nine.TabIndex = 11;
Nine.Text = "9";
Nine.UseVisualStyleBackColor = true;
Nine.Click += Nine_Click;
//
// Ride
//
Ride.Location = new Point(441, 304);
Ride.Name = "Ride";
Ride.Size = new Size(94, 29);
Ride.TabIndex = 10;
Ride.Text = "×";
Ride.UseVisualStyleBackColor = true;
Ride.Click += Ride_Click;
//
// Seven
//
Seven.Location = new Point(12, 304);
Seven.Name = "Seven";
Seven.Size = new Size(94, 29);
Seven.TabIndex = 9;
Seven.Text = "7";
Seven.UseVisualStyleBackColor = true;
Seven.Click += Seven_Click;
//
// Five
//
Five.Location = new Point(155, 364);
Five.Name = "Five";
Five.Size = new Size(94, 29);
Five.TabIndex = 16;
Five.Text = "5";
Five.UseVisualStyleBackColor = true;
Five.Click += Five_Click;
//
// Six
//
Six.Location = new Point(298, 364);
Six.Name = "Six";
Six.Size = new Size(94, 29);
Six.TabIndex = 15;
Six.Text = "6";
Six.UseVisualStyleBackColor = true;
Six.Click += Six_Click;
//
// Reduce
//
Reduce.Location = new Point(441, 364);
Reduce.Name = "Reduce";
Reduce.Size = new Size(94, 29);
Reduce.TabIndex = 14;
Reduce.Text = "-";
Reduce.UseVisualStyleBackColor = true;
Reduce.Click += Reduce_Click;
//
// Four
//
Four.Location = new Point(12, 364);
Four.Name = "Four";
Four.Size = new Size(94, 29);
Four.TabIndex = 13;
Four.Text = "4";
Four.UseVisualStyleBackColor = true;
Four.Click += Four_Click;
//
// Two
//
Two.Location = new Point(155, 425);
Two.Name = "Two";
Two.Size = new Size(94, 29);
Two.TabIndex = 20;
Two.Text = "2";
Two.UseVisualStyleBackColor = true;
Two.Click += Two_Click;
//
// Three
//
Three.Location = new Point(298, 425);
Three.Name = "Three";
Three.Size = new Size(94, 29);
Three.TabIndex = 19;
Three.Text = "3";
Three.UseVisualStyleBackColor = true;
Three.Click += Three_Click;
//
// Add
//
Add.Location = new Point(441, 425);
Add.Name = "Add";
Add.Size = new Size(94, 29);
Add.TabIndex = 18;
Add.Text = "+";
Add.UseVisualStyleBackColor = true;
Add.Click += Add_Click;
//
// One
//
One.Location = new Point(12, 425);
One.Name = "One";
One.Size = new Size(94, 29);
One.TabIndex = 17;
One.Text = "1";
One.UseVisualStyleBackColor = true;
One.Click += One_Click;
//
// Zero
//
Zero.Location = new Point(155, 486);
Zero.Name = "Zero";
Zero.Size = new Size(94, 29);
Zero.TabIndex = 24;
Zero.Text = "0";
Zero.UseVisualStyleBackColor = true;
Zero.Click += Zero_Click;
//
// Point
//
Point.Location = new Point(298, 486);
Point.Name = "Point";
Point.Size = new Size(94, 29);
Point.TabIndex = 23;
Point.Text = ".";
Point.UseVisualStyleBackColor = true;
Point.Click += Point_Click;
//
// ETC
//
ETC.Location = new Point(441, 486);
ETC.Name = "ETC";
ETC.Size = new Size(94, 29);
ETC.TabIndex = 22;
ETC.Text = "=";
ETC.UseVisualStyleBackColor = true;
ETC.Click += ETC_Click;
//
// PrefixAddOrReduce
//
PrefixAddOrReduce.Location = new Point(12, 486);
PrefixAddOrReduce.Name = "PrefixAddOrReduce";
PrefixAddOrReduce.Size = new Size(94, 29);
PrefixAddOrReduce.TabIndex = 21;
PrefixAddOrReduce.Text = "+/-";
PrefixAddOrReduce.UseVisualStyleBackColor = true;
PrefixAddOrReduce.Click += PrefixAddOrReduce_Click;
//
// Prefix
//
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(545, 530);
Controls.Add(Zero);
Controls.Add(Point);
Controls.Add(ETC);
Controls.Add(PrefixAddOrReduce);
Controls.Add(Two);
Controls.Add(Three);
Controls.Add(Add);
Controls.Add(One);
Controls.Add(Five);
Controls.Add(Six);
Controls.Add(Reduce);
Controls.Add(Four);
Controls.Add(Eight);
Controls.Add(Nine);
Controls.Add(Ride);
Controls.Add(Seven);
Controls.Add(Square);
Controls.Add(SquareRoot);
Controls.Add(Except);
Controls.Add(CentOne);
Controls.Add(CE);
Controls.Add(C);
Controls.Add(Clear);
Controls.Add(PerCent);
Controls.Add(Display);
MaximizeBox = false;
Name = "Prefix";
Text = "计算器";
ResumeLayout(false);
}
#endregion
private RichTextBox Display;
private Button PerCent;
private Button Clear;
private Button C;
private Button CE;
private Button Square;
private Button SquareRoot;
private Button Except;
private Button CentOne;
private Button Eight;
private Button Nine;
private Button Ride;
private Button Seven;
private Button Five;
private Button Six;
private Button Reduce;
private Button Four;
private Button Two;
private Button Three;
private Button Add;
private Button One;
private Button Zero;
private Button Point;
private Button ETC;
private Button PrefixAddOrReduce;
}
}