using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 计算器
{
public partial class Form1 : Form
{
int flag;
string left, right;
string op;
public Form1()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(0);
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = Convert.ToString(0);
flag = 0;
left = "";
right = "";
op = "";
} private void button4_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(1);
else
this.textBox1.Text = Convert.ToString(1);
} private void button18_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(2);
else
this.textBox1.Text = Convert.ToString(2);
} private void button19_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(3);
else
this.textBox1.Text = Convert.ToString(3);
} private void button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(4);
else
this.textBox1.Text = Convert.ToString(4);
} private void button14_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(5);
else
this.textBox1.Text = Convert.ToString(5);
} private void button15_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(6);
else
this.textBox1.Text = Convert.ToString(6);
} private void button2_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(7);
else
this.textBox1.Text = Convert.ToString(7);
} private void button10_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(8);
else
this.textBox1.Text = Convert.ToString(8);
} private void button11_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(9);
else
this.textBox1.Text = Convert.ToString(9);
} private void button22_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf('.') > 0)
;
else
this.textBox1.Text = this.textBox1.Text + ".";
} private void button8_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.StartsWith("-"))
this.textBox1.Text = this.textBox1.Text.Substring(this.textBox1.Text.IndexOf('-') + 1);
else
this.textBox1.Text = "-" + this.textBox1.Text;
} private void button23_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
{
this.textBox1.Text = null;
this.textBox1.Text = Convert.ToString(ld + rd);
}
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = this.textBox1.Text;
op = "+";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "+";
flag = 1;
}
} private void button20_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = this.textBox1.Text;
op = "-";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "-";
flag = 1;
}
} private void button16_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = this.textBox1.Text;
op = "*";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "*";
flag = 1;
}
} private void button12_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
{
if (rd == 0)
{
;
}
else
this.textBox1.Text = Convert.ToString(ld / rd);
}
left = this.textBox1.Text;
op = "/";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "/";
flag = 1;
}
} private void button21_Click(object sender, EventArgs e)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = "";
right = "";
op = "";
flag = 0;
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = Convert.ToString(0);
flag = 0;
left = "";
right = "";
op = "";
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = this.textBox1.Text.Substring(0,this.textBox1.Text.Length-1);
if (this.textBox1.Text == "")
{ this.textBox1.Text = "0"; }
return;
}
}
}
例如:输入一个1,再输入一个加号,再输入一个1,文本框里显示的是11。怎么解决?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 计算器
{
public partial class Form1 : Form
{
int flag;
string left, right;
string op;
public Form1()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(0);
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = Convert.ToString(0);
flag = 0;
left = "";
right = "";
op = "";
} private void button4_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(1);
else
this.textBox1.Text = Convert.ToString(1);
} private void button18_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(2);
else
this.textBox1.Text = Convert.ToString(2);
} private void button19_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(3);
else
this.textBox1.Text = Convert.ToString(3);
} private void button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(4);
else
this.textBox1.Text = Convert.ToString(4);
} private void button14_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(5);
else
this.textBox1.Text = Convert.ToString(5);
} private void button15_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(6);
else
this.textBox1.Text = Convert.ToString(6);
} private void button2_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(7);
else
this.textBox1.Text = Convert.ToString(7);
} private void button10_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(8);
else
this.textBox1.Text = Convert.ToString(8);
} private void button11_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "0")
this.textBox1.Text = this.textBox1.Text + Convert.ToString(9);
else
this.textBox1.Text = Convert.ToString(9);
} private void button22_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf('.') > 0)
;
else
this.textBox1.Text = this.textBox1.Text + ".";
} private void button8_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.StartsWith("-"))
this.textBox1.Text = this.textBox1.Text.Substring(this.textBox1.Text.IndexOf('-') + 1);
else
this.textBox1.Text = "-" + this.textBox1.Text;
} private void button23_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
{
this.textBox1.Text = null;
this.textBox1.Text = Convert.ToString(ld + rd);
}
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = this.textBox1.Text;
op = "+";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "+";
flag = 1;
}
} private void button20_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = this.textBox1.Text;
op = "-";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "-";
flag = 1;
}
} private void button16_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = this.textBox1.Text;
op = "*";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "*";
flag = 1;
}
} private void button12_Click(object sender, EventArgs e)
{
if (flag == 1)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
{
if (rd == 0)
{
;
}
else
this.textBox1.Text = Convert.ToString(ld / rd);
}
left = this.textBox1.Text;
op = "/";
flag = 1;
}
if (flag == 0)
{
left = this.textBox1.Text;
op = "/";
flag = 1;
}
} private void button21_Click(object sender, EventArgs e)
{
right = this.textBox1.Text;
double ld = Convert.ToDouble(left);
double rd = Convert.ToDouble(right);
if (op == "+")
this.textBox1.Text = Convert.ToString(ld + rd);
if (op == "-")
this.textBox1.Text = Convert.ToString(ld - rd);
if (op == "*")
this.textBox1.Text = Convert.ToString(ld * rd);
if (op == "/")
this.textBox1.Text = Convert.ToString(ld / rd);
left = "";
right = "";
op = "";
flag = 0;
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = Convert.ToString(0);
flag = 0;
left = "";
right = "";
op = "";
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = this.textBox1.Text.Substring(0,this.textBox1.Text.Length-1);
if (this.textBox1.Text == "")
{ this.textBox1.Text = "0"; }
return;
}
}
}
例如:输入一个1,再输入一个加号,再输入一个1,文本框里显示的是11。怎么解决?
你那button21就是等号按钮吧?转换类型的时候判断一下值为不为空,为空就会出现异常