c# 一个textbox 中输入表达式 点击按钮列外一个textbox 得出结果
简单四则运算就好如textbox1中输入111+222点击button后textbox2中显示333...
简单四则运算就好 如textbox1中输入111+222 点击button后textbox2中显示333
展开
5个回答
展开全部
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请从新输入表达式");
textBox1.Text = "";
textBox1.Focus();
}
else
{
string s = textBox1.Text;
DataTable dt = new DataTable();
DataColumn col = new DataColumn("col1", typeof(string), s);
dt.Columns.Add(col);
dt.Rows.Add(new object[] { "" });
string val = Convert.ToString(dt.Rows[0][0]);
MessageBox.Show(textBox1.Text + "=" + val);
textBox1.Text = "";
}
}
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请从新输入表达式");
textBox1.Text = "";
textBox1.Focus();
}
else
{
string s = textBox1.Text;
DataTable dt = new DataTable();
DataColumn col = new DataColumn("col1", typeof(string), s);
dt.Columns.Add(col);
dt.Rows.Add(new object[] { "" });
string val = Convert.ToString(dt.Rows[0][0]);
MessageBox.Show(textBox1.Text + "=" + val);
textBox1.Text = "";
}
}
展开全部
说一个思路吧..
你在button的事件里先得到textbox1.text
string str=textbox1.Text;
然后再去用一个值来判断str中是否存在+或-或*或/
如果存在..把str分成三部分 第一部分就是111 第二部分是+ 第三部分是333
最后一步..如果是+把那个数相加,其它的类似..
你在button的事件里先得到textbox1.text
string str=textbox1.Text;
然后再去用一个值来判断str中是否存在+或-或*或/
如果存在..把str分成三部分 第一部分就是111 第二部分是+ 第三部分是333
最后一步..如果是+把那个数相加,其它的类似..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是asp,这个跟我的一个问题很相像~
但在C#就不太清楚了。
【asp+数据库】如何把字符"1+2" 让它等于3?
http://zhidao.baidu.com/question/73048087.html
<%
Set x = CreateObject("MSScriptControl.ScriptControl")
x.Language = "VBScript"
a = 1
b = 2
c = 3
d = 4
gongshi = Replace("a+b", "a", a) '变量自己替换了哈,相信你会的
gongshi = Replace(gongshi, "b", b)
result = x.Eval(gongshi)
response.write gongshi & "=" & result
%>
但在C#就不太清楚了。
【asp+数据库】如何把字符"1+2" 让它等于3?
http://zhidao.baidu.com/question/73048087.html
<%
Set x = CreateObject("MSScriptControl.ScriptControl")
x.Language = "VBScript"
a = 1
b = 2
c = 3
d = 4
gongshi = Replace("a+b", "a", a) '变量自己替换了哈,相信你会的
gongshi = Replace(gongshi, "b", b)
result = x.Eval(gongshi)
response.write gongshi & "=" & result
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace size
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string splitChar = "+-*/";
string[] strArray = this.textBox1.Text.Split(splitChar.ToCharArray());
int len=strArray[0].Length;
char[] chArray=this.textBox1.Text.ToCharArray();
char ch=chArray[strArray[0].Length];
double x = Int32.Parse(strArray[0]);
double y = Int32.Parse(strArray[1]);
double res = 0.0;
switch (ch)
{
case '+':
res=x + y;
this.textBox2.Text = res.ToString();
break;
case '-':
res = x - y;
this.textBox2.Text = res.ToString();
break;
case '*':
res = x * y;
this.textBox2.Text = res.ToString();
break;
case '/':
res = x / y;
this.textBox2.Text = res.ToString();
break;
default:
throw new Exception();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace size
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string splitChar = "+-*/";
string[] strArray = this.textBox1.Text.Split(splitChar.ToCharArray());
int len=strArray[0].Length;
char[] chArray=this.textBox1.Text.ToCharArray();
char ch=chArray[strArray[0].Length];
double x = Int32.Parse(strArray[0]);
double y = Int32.Parse(strArray[1]);
double res = 0.0;
switch (ch)
{
case '+':
res=x + y;
this.textBox2.Text = res.ToString();
break;
case '-':
res = x - y;
this.textBox2.Text = res.ToString();
break;
case '*':
res = x * y;
this.textBox2.Text = res.ToString();
break;
case '/':
res = x / y;
this.textBox2.Text = res.ToString();
break;
default:
throw new Exception();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private void button1_Click(object sender, EventArgs e)
{
try
{
string splitChar = "+-*/";
string[] strArray = this.textBox1.Text.Split(splitChar.ToCharArray());
int len=strArray[0].Length;
char[] chArray=this.textBox1.Text.ToCharArray();
char ch=chArray[strArray[0].Length];
double x = Int32.Parse(strArray[0]);
double y = Int32.Parse(strArray[1]);
double res = 0.0;
switch (ch)
{
case '+':
res=x + y;
this.textBox2.Text = res.ToString();
break;
case '-':
res = x - y;
this.textBox2.Text = res.ToString();
break;
case '*':
res = x * y;
this.textBox2.Text = res.ToString();
break;
case '/':
res = x / y;
this.textBox2.Text = res.ToString();
break;
default:
throw new Exception();
}
{
try
{
string splitChar = "+-*/";
string[] strArray = this.textBox1.Text.Split(splitChar.ToCharArray());
int len=strArray[0].Length;
char[] chArray=this.textBox1.Text.ToCharArray();
char ch=chArray[strArray[0].Length];
double x = Int32.Parse(strArray[0]);
double y = Int32.Parse(strArray[1]);
double res = 0.0;
switch (ch)
{
case '+':
res=x + y;
this.textBox2.Text = res.ToString();
break;
case '-':
res = x - y;
this.textBox2.Text = res.ToString();
break;
case '*':
res = x * y;
this.textBox2.Text = res.ToString();
break;
case '/':
res = x / y;
this.textBox2.Text = res.ToString();
break;
default:
throw new Exception();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询