计算器
可以分一下类
数字键
+ - * / %
=和撤销
归零C最简单
下面看一下计算器具体的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication4
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} //记录是否刚点过运算符
private bool isok = true;
//记录结果
private decimal Sum;
//记录上一次的运算符
private string PreYunsuanfu;
//记录表达式
private string Biaodashi; private void btnling_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
// isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnyi_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btner_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnsan_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnsi_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnwu_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnliu_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnqi_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnba_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnjiu_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnjia_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "+";
PreYunsuanfu = "+";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnjian_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "-";
PreYunsuanfu = "-";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btncheng_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "*";
PreYunsuanfu = "*";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnchu_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "/";
PreYunsuanfu = "/";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnmo_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "%";
PreYunsuanfu = "%";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnClear_Click(object sender, EventArgs e)
{
txtshuru.Text = "";
txtjisuanshi.Text = "";
isok = true;
PreYunsuanfu = null;
Sum = ;
Biaodashi = "";
} private void btnchexiao_Click(object sender, EventArgs e)
{
if (!isok)
{
if (txtshuru.Text.Length == )
{
txtshuru.Text = "";
isok = true;
}
else
{
txtshuru.Text = txtshuru.Text.Substring(, txtshuru.Text.Length - );
}
}
} private void btndian_Click(object sender, EventArgs e)
{
if (txtshuru.Text == "")
{
txtshuru.Text = "0.";
}
if (!isok && !txtshuru.Text.Contains("."))
{
txtshuru.Text += ".";
}
isok = false;
} private void btndeng_Click(object sender, EventArgs e)
{
txtjisuanshi.Text = "";
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
}
txtshuru.Text = Sum.ToString();
isok = true;
Biaodashi = "";
Sum = ;
PreYunsuanfu = null;
} private void btn*_Click(object sender, EventArgs e)
{
if (!isok)
{
if (decimal.Parse(txtshuru.Text) > )
{
txtshuru.Text = "-" + txtshuru.Text;
}
else
{
txtshuru.Text = txtshuru.Text.Substring();
}
}
} }
}
计算器 很详细的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace FirstForm
{
public partial class JiSuanQi : Form
{
public JiSuanQi()
{
InitializeComponent();
} private double sum=;
private string biaodashi;
private string Preyunsuanfu;//存放之前的运算符
private bool isok = true;//用来判断是否刚刚点过运算符 private void btnling_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (isok)
{
txtshuru.Text = btn.Text;
isok = false;
}
else
{
txtshuru.Text += btn.Text;
} } private void btnjia_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string yunsuanfu = btn.Text;
if (Preyunsuanfu == null)
{
biaodashi += txtshuru.Text;
sum = double.Parse(txtshuru.Text);
}
else
{
if (!isok)
{
if (Preyunsuanfu == "+")
{
sum += double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "-")
{
sum -= double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "*")
{
sum *= double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "/")
{
sum /= double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "%")
{
sum %= double.Parse(txtshuru.Text);
} biaodashi = biaodashi+ Preyunsuanfu + txtshuru.Text;
txtshuru.Text = sum.ToString();
}
}
txtjisuanshi.Text = biaodashi + yunsuanfu;
Preyunsuanfu = yunsuanfu;
isok = true; } private void btndeng_Click(object sender, EventArgs e)
{ } private void btnchexiao_Click(object sender, EventArgs e)
{ }
}
}
计算器 另一种方法