110,545
社区成员
发帖
与我相关
我的任务
分享
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 crta
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double nPrevValue = 0;//数据1
private bool bAppend = true;//数字按钮判断标识符
private string strPrevOpar = "";//运算符标识符
public double M,m;//记忆标识符
public string a;//小数点标识符
private void NumButton_Click(object sender, EventArgs e)//数字按钮输入
{
if (bAppend)
{
Button btn = sender as Button;
if (btn.Text == "0")
{
if (textBoxResult.Text == "")
{
textBoxResult.Text += "0";
}
else if (textBoxResult.Text == "0")
{
textBoxResult.Text +=btn.Text ;
}
else
{
textBoxResult.Text += btn.Text;
}
}
else
{
if (textBoxResult.Text == "0")
{
textBoxResult.Text = "";
}
textBoxResult.Text += btn.Text;
}
}
else
{
textBoxResult.Text = "";
Button btn = sender as Button;
textBoxResult.Text += btn.Text;
bAppend = true;
}
}
private void OperButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
double result;//结果
if (btn.Text!="="&&btn.Text!="CE"&&btn.Text!="MS"&&btn.Text!="MR"&&btn.Text!="MC"&&btn.Text!="M+"&&btn.Text!="BackSpace"&&btn.Text!="."&&btn.Text!="sqrt"&&btn.Text!="1/x"&&btn.Text!="x^2"&&btn.Text!="+/-")//判断不为如下字符的情况
{
strPrevOpar = btn.Text;
try
{
nPrevValue = Convert.ToDouble(textBoxResult.Text);
}
catch (System.Exception )
{
nPrevValue = 0;
}
bAppend = false;
}
else if (btn.Text=="CE")//运算符为“CE”的情况
{
nPrevValue = 0;
bAppend = true;
strPrevOpar = "";
textBoxResult.Text = "0";
}
else if(btn.Text=="MS")//运算符为“MS”的情况
{
bAppend = true;
if (textBoxResult.Text == "0.")
{
M = 0;
lab_m.Text = null;
}
else
{
if (textBoxResult.Text == "0")
{
M = 0;
lab_m.Text = null;
}
else
{
if (M != 0)
{
M = 0;
M += Convert.ToDouble(textBoxResult.Text);
}
else if (M == 0)
{
lab_m.Text= "M";
M += Convert.ToDouble(textBoxResult.Text);
}
}
}
}
else if(btn.Text=="MR")//运算符为“MR”的情况
{
textBoxResult.Text = null;
textBoxResult.Text = Convert.ToString(M);
}
else if (btn.Text == "MC")//运算符为“MC”的情况
{
lab_m.Text = null;
M = 0;
m = 0;
}
else if (btn.Text == "M+")//运算符为“M+”的情况
{
m += Convert.ToDouble(textBoxResult.Text);
M += m;
m = 0;
}
else if (btn.Text == "BackSpace")//运算符为“backspace”的情况
{
if (textBoxResult.Text == null)
{
MessageBox.Show("it`s Empty!");
return;
}
else
{
if (textBoxResult.Text.Length != 0)
{
textBoxResult.Text = textBoxResult.Text.Remove(textBoxResult.Text.Length - 1, 1).ToString();
}
else
{
MessageBox.Show("left a little!");
textBoxResult.Text = "0";
}
}
}
else if(btn.Text==".")//运算符为"."的情况
{
if (a != null)
{
if (a != ".")
{
a += btn_piont.Text;
textBoxResult.Text += btn_piont.Text;
}
else
{ } //防止开始点多输入了的错误
}
else
{
textBoxResult.Text += btn_piont.Text;
a += btn_piont.Text;
}
}
else if (btn.Text == "sqrt")//运算符为“sqrt”的情况
{
textBoxResult.Text = Math.Sqrt(Convert.ToDouble(textBoxResult.Text)).ToString();
}
else if (btn.Text == "1/x")//运算符为“1/x”的情况
{
if (textBoxResult.Text != "0")
{
textBoxResult.Text =(1/ Convert.ToDouble(textBoxResult.Text)).ToString();
}
else
{
MessageBox.Show("除数不能为零");
}
}
else if (btn.Text == "x^2")//运算符为“x^2”的情况
{
textBoxResult.Text = Math.Pow(Convert.ToDouble(textBoxResult.Text), 2).ToString();
}
else if (btn.Text == "+/-")//运算符为“+/-”的情况
{
textBoxResult.Text = (Convert.ToDouble(textBoxResult.Text) * (-1)).ToString();
}
else
{
double current = 0;//数据2
try
{
current = Convert.ToDouble(textBoxResult.Text);
}
catch (System.Exception)
{
current = 0;
}
switch (strPrevOpar)
{
case "+"://运算符为“+”的情况
result = current + nPrevValue;
break;
case "-"://运算符为“-”的情况
result = current + nPrevValue;
break;
case "*"://运算符为“*”的情况
result = current * nPrevValue;
break;
case "/"://运算符为“/”的情况
result = nPrevValue / current;
break;
default:
result = current;
break;
}
textBoxResult.Text = result.ToString();
nPrevValue = 0;
bAppend = false;
strPrevOpar = "";
}
}
}
}