不使用按钮。窗体内有一个手工输入用的TextBox、一个“计算”Button按钮、一个“结果”TextBox、一个显示计算步骤的RichTextBox。需求:
1、可手工在TextBox输入包含括号的加(+)、减(-)、乘(*)、除(/)的四则混合运算。
2、按“计算”Button,在“结果”TextBox中显示结果,小数点精确到后2位,未除尽需四舍五入,而不是舍去。
3、RichTextBox根据程序计算,列出每一项运算的步骤。这个和传统的计算器不同,就是没有按钮啊,只能在TextBox输入!请大家提供一个思路吧!或者直接上代码,谢谢!

解决方案 »

  1.   

    建议你可以使用逆波兰表达式
    http://www.cnblogs.com/rayrain/articles/1530646.html
    http://www.codeproject.com/KB/cs/rpn_expressionparser.aspx
      

  2.   

    http://topic.csdn.net/u/20110430/02/a3a306f8-2e21-4271-b3c5-35e2018933be.html
      

  3.   

    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;
    using System.IO;
    using System.Reflection;
    using System.Diagnostics;namespace Caculator
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Caculate ca = new Caculate();
                ca.RegType(textBox1.Text);
                ca.Compile();
                IA a = ca.Create_A();
                textBox2.Text = a.f().ToString() ;
            }
        }
        public interface IA
        {
            int f();
        }    public class Caculate
        {
            public void RegType(string str)  // 动态注册一个类型
            {
                StreamWriter sw = new StreamWriter("a.cs");
                sw.WriteLine("public class A : Caculator.IA { public int f() { try { return " + str + "; } catch { return int.MinValue; } } } ");
                sw.Close();
            }       // C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
           // C:\Windows\System32\cmd.exe
            //在C盘根目录下 out.txt查看是否编译成功        public void Compile()//编译 下面的字符串写你的CMD 和 编译器路径 
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = @"C:\Windows\System32\cmd.exe";
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                info.Arguments = @"/c C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe  /t:libirary " + path + @"\a.cs  /r:"+ Assembly.GetExecutingAssembly().Location+@"  >C:\out.txt";
                info.WindowStyle = ProcessWindowStyle.Hidden;
                Process p = Process.Start(info);
                p.WaitForExit();
            }
            public IA Create_A()//创建A的对象
            {
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                Assembly asm = Assembly.LoadFrom(path + @"\a.dll");
                IA a = asm.CreateInstance("A") as IA;
                return a;
            }
        }
    }
      

  4.   

    有点BUG 稍等,加个程序域
      

  5.   

    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;
    using System.IO;
    using System.Reflection;
    using System.Diagnostics;namespace Caculator
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Caculate ca = new Caculate();
                ca.RegType(textBox1.Text);
                ca.Compile();
                IA a = ca.Create_A();
                textBox2.Text = a.f().ToString() ;
                ca.UnloadAdm();
            }
        }
        public interface IA
        {
            int f();
        }    public class Caculate:MarshalByRefObject
        {
            AppDomain adm;  
            public void RegType(string str)  // 动态注册一个类型
            {
                StreamWriter sw = new StreamWriter("a.cs");
                sw.WriteLine("public class A :System.MarshalByRefObject, Caculator.IA { public int f() { try { return " + str + "; } catch { return int.MinValue; } } } ");
                sw.Close();
            }       // C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
           // C:\Windows\System32\cmd.exe
            //在C盘根目录下 out.txt查看是否编译成功        public void Compile()//编译 下面的字符串写你的CMD 和 编译器路径 
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = @"C:\Windows\System32\cmd.exe";
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                info.Arguments = @"/c C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe  /t:library " + path + @"\a.cs  /r:"+ Assembly.GetExecutingAssembly().Location+@"  >E:\out.txt";
                info.WindowStyle = ProcessWindowStyle.Hidden;
                Process p = Process.Start(info);
                p.WaitForExit();
            }
            public IA Create_A()//创建A的对象
            {
                adm = AppDomain.CreateDomain("abc", null, new AppDomainSetup());
                return adm.CreateInstanceFromAndUnwrap("a.dll", "A") as IA;
                
                //string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                //Assembly asm = Assembly.LoadFrom(path + @"\a.dll");
                //IA a = asm.CreateInstance("A") as IA;
                //return a;
            }
            public void UnloadAdm()
            {
                AppDomain.Unload(adm);
     
            }
        }
    }没什么问题了