计算器编程代码
更新时间: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,
} }\ }
正在阅读:
计算器编程代码03-21
2.1信息获取的一般过程08-25
安全试题题库05-27
精馏03-07
社区工作者考试复习资料04-30
垃圾箱的相遇作文600字07-04
膨胀土路基要求01-04
暑假小学日记400字10-29
Nodejs 给前端带来了什么变化12-03
高考满分作文十大技巧 - 图文01-03
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 计算器
- 编程
- 代码
- 心理教育主题班会总结
- 2017年中国自卸式电磁除铁器现状调研及市场前景预测(目录) -
- 中考数学试题分项版解析汇编第期专题统计与概率含解析5
- 2015-2020年中国乙烯利市场评估报告 - 图文
- 《数字逻辑》(白中英)(第六版)习题解答,DOC
- 2014年中央戏剧学院艺术本、专科招生简章
- 统计学试题
- 《工程结构抗震》课程复习要点
- 如何做好尽职调查
- 八年级(下)《爱莲说》《芙蕖》复习案
- 二外日语考研总结 - 图文
- 最新2019扬州郭猫儿,善口技 阅读附答案-范文模板(2页)
- CIS策划方案
- 桐乡市小升初小学生择校重点中学入学简历自荐信自我介绍word模板
- 行政管理学期末考试复习重点总结
- 水的自述作文450字完美版
- 人因实验报告修订版
- 单片机学习板的设计 - 图文
- 高中物理选修3-1《磁场》单元测试题(含答案)
- 海康高清数字视频监控方案(新)2018 - 图文