我的老师和你的一样
学了一点皮毛就叫我写了
我写了如下代码
希望对你有帮助using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void kong()
{
if (t.Text == "0" || shu == "+" || shu == "-" || shu == "*" || shu == "/" || shu == "=" || shu == "NO")
{
t.Text = "";
}
}
//1
private void b1_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "1";
if (shu == "1")
{
t.Text = t.Text + 1;
}
}
}
//3
private void b3_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "3";
if (shu == "3")
{
t.Text = t.Text + 3;
}
}
}
//6
private void b6_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "6";
if (shu == "6")
{
t.Text = t.Text + 6;
}
}
}
//5
private void b5_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "5";
if (shu == "5")
{
t.Text = t.Text + 5;
}
}
}
//4
private void b4_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "4";
if (shu == "4")
{
t.Text = t.Text + 4;
}
}
}
//7
private void b7_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "7";
if (shu == "7")
{
t.Text = t.Text + 7;
}
}
}
//8
private void b8_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "8";
if (shu == "8")
{
t.Text = t.Text + 8;
}
}
}
//9
private void b9_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "9";
if (shu == "9")
{
t.Text = t.Text + 9;
}
}
}
//0
private void b0_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "0";
if (shu == "0")
{
t.Text = t.Text + 0;
}
}
}
decimal i = 0;
string shu = "";
string huhao = "";
string myhuhao = "";
//jia 加
private void jia_Click(object sender, EventArgs e)
{
decimal a = 0;
a = a + i;
if (myhuhao == "+")
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
myhuhao = "+";
}
else
{
return;
}
}
bool aa = decimal.TryParse(t.Text, out i);
if (aa == true)
{
if (a == 0)
{
t.Text = t.Text;
if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
}
else
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
if (myhuhao == "+")
{
t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "-")
{
t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "/")
{
if (t.Text == "0")
{
MessageBox.Show("除数不为0");
}
else
{
t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
}
}
}
}
}
else
{
MessageBox.Show("只可以输入数字");
}
decimal.TryParse(t.Text, out i);
if (aa == true)
{
shu = "+";
huhao = "+";
myhuhao = "+";
dian = "off";
}
}
//jian 减
private void jian_Click(object sender, EventArgs e)
{
decimal a = 0;
a = a + i;
if (myhuhao == "-")
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
myhuhao = "-";
}
else
{
return;
}
}
bool aa = decimal.TryParse(t.Text, out i);
if (aa == true)
{
if (a == 0)
{
t.Text = t.Text;
if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
}
else
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
if (myhuhao == "+")
{
t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "-")
{
t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "/")
{
if (t.Text == "0")
{
MessageBox.Show("除数不为0");
}
else
{
t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
}
}
}
}
}
else
{
MessageBox.Show("只可以输入数字");
}
decimal.TryParse(t.Text, out i);
if (aa == true)
{
shu = "-";
huhao = "-";
myhuhao = "-";
dian = "off";
}
}
//cheng 乘
private void cheng_Click(object sender, EventArgs e)
{
decimal a = 0;
a = a + i;
if (myhuhao == "*")
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
myhuhao = "*";
}
else
{
return;
}
}
bool aa = decimal.TryParse(t.Text, out i);
if (aa == true)
{
if (a == 0)
{
t.Text = t.Text;
if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
}
else
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
if (myhuhao == "+")
{
t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "-")
{
t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "/")
{
if (t.Text == "0")
{
MessageBox.Show("除数不为0");
}
else
{
t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
}
}
}
}
}
else
{
MessageBox.Show("只可以输入数字");
}
decimal.TryParse(t.Text, out i);
if (aa == true)
{
shu = "*";
huhao = "*";
myhuhao = "*";
dian = "off";
}
}
//chu 除
private void chu_Click(object sender, EventArgs e)
{
decimal a = 0;
a = a + i;
if (myhuhao == "/")
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
myhuhao = "/";
}
else
{
return;
}
}
bool aa = decimal.TryParse(t.Text, out i);
if (aa == true)
{
if (a == 0)
{
t.Text = t.Text;
if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
}
else
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
if (myhuhao == "+")
{
t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "-")
{
t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "*")
{
t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
}
else if (myhuhao == "/")
{
if (t.Text == "0")
{
MessageBox.Show("除数不为0");
}
else
{
t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
}
}
}
}
}
else
{
MessageBox.Show("只可以输入数字");
}
decimal.TryParse(t.Text, out i);
if (aa == true)
{
shu = "/";
huhao = "/";
myhuhao = "/";
dian = "off";
}
}
//deng 等
private void deng_Click(object sender, EventArgs e)
{
if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
{
myhuhao = "=";
}
else
{
return;
}
if (huhao == "+")
{
t.Text = (i + Convert.ToDecimal(t.Text)).ToString();
}
else if (huhao == "-")
{
t.Text = (i - Convert.ToDecimal(t.Text)).ToString();
}
else if (huhao == "*")
{
t.Text = (i * Convert.ToDecimal(t.Text)).ToString();
}
else if (huhao == "/")
{
if (t.Text == "0")
{
MessageBox.Show("除数不为0");
dian = "NO";
myhuhao = "NO";
t.Text = "0";
i = 0;
huhao = "off";
return;
}
else
{
t.Text = (i / Convert.ToDecimal(t.Text)).ToString();
}
}
myhuhao = "=";
shu = "NO";
dian = "off";
}
//2
private void b2_Click(object sender, EventArgs e)
{
dian = "NO";
if (myhuhao == "=")
{
MessageBox.Show("请选 + - * / 在按数字");
}
else
{
kong();
shu = "2";
if (shu == "2")
{
t.Text = t.Text + 2;
}
}
}
//C
private void C_Click(object sender, EventArgs e)
{
dian = "NO";
myhuhao = "NO";
t.Text = "0";
i = 0;
huhao = "off";
}
//点
string dian = "";
private void bu1_Click(object sender, EventArgs e)
{
if (dian == "off")
{
MessageBox.Show("按了 + - * / 不可以按<点>请选按数字,再按点.如:0.1等...");
}
else
{
//t.Text = ".";
if (t.Text == "")
{
//t.Text = "请输入数字";
return;
}
if (t.Text.IndexOf('.') == -1)
{
//t.text.indexof('.') 是查找字符串t中,字符"." 的位置
// 如果t中没有包含 .那么会返回 -1
//找不到
t.Text = t.Text + ".";
}
else
{
//找到. 已存在一个.
return;
}
}
}
}
}