using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calcu_Pro
{
public partial class Calcu_form : Form
{
public Calcu_form()
{
InitializeComponent();
}
//运算数A
private string dblAcc;
//运算数B
private string dblSec;
//通过获取strOper的值来决定运算+,-,*,/,=
private string strOper;
// 这个成员变量用于标识是否进行过“=”运算,默认true为没有进行过此运算
private bool equal_flag = true;
//中间变量
private string num = "";
//获得结果button
private void btn_equel_Click(object sender, EventArgs e)
{
if (equal_flag)
{
if ((!dblAcc.Equals("")) && (!dblSec.Equals("")) && (!strOper.Equals("")))
{
txtResult.Text = "";
dblAcc = calculte(dblAcc, dblSec, strOper);
txtResult.Text = dblAcc;
// 点击过“=”后,标志位就变为false
equal_flag = false;
}
else
{
MessageBox.Show("您的输入有误!", "提示");
}
}
else
{
dblAcc = calculte(dblAcc, dblSec, strOper);
txtResult.Text = dblAcc;
equal_flag = false;
}
}
//除法button
private void btn_division_Click(object sender, EventArgs e)
{
num = "";
//strOper = "/";
operator_check(btn_division);
}
//乘法button
private void btn_multiply_Click(object sender, EventArgs e)
{
num = "";
//strOper = "*";
operator_check(btn_multiply);
}
//减法button
private void btn_subtract_Click(object sender, EventArgs e)
{
num = "";
//strOper = "-";
operator_check(btn_subtract);
}
//加法button
private void btn_add_Click(object sender, EventArgs e)
{
num = "";
//strOper = "+";
operator_check(btn_add);
}
//数字1
private void btn_one_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_one);
}
else
{
numberInputCheck(btn_one);
equal_flag = true;
}
}
//数字2
private void btn_two_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_two);
}
else
{
numberInputCheck(btn_two);
equal_flag = true;
}
}
//数字3
private void btn_three_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_three);
}
else
{
numberInputCheck(btn_three);
equal_flag = true;
}
}
//数字4
private void btn_four_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_four);
}
else
{
numberInputCheck(btn_four);
equal_flag = true;
}
}
//数字5
private void btn_five_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_five);
}
else
{
numberInputCheck(btn_five);
equal_flag = true;
}
}
//数字6
private void btn_six_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_six);
}
else
{
numberInputCheck(btn_six);
equal_flag = true;
}
}
//数字7
private void btn_seven_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_seven);
}
else
{
numberInputCheck(btn_seven);
equal_flag = true;
}
}
//数字8
private void btn_eight_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_eight);
}
else
{
numberInputCheck(btn_eight);
equal_flag = true;
}
}
//数字9
private void btn_nine_Click(object sender, EventArgs e)
{
if (equal_flag)
{
numberInputCheck(btn_nine);
}
else
{
numberInputCheck(btn_nine);
equal_flag = true;
}
}
//数字0
private void btn_zero_Click(object sender, EventArgs e)
{
// 没有点击过等号
if (equal_flag)
{
numberInputCheck(btn_zero);
}
else
{
numberInputCheck(btn_zero);
equal_flag = true;
}
}
private void button8_Click(object sender, EventArgs e)
{
txtResult.Text = "0.0";
}
private void numberInputCheck(Button button)
{
// 如果输入框中是“0.0”或者“0”,说明这个数字是第一个操作数
if (txtResult.Text.Equals("0.0") || txtResult.Text.Equals("0"))
{
txtResult.Text = button.Text;
dblAcc =txtResult.Text;
}
// 如果输入框中不是上面的情况、长度不为0并且操作符已经输入,此时应该做的是第一个操作数拼接
else if ((strOper == null) && (txtResult.Text.Length != 0) && (!txtResult.Text.Equals("0.0")) && (!txtResult.Text.Equals("0")))
{
txtResult.Text += button.Text;
dblAcc =txtResult.Text;
}
// 如果输入框中不是上面的情况、长度不为0并且操作符已经输入,此时应该做的是第二个操作数拼接
else if ((strOper!=null) && (txtResult.Text.Length != 0) && (!txtResult.Text.Equals("0.0")) && (!txtResult.Text.Equals("0")))
{
txtResult.Text = "";
num += button.Text;
txtResult.Text += num;
dblSec = txtResult.Text;
}
}
private void operator_check(Button button)
{
// 第二个操作数还没有输入,说明这是用户这个运算中的第一个操作符
if ((dblAcc != null) && (dblSec==null))
{
strOper = button.Text;
}
// 两个操作数均不为空,说明这是第二个操作符了,这时应该将前两个运算数进行运算
else if ((dblAcc!=null) && (dblSec!=null) && (strOper!=null) && equal_flag)
{
txtResult.Text = "";
//当两个数都不为空的时候
if (!dblAcc.Equals("") && !dblSec.Equals(""))
{
dblAcc = calculte(dblAcc, dblSec, strOper);
}
txtResult.Text = dblAcc;
num = "";
dblSec = "";
}
// 完成前一个运算后直接点击运算符,此时应该将上一次的结果作为第二次运算的第一个操作数
else if (!equal_flag)
{
strOper = button.Text;
dblAcc =txtResult.Text;
num = "";
}
// 直接点击操作符时提示出错!
else if (dblAcc != null)
{
MessageBox.Show("请输入您要进行运算的数字!", "提示");
}
else
{
MessageBox.Show("wrong");
}
//前一个运算符
strOper = button.Text;
}
private string calculte(string dblAcc, string dblSec, string strOper)
{
switch (strOper)
{
case "+":
dblAcc = (double.Parse(dblAcc) + double.Parse(dblSec)).ToString();
return dblAcc;
case "-":
dblAcc = (double.Parse(dblAcc) - double.Parse(dblSec)).ToString();
return dblAcc;
case "*":
dblAcc = (double.Parse(dblAcc) * double.Parse(dblSec)).ToString();
return dblAcc;
case "/":
dblAcc = (double.Parse(dblAcc) / double.Parse(dblSec)).ToString();
return dblAcc;
default:
MessageBox.Show("您的操作有误!", "提示");
return "wrong";
}
}
}
}
C# 小计算器
最新推荐文章于 2021-04-22 14:02:22 发布