【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《计算器编程代码》,欢迎阅读!
using using using using using using using using
System;
System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text;
System.Windows.Forms; namespace calculator {
public partial class Form1 : Form { public Form1()
InitializeComponent();
private void Form1_Load( object sender, EventArgs e) private void btn_Add_Click( object sender, EventArgs e)
Button btn = ( Button )sender;
// 空格用来分隔数值和运算符 txt_Result.Text = txt_Result.Text +
" " + btn .Text + " "
private void btn_Result_Click( object sender, EventArgs e)
// 保存计算结果 double d_result;
// 保存文本框中的算术表达式 string s_txt = txt_Result.Text; // 查找空格位置 int space = s_txt.IndexOf(
' ' );
//s1 保存第一个运算数 string s1 = s_txt.Substring( 0, space ); //operation 用
来保存运算数
char opoperation = Convert .ToChar(s_txt.Substring((space + 1), 1)); //s2 保存第二
个运算数
string s2 = s_txt.Substring(space + 3);
// 将运算数从 string 转成 Double 型 double arg1 = Convert .ToDouble(s1); double arg2 = Convert .ToDouble(s2);
// 根据运算符进行计算 switch (opoperation )
case '+' : d_result = arg1 + arg2; break ; case '-' : d_result = arg1 - arg2;
break;
case '*':
d_result = arg1 * arg2; break;
case 7'
if (arg2 == 0)
II抛出异常
throw new ApplicationException
();
else
d_result = arg1 / arg2;
break; default :
throw new ApplicationException
()
;
//将计算结果显示在文本框中 txt_Result.Text = d_result.ToString();
private void btn_Clear_Click( object sender, EventArgs e)
//将文本清空
txt Result.Text = HH
private void button0_Click( object sender, EventArgs e)
Button btn = ( Button
)sender; txt_Result.Text += btn.Text;
private void but1_Click( object sender, EventArgs e)
Button btn = ( Button )sender;
II空格用来分隔数值和运算符 txt Result.Text = txt Result.Text + ""+ btn.Text +
private void but2_Click( object sender, EventArgs e)
Button btn = ( Button )sender; II空格用来分隔数值和运算符 txt Result.Text = txt Result.Text + ""+ btn.Text +
private void but3_Click( object sender, EventArgs e)
Button btn = ( Button )sender;
II空格用来分隔数值和运算符
txt Result.Text = txt Result.Text + ""+ btn.Text +
private
void button1_Click( object sender, EventArgs e)
精选文库
2
Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button2_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button3_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button4_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button5_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button6_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button7_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button8_Click( object sender, EventArgs Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private
void button9_Click( object
sender,
EventArgs
Button btn = ( Button )sender; txt_Result.Text += btn.Text;
private void lbl_Result_Click( object sender, EventArgs e)
Private
void txt_Result_TextChanged( object sender, EventArgs e)
e)
e)
e)
e)
e)
e)
e)
e)
精选文库
3
{
本文来源:https://www.wddqxz.cn/abc1060c0aa1284ac850ad02de80d4d8d15a01b3.html