有点难度的计算器问题
不使用按钮。窗体内有一个手工输入用的TextBox、一个“计算”Button按钮、一个“结果”TextBox、一个显示计算步骤的RichTextBox。需求:
1、可手工在TextBox输入包含括号的加(+)、减(-)、乘(*)、除(/)的四则混合运算。
2、按“计算”Button,在“结果”TextBox中显示结果,小数点精确到后2位,未除尽需四舍五入,而不是舍去。
3、RichTextBox根据程序计算,列出每一项运算的步骤。
这个和传统的计算器不同,就是没有按钮啊,只能在TextBox输入!请大家提供一个思路吧!或者直接上代码,谢谢! --------------------编程问答-------------------- 建议你可以使用逆波兰表达式
http://www.cnblogs.com/rayrain/articles/1530646.html
http://www.codeproject.com/KB/cs/rpn_expressionparser.aspx --------------------编程问答-------------------- 路過。 --------------------编程问答-------------------- 反射啊
--------------------编程问答-------------------- http://topic.csdn.net/u/20110430/02/a3a306f8-2e21-4271-b3c5-35e2018933be.html --------------------编程问答--------------------
我给你写一个吧 --------------------编程问答--------------------
using System;--------------------编程问答-------------------- 有点BUG 稍等,加个程序域 --------------------编程问答--------------------
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 inte易做图ce 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;
}
}
}
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 inte易做图ce 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);
}
}
}
没什么问题了 --------------------编程问答-------------------- 1、首先是对表达式的处理:一种方法是自己手动写,这里注意运算顺序的问题,这个难度不是很大;另一种方法就是调用脚本的计算功能或者表的一个计算的函数都可以轻易实现.
2、得到结果保留小数点应该不难吧
补充:.NET技术 , C#