c#简易计算器程序

各位高手帮我写个简易计算器程序,谢谢!!
2024-11-29 23:53:51
推荐回答(3个)
回答(1):

#region 【按钮事件】
private void btnX_1_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "1";
}
else
{
ttx_show.Text += "1";
}
}

private void btnX_2_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "2";
}
else
{
ttx_show.Text += "2";
}
}

private void btnX_3_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "3";
}
else
{
ttx_show.Text += "3";
}
}

private void btnX_4_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "4";
}
else
{
ttx_show.Text += "4";
}
}

private void btnX_5_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "5";
}
else
{
ttx_show.Text += "5";
}
}

private void btnX_6_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "6";
}
else
{
ttx_show.Text += "6";
}
}

private void btnX_7_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "7";
}
else
{
ttx_show.Text += "7";
}
}

private void btnX_8_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "8";
}
else
{
ttx_show.Text += "8";
}
}

private void btnX_9_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "9";
}
else
{
ttx_show.Text += "9";
}
}

private void btnX_0_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "0";
}
else
{
ttx_show.Text += "0";
}
}

private void btnX_round_Click(object sender, EventArgs e)
{
//小数点代码
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "0.";
}
else
{
ttx_show.Text += ".";
}
}

private void btnX_bracket_left_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "(";
}
else
{
ttx_show.Text += "(";
}
}

private void btnX_bracket_right_Click(object sender, EventArgs e)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = ")";
}
else
{
ttx_show.Text += ")";
}
}

#region 【退格】
private void btnX_backspace_Click(object sender, EventArgs e)
{
ttx_show.Text = ttx_show.Text.Substring(0, ttx_show.Text.Length - 1);//退格
}
#endregion

#region 【等号】
private void btnX_equal_Click(object sender, EventArgs e)
{
equal_fangfa();
}
#endregion

#region 【除号】
private void btnX_divided_Click(object sender, EventArgs e)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "/";
}
}
#endregion

#region 【乘号】
private void btnX_multiply_Click(object sender, EventArgs e)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "*";
}
}
#endregion

#region 【减号】
private void btnX_decrease_Click(object sender, EventArgs e)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "-";
}
}
#endregion

#region 【加号】
private void btnX_add_Click(object sender, EventArgs e)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "+";
}
}
#endregion

#region 【清零】
private void btnX_CE_Click(object sender, EventArgs e)
{
i = 0;
ttx_show.Text = i.ToString("###,##0.00");
}
#endregion

#endregion

#region 【自定义方法】
private void equal_fangfa()
{
try
{
equal = Evaluator.EvalToDouble(ttx_show.Text);//调用四则运算类
}
catch (Exception ec)
{
MessageBox.Show("错误代码为:" + ec.Message);
}
ttx_show.Text = equal.ToString();
}
#endregion

#region 【键盘事件】
private void ttx_show_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar == (int)Keys.D1)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "1";
}
else
{
ttx_show.Text += "1";
}
}

if ((int)e.KeyChar == (int)Keys.D2)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "2";
}
else
{
ttx_show.Text += "2";
}
}

if ((int)e.KeyChar == (int)Keys.D3)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "3";
}
else
{
ttx_show.Text += "3";
}
}

if ((int)e.KeyChar == (int)Keys.D4)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "4";
}
else
{
ttx_show.Text += "4";
}
}

if ((int)e.KeyChar == (int)Keys.D5)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "5";
}
else
{
ttx_show.Text += "5";
}
}

if ((int)e.KeyChar == (int)Keys.D6)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "6";
}
else
{
ttx_show.Text += "6";
}
}

if ((int)e.KeyChar == (int)Keys.D7)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "7";
}
else
{
ttx_show.Text += "7";
}
}

if ((int)e.KeyChar == (int)Keys.D8)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "8";
}
else
{
ttx_show.Text += "8";
}
}

if ((int)e.KeyChar == (int)Keys.D9)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "9";
}
else
{
ttx_show.Text += "9";
}
}

if ((int)e.KeyChar == (int)Keys.D0)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "0";
}
else
{
ttx_show.Text += "0";
}
}

#region 【小键盘数字键】
if ((int)e.KeyChar == (int)Keys.NumPad1)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "1";
}
else
{
ttx_show.Text += "1";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad2)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "2";
}
else
{
ttx_show.Text += "2";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad3)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "3";
}
else
{
ttx_show.Text += "3";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad4)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "4";
}
else
{
ttx_show.Text += "4";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad5)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "5";
}
else
{
ttx_show.Text += "5";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad6)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "6";
}
else
{
ttx_show.Text += "6";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad7)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "7";
}
else
{
ttx_show.Text += "7";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad8)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "8";
}
else
{
ttx_show.Text += "8";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad9)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "9";
}
else
{
ttx_show.Text += "9";
}
}

if ((int)e.KeyChar == (int)Keys.NumPad0)
{
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "0";
}
else
{
ttx_show.Text += "0";
}
}
#endregion

#region 【加号】
if ((int)e.KeyChar == (int)Keys.Add)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "+";
}
}
#endregion

#region 【减号】
if ((int)e.KeyChar == (int)Keys.Subtract)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "-";
}
}
#endregion

#region 【乘号】
if ((int)e.KeyChar == (int)Keys.Multiply)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "*";
}
}
#endregion

#region 【除号】
if ((int)e.KeyChar == (int)Keys.Divide)
{
if (ttx_show.Text != "0.00")
{
ttx_show.Text += "/";
}
}
#endregion

#region 【小数点】
if ((int)e.KeyChar == (int)Keys.Decimal)//这里有问题,按小数点没反映
{
//小数点代码
if (ttx_show.Text == "0.00")
{
ttx_show.Text = "0.";
}
else
{
ttx_show.Text += ".";
}
}
#endregion

#region 【求结果-回车】
if ((int)e.KeyChar == (int)Keys.Enter)
{
equal_fangfa();
}
#endregion

#region 【退格键】
if ((int)e.KeyChar == (int)Keys.Back)
{
ttx_show.Text = ttx_show.Text.Substring(0, ttx_show.Text.Length - 1);//退格
}
#endregion

#region 【清零】
if ((int)e.KeyChar == (int)Keys.Escape)
{
i = 0;
ttx_show.Text = i.ToString("###,##0.00");
}
#endregion
}
#endregion

#region 【菜单--复制】
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ttx_show.SelectedText != "")
{
Clipboard.SetDataObject(ttx_show.SelectedText);
}
}
#endregion

#region 【菜单--粘贴】
private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
{
//莫看懂,拿来用
IDataObject iData = Clipboard.GetDataObject();

// Determines whether the data is in a format you can use.
if (iData.GetDataPresent(DataFormats.Text))
{
// Yes it is, so display it in a text box.
ttx_show.Text = (String)iData.GetData(DataFormats.Text);
}
}
#endregion

下面是四则混合计算类
class Evaluator
{
public static int EvalToInteger(string statement)
{
string s = EvalToString(statement);
return int.Parse(s.ToString());
}

public static double EvalToDouble(string statement)
{
string s = EvalToString(statement);
return double.Parse(s);
}

public static string EvalToString(string statement)
{
object o = EvalToObject(statement);
return o.ToString();
}

public static object EvalToObject(string statement)
{
return _evaluatorType.InvokeMember("Eval",BindingFlags.InvokeMethod,null,_evaluator,new object[] { statement });
}

static Evaluator()
{
ICodeCompiler compiler;
compiler = new JScriptCodeProvider().CreateCompiler();

CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;

CompilerResults results;
results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);

Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");

_evaluator = Activator.CreateInstance(_evaluatorType);
}

private static object _evaluator = null;
private static Type _evaluatorType = null;
private static readonly string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";

}

不是我写的,从其他程序里面剪出来的

回答(2):

帅哥,洗头吗?5块钱!
=============================
自己动手学着写吧,我帮你写几分钟就搞定,但你还是不会;你自己写可能要一个晚上,但你自己肯定学到了很多东西。

不知你是不是学生,多看书,多动手,不要毕业时找不到工作抱怨学校不好。

回答(3):

楼上的程序写的太霸道了!