计算器编程代码

更新时间:2024-03-21 06:40:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

//添加方法

public double add(double a,double c) {

return a+b; }

其余的你可以自己 写下就好了·都是一样的·只是符号不一样· 3个文本框 4个点击按钮 分别代表 + - * / 如果点击的+的按钮

你在+的点击事件里面 调用这个方法就好了

//添加按钮+的事件

private void btnX_1_Click(object sender, EventArgs e) {

//textbox3 是等于的值

this.textbox3.text=add(double.parse(this.textbox1.text),double.parse(this.textbox2.text)).tostring(); }

其余的按这个方法写就好了!

private void btnX_1_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_2_Click(object sender, EventArgs e) {

if (ttx_show.Text == \

{

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_3_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_4_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_5_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \

} }

private void btnX_6_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_7_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_8_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_9_Click(object sender, EventArgs e) {

if (ttx_show.Text == \

{

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_0_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_round_Click(object sender, EventArgs e) {

//小数点代码

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_bracket_left_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

private void btnX_bracket_right_Click(object sender, EventArgs e) {

if (ttx_show.Text == \ {

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 != \ {

ttx_show.Text += \ } }

#endregion

#region 【乘号】

private void btnX_multiply_Click(object sender, EventArgs e)

{

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【减号】

private void btnX_decrease_Click(object sender, EventArgs e) {

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【加号】

private void btnX_add_Click(object sender, EventArgs e) {

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【清零】

private void btnX_CE_Click(object sender, EventArgs e) { i = 0;

ttx_show.Text = i.ToString(\ }

#endregion

#endregion

#region 【自定义方法】 private void equal_fangfa() { try

{

equal = Evaluator.EvalToDouble(ttx_show.Text);//调用四则运算类 }

catch (Exception ec) {

MessageBox.Show(\错误代码为:\ }

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 == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D2) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D3) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D4) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D5) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D6) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ }

}

if ((int)e.KeyChar == (int)Keys.D7) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D8) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D9) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.D0) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

#region 【小键盘数字键】

if ((int)e.KeyChar == (int)Keys.NumPad1) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad2) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad3) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \

} }

if ((int)e.KeyChar == (int)Keys.NumPad4) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad5) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad6) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad7) {

if (ttx_show.Text == \

{

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad8) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad9) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \ } }

if ((int)e.KeyChar == (int)Keys.NumPad0) {

if (ttx_show.Text == \ {

ttx_show.Text = \ } else {

ttx_show.Text += \

} }

#endregion

#region 【加号】

if ((int)e.KeyChar == (int)Keys.Add) {

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【减号】

if ((int)e.KeyChar == (int)Keys.Subtract) {

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【乘号】

if ((int)e.KeyChar == (int)Keys.Multiply) {

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【除号】

if ((int)e.KeyChar == (int)Keys.Divide) {

if (ttx_show.Text != \ {

ttx_show.Text += \ } }

#endregion

#region 【小数点】

if ((int)e.KeyChar == (int)Keys.Decimal)//这里有问题,按小数点没反映 {

//小数点代码

if (ttx_show.Text == \ {

ttx_show.Text = \ } 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(\ }

#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(\object[] { statement }); }

static Evaluator() {

ICodeCompiler compiler;

compiler = new JScriptCodeProvider().CreateCompiler();

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

CompilerResults results; results _jscriptSource);

Assembly assembly = results.CompiledAssembly;

_evaluatorType = assembly.GetType(\

_evaluator = Activator.CreateInstance(_evaluatorType); }

private static object _evaluator = null; private static Type _evaluatorType = null; private static readonly string _jscriptSource = @\ Evaluator {

class Evaluator {

public function Eval(expr : String) : String

{

return eval(expr); }

=

compiler.CompileAssemblyFromSource(parameters,

} }\ }

本文来源:https://www.bwwdw.com/article/4hw8.html

Top