bool ClearFlag = false;
double M = 0;
///
/// 0-9,+,-,*,/,求余%
///
///
///
private void buttonInput_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string input = tb_Result.Text.Replace("\r\n0", "");
string curBtn = btn.Text;
if (ClearFlag)
{ input = ""; ClearFlag = false; }
if (input == "" && curBtn == "0")
return;
if (btn.Text == "+" || btn.Text == "-" || btn.Text == "*" || btn.Text == "/" || btn.Text == "%")
{
if (input == "")
input = "0";
if (input.Contains("+") || input.Contains("-") || input.Contains("*") || input.Contains("/") || input.Contains("%"))
{
input = Calculate(input);
}
tb_Result.Text = input + " " + btn.Text + " " + "\r\n";
return;
}
else tb_Result.Text = input + btn.Text;
}
string Calculate(string formula)
{
ClearFlag = true;
formula = formula.Replace("\r\n", "");
string result = "";
double d_result;
string s_txt = formula;
int space = s_txt.IndexOf(' ');
if (space == -1) return formula;
string s1 = s_txt.Substring(0, space);
char operation = Convert.ToChar(s_txt.Substring((space + 1), 1));
string s2 = s_txt.Substring(space + 3);
double arg1 = Convert.ToDouble(s1);
if (operation == '√' || operation == 'x' )
s2 = "0";
if (s2 == "") return "\r\n0";
double arg2 = Convert.ToDouble(s2);
switch (operation)
{
case '+':
d_result = arg1 + arg2;
break;
case '-':
d_result = arg1 - arg2;
break;
case '*':
d_result = arg1 * arg2;
break;
case '/':
if (arg2 == 0)
{
throw new ApplicationException();
}
else
{
d_result = arg1 / arg2;
}
break;
case '√': d_result = Math.Pow(arg1, 0.5);
break;
case 'x': d_result = Math.Pow(arg1, 2);
break;
case '%': d_result = arg1 % arg2;
break;
default:
throw new ApplicationException();
}
result = d_result.ToString();
return result;
}
///
/// CE,C
///
///
///
private void buttonClear_Click(object sender, EventArgs e)
{
tb_Result.Text = "\r\n0";
}
///
/// =
///
///
///
private void buttonEqual_Click(object sender, EventArgs e)
{
string input = tb_Result.Text.Replace("\r\n0", "");
tb_Result.Text = "\r\n" + Calculate(input);
}
///
/// 后退
///
///
///
private void buttonBackSpace_Click(object sender, EventArgs e)
{
int length = tb_Result.Text.Length;
if (length > 0)
tb_Result.Text = tb_Result.Text.Substring(0, length - 1);
}
///
/// 根号,平方
///
///
///
private void buttonOperation_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string input = tb_Result.Text.Replace("\r\n0", "");
string curBtn = btn.Text;
input = input + " " + btn.Text + " " + "\r\n";
tb_Result.Text = "\r\n" + Calculate(input);
}
private void buttonMs_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string input = tb_Result.Text.Replace("\r\n0", "");
string curBtn = btn.Text.Replace("M","");
char operation = Convert.ToChar(curBtn.Substring(0, 1));
double arg =0, d_result=0;
try
{
arg = Convert.ToDouble(input);
}
catch { return; }
switch (operation)
{
case '+':
d_result = M + arg;
break;
case '-':
d_result = M - arg;
break;
case 'S':
M = arg;
return;
case 'R':
d_result = M;
break;
case 'C':
M =0;
return;
default:
return;
}
tb_Result.Text = "\r\n" + d_result;
}
50块帮写。