为什么我这个计算器在输完第二个数字的时候,结果却错误,比如11-11=10,真心求指教,代码如下;
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 计算
{ public partial class Form1 : Form
{
public enum CalcuType
{
None,
JIA,
JIAN,
CHENG,
CHU,
KAIFANG,
CHUFANG,
}
private double ? ValueF = null ;
private double ? ValueL = null ;
private CalcuType Calculatetype = CalcuType.None;
private bool isNew = false;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
btn0.Click +=new EventHandler(btn_Click);
btn1.Click += new EventHandler(btn_Click);
btn2.Click += new EventHandler(btn_Click);
btn3.Click += new EventHandler(btn_Click);
btn4.Click += new EventHandler(btn_Click);
btn5.Click += new EventHandler(btn_Click);
btn6.Click += new EventHandler(btn_Click);
btn7.Click += new EventHandler(btn_Click);
btn8.Click += new EventHandler(btn_Click);
btn9.Click += new EventHandler(btn_Click);
}
private void btn_Click(object sender, EventArgs e)//是不是这里面出问题啦?
{
Button btn = (Button)sender;
string NumberStr = btnShow.Text;
if (this.isNew)
{
NumberStr = btn.Text;
this.ValueL = double.Parse(NumberStr);
}
else
{
if (new string[] { "0", "0.", "-0", "-0." }.Contains(NumberStr ))
{
NumberStr = "";
}
NumberStr += btn.Text;
this.ValueF = double.Parse(NumberStr);
}
this.btnShow.Text = NumberStr ;
this.isNew = false;
}
private void btnResult_Click(object sender, EventArgs e)
{
switch (Calculatetype)
{
case CalcuType.JIA:
this.btnShow.Text = (ValueF + ValueL).ToString ();
break;
case CalcuType.JIAN:
this.btnShow.Text = (ValueF - ValueL).ToString();
break;
case CalcuType.CHENG:
this.btnShow.Text = (ValueF * ValueL).ToString();
break;
case CalcuType.CHU:
this.btnShow.Text = (ValueF / ValueL).ToString();
break;
case CalcuType.KAIFANG:
this .btnShow .Text =Math .Pow ((double ) ValueF ,(double ) ValueL ).ToString() ;
break;
case CalcuType.CHUFANG:
this.btnShow.Text = Math.Pow((double)ValueF, 1/(double)ValueL).ToString();
break;
}
this.ValueF = double.Parse(btnShow.Text);
this.isNew = true;
} private void btn归零_Click(object sender, EventArgs e)
{
this.ValueF = null;
this.ValueL = null;
this.Calculatetype = CalcuType.None;
this.btnShow.Text = "0.";
}
private void btnJIA_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.JIA ;
this.isNew = true ;
} private void btnJIAN_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.JIAN;
this.isNew = true;
} private void btnCHENG_Click(object sender, EventArgs e)
{
this.btnResult_Click( sender, e);
this.Calculatetype = CalcuType.CHENG;
this.isNew = true;
} private void btnCHU_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.CHU;
this.isNew = true;
} private void btnKAIFANG_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.KAIFANG;
this.isNew = true;
} private void btnCHUFANG_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.CHUFANG;
this.isNew = true;
} private void btn退格_Click(object sender, EventArgs e)
{
if (this.btnShow.Text.Length == 1)
{
this.btnShow.Text = "0.";
}
else
{
this.btnShow.Text = btnShow.Text.Substring(0, btnShow.Text.Length - 1);
}
} }
}
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 计算
{ public partial class Form1 : Form
{
public enum CalcuType
{
None,
JIA,
JIAN,
CHENG,
CHU,
KAIFANG,
CHUFANG,
}
private double ? ValueF = null ;
private double ? ValueL = null ;
private CalcuType Calculatetype = CalcuType.None;
private bool isNew = false;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
btn0.Click +=new EventHandler(btn_Click);
btn1.Click += new EventHandler(btn_Click);
btn2.Click += new EventHandler(btn_Click);
btn3.Click += new EventHandler(btn_Click);
btn4.Click += new EventHandler(btn_Click);
btn5.Click += new EventHandler(btn_Click);
btn6.Click += new EventHandler(btn_Click);
btn7.Click += new EventHandler(btn_Click);
btn8.Click += new EventHandler(btn_Click);
btn9.Click += new EventHandler(btn_Click);
}
private void btn_Click(object sender, EventArgs e)//是不是这里面出问题啦?
{
Button btn = (Button)sender;
string NumberStr = btnShow.Text;
if (this.isNew)
{
NumberStr = btn.Text;
this.ValueL = double.Parse(NumberStr);
}
else
{
if (new string[] { "0", "0.", "-0", "-0." }.Contains(NumberStr ))
{
NumberStr = "";
}
NumberStr += btn.Text;
this.ValueF = double.Parse(NumberStr);
}
this.btnShow.Text = NumberStr ;
this.isNew = false;
}
private void btnResult_Click(object sender, EventArgs e)
{
switch (Calculatetype)
{
case CalcuType.JIA:
this.btnShow.Text = (ValueF + ValueL).ToString ();
break;
case CalcuType.JIAN:
this.btnShow.Text = (ValueF - ValueL).ToString();
break;
case CalcuType.CHENG:
this.btnShow.Text = (ValueF * ValueL).ToString();
break;
case CalcuType.CHU:
this.btnShow.Text = (ValueF / ValueL).ToString();
break;
case CalcuType.KAIFANG:
this .btnShow .Text =Math .Pow ((double ) ValueF ,(double ) ValueL ).ToString() ;
break;
case CalcuType.CHUFANG:
this.btnShow.Text = Math.Pow((double)ValueF, 1/(double)ValueL).ToString();
break;
}
this.ValueF = double.Parse(btnShow.Text);
this.isNew = true;
} private void btn归零_Click(object sender, EventArgs e)
{
this.ValueF = null;
this.ValueL = null;
this.Calculatetype = CalcuType.None;
this.btnShow.Text = "0.";
}
private void btnJIA_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.JIA ;
this.isNew = true ;
} private void btnJIAN_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.JIAN;
this.isNew = true;
} private void btnCHENG_Click(object sender, EventArgs e)
{
this.btnResult_Click( sender, e);
this.Calculatetype = CalcuType.CHENG;
this.isNew = true;
} private void btnCHU_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.CHU;
this.isNew = true;
} private void btnKAIFANG_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.KAIFANG;
this.isNew = true;
} private void btnCHUFANG_Click(object sender, EventArgs e)
{
this. btnResult_Click( sender, e);
this.Calculatetype = CalcuType.CHUFANG;
this.isNew = true;
} private void btn退格_Click(object sender, EventArgs e)
{
if (this.btnShow.Text.Length == 1)
{
this.btnShow.Text = "0.";
}
else
{
this.btnShow.Text = btnShow.Text.Substring(0, btnShow.Text.Length - 1);
}
} }
}
“System.Array”并不包含“Contains”的定义
这是问题,请问是怎么回事吖??
{
NumberStr = btn.Text;
this.ValueL = double.Parse(NumberStr);
}
结果 11-11=10(实际11-1=10)C#My计算器源代码
http://download.csdn.net/detail/h_gxi/5103245