简易计算器的实现
最近学习了一下c#,编写了一个功能简单的计算器,下面是我的源代码和思路,用以同大家交流。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace calculate
{
public partial class Form1 : Form
{
double dbfrist;//存储{dy}个数字
double dbsec;//存储第二个数字
double result;//存储结果
Char ope;//标示加减乘除
public Form1()
{
InitializeComponent();
}
//清空按钮
private void button22_Click(object sender, EventArgs e)
{
txtResult.Text = "";
}
//输入数字
private void bnt1_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + '1';
}
。。。。。。。。
//加法
private void bntAdd_Click(object sender, EventArgs e)
{
dbfrist = Convert.ToDouble(txtResult.Text);
txtResult.Text ="";
ope = '+';
}
...................................................................
//处理等号
private void bntEqu_Click(object sender, EventArgs e)
{
dbsec = Convert.ToDouble(txtResult.Text);
switch (ope)
{
case '+': result = dbfrist + dbsec; break;
case '-': result = dbfrist - dbsec; break;
case '*': result = dbfrist * dbsec; break;
case '/':
if (dbsec == 0)
{
MessageBox.Show("除数不能为零!");
}
else
result = dbfrist / dbsec; break;
default: break;
}
txtResult.Text = Convert.ToString(result);
}