主题:一个C#问题
smilecrane
[专家分:0] 发布于 2010-09-09 19:22:00
我现在想在textbox里输入a+b=c这样的式子,这里包括加减乘除,那在textbox里都是string类型的对象,我要如何可以做到让它自己识别“+,-,×,/”这些符号,然后判断等式是否正确,在一个label里显示true,false,我应该怎么做,是不是用到运算符重载呢?
回复列表 (共3个回复)
沙发
coolskyboy [专家分:0] 发布于 2010-09-14 23:22:00
不知道学过数据结构没?可以使用堆栈来处理这个问题。
板凳
咱是菜鸟 [专家分:20] 发布于 2010-12-10 09:54:00
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class 判断 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string aa = this.TextBox1.Text;
int bb=0,cc=0,gg=0,ff=0,hh=0;
for (int i = 0; i <= aa.Length - 1; i++)
{
if (aa.Substring(i, 1) == "+")
{
bb = i;
}
if (aa.Substring(i, 1) == "-")
{
gg = i;
}
if (aa.Substring(i, 1) == "*")
{
ff = i;
}
if (aa.Substring(i, 1) == "/")
{
hh = i;
}
if (aa.Substring(i, 1) == "=")
{
cc = i;
}
}
if (bb != 0)
{
int dd = int.Parse(aa.Substring(0, bb));
int ee = int.Parse(aa.Substring(bb + 1, cc - bb - 1));
int kk = int.Parse(aa.Substring(cc + 1, aa.Length - 1 - cc));
if (dd + ee == kk)
this.Label1.Text = "正确";
else
this.Label1.Text = "错误";
}
if (gg != 0)
{
int dd = int.Parse(aa.Substring(0, gg));
int ee = int.Parse(aa.Substring(gg + 1, cc - gg - 1));
int kk = int.Parse(aa.Substring(cc + 1, aa.Length - 1 - cc));
if (dd - ee == kk)
this.Label1.Text = "正确";
else
this.Label1.Text = "错误";
}
if (ff != 0)
{
int dd = int.Parse(aa.Substring(0, ff));
int ee = int.Parse(aa.Substring(ff + 1, cc - ff - 1));
int kk = int.Parse(aa.Substring(cc + 1, aa.Length - 1 - cc));
if (dd * ee == kk)
this.Label1.Text = "正确";
else
this.Label1.Text = "错误";
}
if (hh != 0)
{
int dd = int.Parse(aa.Substring(0, hh));
int ee = int.Parse(aa.Substring(hh + 1, cc - hh - 1));
int kk = int.Parse(aa.Substring(cc + 1, aa.Length - 1 - cc));
if (dd / ee == kk)
this.Label1.Text = "正确";
else
this.Label1.Text = "错误";
}
}
}
3 楼
wyk335858575 [专家分:30] 发布于 2010-12-10 15:28:00
label设个属性就能显示
————————————————————————————
[url=http://www.wjlweixiu88.com/]万家乐热水器维修[/url]
我来回复