关于字符串的问题!简单

nishengqiang04 2005-03-25 06:16:28
比如:(12+14)*3=78 在文本框里面怎样判断这个字符串的结果是对是错!
...全文
132 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nishengqiang04 2005-03-25
  • 打赏
  • 举报
回复
谢谢!
LoveCherry 2005-03-25
  • 打赏
  • 举报
回复
上面这个代码是按照你的要求写的你可以运行一下
LoveCherry 2005-03-25
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Text;

namespace WindowsApplication12
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 48);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string [] s=this.textBox1.Text.Split('=');
if(Calc(s[0]).ToString()==s[1])
this.label1.Text="正确";
else
this.label1.Text="不正确";
}

public static object Calc(string expression)
{
string className = "Calc";
string methodName = "Run";
expression=expression.Replace("/","*1.0/");

// 创建编译器实例。
ICodeCompiler complier = (new CSharpCodeProvider().CreateCompiler());
// 设置编译参数。
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false;
paras.GenerateInMemory = true;

// 创建动态代码。
StringBuilder classSource = new StringBuilder();
classSource.Append("public class "+ className +"\n");
classSource.Append("{\n");
classSource.Append(" public object " + methodName + "()\n");
classSource.Append(" {\n");
classSource.Append(" return "+ expression + ";\n");
classSource.Append(" }\n");
classSource.Append("}");

//System.Diagnostics.Debug.WriteLine(classSource.ToString());

// 编译代码。
CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString());

// 获取编译后的程序集。
Assembly assembly = result.CompiledAssembly;

// 动态调用方法。
object eval = assembly.CreateInstance(className);
MethodInfo method = eval.GetType().GetMethod(methodName);
object reobj = method.Invoke(eval, null);
GC.Collect();
return reobj;

}
}
}
zealic 2005-03-25
  • 打赏
  • 举报
回复
可以用 CodeDOM
动态编译这个表达式并得出结果并和自己写的代码对比
这样就能判断了
hackate 2005-03-25
  • 打赏
  • 举报
回复
你就判断昨天=前面的数据在外面运算的结果跟后面的一样吗?就这样,呵呵
nishengqiang04 2005-03-25
  • 打赏
  • 举报
回复
是的

lovebanyi 2005-03-25
  • 打赏
  • 举报
回复
:(12+14)*3=78 这一例都是在文本框里的吗..
如果是这样的话.要判断.就得进行所谓的..词法分析.语法分析.类似编译的过程.
才知道对不对啊.

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧