110,499
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
double a = 0, b = 0, adds = 0;
bool c = false;
bool cjj = true;
string d;
public Form1()
{
InitializeComponent();
}
private void Form_Load(object sender, EventArgs e)
{
}
private void button9_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "7";
}
private void button10_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "8";
}
private void button11_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "9";
}
private void button5_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "4";
}
private void button6_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "5";
}
private void button7_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "6";
}
private void button1_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "1";
}
private void button2_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "2";
}
private void button3_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "3";
}
private void button14_click(object sender, EventArgs e)
{
if (c)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += 0;
if (d == "/")
{
textBox1.Clear();
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//要实现三个以上数相加必须在按下第二个+符号按钮之前,把前两个数相加,
//2+3=5+3=8
private void button4_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "+";
}
private void button8_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "-";
}
private void button12_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "*";
}
private void button16_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "/";
}
private void button13_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "*2";
}
private void button15_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "sqrt";
}
private void button18_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "log";
}
private void button19_click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "ln";
}
//这个函数是按等于之后才工作的
private void button20_click(object sender, EventArgs e)
{
switch (d)
{
case "+":
a = b + double.Parse(textBox1.Text);//将文本框内容转换成double类型
break;
case "-":
a = b - double.Parse(textBox1.Text);
break;
case "*":
a = b * double.Parse(textBox1.Text);
break;
case "/":
a = b / double.Parse(textBox1.Text);
break;
case "*2":
a = b * double.Parse(textBox1.Text);
break;
case "sqrt":
a = Math.Sqrt(b);
break;
case "log":
a = Math.Log(double.Parse(textBox1.Text), b);
break;
case "ln":
a = Math.Log(b, Math.E);
break;
}
textBox1.Text = a + "";
c = false;
}
private void button17_click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}
//通过字符串自动计算结果
public class Expression
{
object instance;
MethodInfo method;
/// <summary>
/// 表达试运算
/// </summary>
/// <param name="expression">表达试</param>
public Expression(string expression)
{
if (expression.IndexOf("return") < 0) expression = "return " + expression + ";";
string className = "Expression";
string methodName = "Compute";
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = true;
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string.
Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}",
className, methodName, expression));
if (cr.Errors.Count > 0)
{
string msg = "Expression(\"" + expression + "\"): \n";
foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
throw new Exception(msg);
}
instance = cr.CompiledAssembly.CreateInstance(className);
method = instance.GetType().GetMethod(methodName);
}
/// <summary>
/// 处理数据
/// </summary>
/// <param name="x"></param>
/// <returns>返回计算值</returns>
public double Compute(double x)
{
return (double)method.Invoke(instance, new object[] { x });
}
}
调用:
string s="1+2+3";
Expression expression = new Expression(s);
double d = expression.Compute(0);