各位大神帮帮忙呀,我是个初学者,设计一个和Windows附件计算器差不多的东西,大部分都是参考书上的,但是总是有几处是错误的
,不知道怎么改,在线等!急!
public partial class Form1 : Form
{
enum caculatorstate
{
WaitForOperand1, //等待第一个操作数
Operand1Inputing, //输入第一个操作数
WaitForOperand2, //等待第二个操作数
Operand2Inputing, //输入第二个操作数
Error //出错
}
//用于计算机的委托
delegate double ComputeDelegate(double num1,double num2);
//选中的计算类型(加,减,乘,除)
private ComputeDelegate operate;
//两个操作数
double operand1=0.0, operand2=0.0;
//计算机的状态
caculatorstate state = caculatorstate.WaitForOperand1;
private void numClick(object sender, EventArgs e)
{ //0至9数字按钮的Click事件处理程序
//如果错误则返回
if (AccessibleStates == caculatorstate.Error) |此处AccessibleStates红下划线
{
return;
}
Button button = (Button)sender;
//如果等待操作数,则用按钮上的文字替代TextBox的文字
if (AccessibleStates == caculatorstate.WaitForOperand1) |同上
{
textBox1.Text = button.Text;
AccessibleStates = caculatorstate.Operand1Inputing; |同上
}
else if (state == caculatorstate.WaitForOperand2)
{
textBox1.Text = button.Text;
state = caculatorstate.Operand2Inputing;
}
else //当前正在输入操作数,将按钮上的文字追加到TextBox中
{
if (textBox1.Text == “0”)
{
textBox1.Text = button.Text;
}
else
{
textBox1.AppendText(button.Text);
}
}
}
private void calculator_load(object sender, EventArgs e)
{
initializecalculator();
MaximizeBox = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
textBox1.ReadOnly = true;
num0.Click += numClick;
num1.Click += numClick;
num2.Click += numClick;
num3.Click += numClick;
num4.Click += numClick;
num5.Click += numClick;
num6.Click += numClick;
num7.Click += numClick;
num8.Click += numClick;
num9.Click += numClick;
}
//初始化计算器
private void initializecalculator()
{
textBox1.Text = “0”;
state = caculatorstate.WaitForOperand1;
operate = null;}
private double add(double d1, double d2) //加减乘除四个计算方法
{
return d1 + d2;
}
private double subtract(double d1, double d2)
{
return d1 – d2;
}
private double multiply(double d1, double d2)
{
return d1 * d2;
}
private double divide(double d1, double d2)
{
return d1 / d2;
}
private void caculate()//通过计算得到结果
{
if (operate == null)
return;
if (state == caculatorstate.Operand2Inputing)
{
operand2 = double.Parse(textBox1.Text);
}
try
{
double result = operate (operand1, operand2);
if (double.IsInfinity(result))
{
textBox1.Text = “结果无穷大。”;
state = caculatorstate.Error;
}
else
{
textBox1.Text = result.ToString();
operand1 = result;
}
}
catch (DivideByZeroException)
{
textBox1.Text = “除数不能为零。”;
state = caculatorstate.Error;
}
catch (OverflowException)
{
state = caculatorstate.Error;
textBox1.Text = “运算溢出。”;
}
}
}
}
private void operatorClick(object sender,EventArgs e); 此处void红下划线
{//加减乘除运算符按钮的事件处理程序
//如果计算机处于错误状态则返回
If (state==CaculatorState.Error)
{
Return;
}
//如果当时正在输入第二个操作数,则先计算出结果
If(state==CaculatorState.Operand2Inputing)
{
Calculate();
}
//设计计算委托
Btton button=(Button)sender;
Switch(button.Text)
{case”+”:
Operate=add;
Break;
Case”-“:
Operate=substract;
Break;
Case”*”:
Operate=multiply;
Break;
Case”/”:
Operate=divide;
Break;
}
//设置操作数
Operand1=double.Parse(textBox1.Text);
Operand2=operand1
//等待第二个操作数
State=CaculatoorState.WaitForOperand2;
}
Private void button20_Click(object sender,EventArgs e)
{//等号的事件处理程序
If(state==CaculatorState.Error)
{
Return;
}
Calculate();
//等待第一个操作数
State=CaculatorState.WaitForOperand1;
Private void pointClick(object sender,EventArgs e)
//小数点的事件处理程序
{
If (textBox1.Text.IndexOf(“.”)<0)
{
textBox1.AppendText(“.”);
}
}
Private void button22_Click(object sender,EventArgs e)
{//清除CE键的事件处理程序
textBox1.Text=”0″;
if(state==CaculatorState.Error)
{
State=CaculatorState.WaitForOperand1;
}
}
Private void button23_Click(object sender,EventArgs e)
{ //清除键C的事件处理程序
initializeCalculator();
}
Private void button5_Click(object sender,EventArgs e)
{//“sqrt”按钮的事件处理程序
If (state==CaculatorState.Error)
{
Return;
}
If(textBox1.Text.StartsWith(“-“))
{
textBox1.Text=”错误。”;
state=CaculatorState.Error;
}
Else
{
textBox1.Text=Math.Sqrt(double.Parse(textBox1.Text)).ToString();
state=CaculatorState.WaitForOperand1;
}
}
//正负号按钮的事件处理程序
Private void button17_Click(object sender,EventArgs e)
{
If(state==CaculatorState.Error)
{
Return;
}
If(textBox1.Text.StartsWith(“-“))
{
textBox1.Text=textBox1.Text.Substring(1);
}
Else
{textBox1.Text=”-“+textBox1.Text;
}
}
Private void button14_Click(object sender,EventArgs e)
{//倒数按钮的事件处理程序
If(state==CaculatorState.Error)
{
Return;
}
Double result=1.0/double.Parse(textBox1.Text);
If (double.IsInfinity(result))
{
textBox1.Text=”无穷大。”;
state=CaculatorState.Error;
}
Else
{
textBox1.Text=result.Tostring();
state=CaculatorState.WaitForOperand;
}
}
Private void button21_Click(object sender,EventArgs e)
{//退格键的事件处理程序
If(textBox1.Text.Length>1)
{
textBox1.Text=textBox1.TextSubstring(0,txetBox1.Text.Length-1);
}
Else
{
textBox1.Text=”0″;
}
}
}
错误 1 应输入 class、delegate、enum、interface 或 struct
错误 2 “System.Windows.Forms.AccessibleStates”是“类型”,但此处被当做“变量”来使用