"22+44+8"类似这样的字符串如何能够直接换算成和?

slb00814 2009-12-24 12:09:05
c# 语言 别和我说拆分 循环。。。。
...全文
734 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxiaohui8709253 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 liherun 的回复:]
C# code
private string size(string str)
{
string bz = "";
str = (((((str.Replace(" ", "")).Replace("{","(")).Replace("[","(")).Replace("}",")")).Repla……
[/Quote]
厉害
Peter200694013 2010-03-23
  • 打赏
  • 举报
回复
int result = Convert.ToInt32(new DataTable().Compute("22+44+8",""));
cdglynn 2010-03-23
  • 打赏
  • 举报
回复
一个字符一个字符的读取,然后解析出整数来,再做和运算
yao2004jessica 2010-03-23
  • 打赏
  • 举报
回复
很奇怪为什么不能用system.data
spmzfz 2010-03-23
  • 打赏
  • 举报
回复
System.Data.DataTable dt = new DataTable();
Console.WriteLine( dt.Compute("33+7+5","+").ToString());
mowensky 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zgke 的回复:]
this.Text=new DataTable().Compute("22+44+8", "").ToString();
[/Quote]

学习了
林三一 2009-12-28
  • 打赏
  • 举报
回复
ding
sohighthesky 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 sdfkfkd 的回复:]
还有一个方法,不知道是引用什么东西,使用JAVASCRIPT脚本的EVAL方法计算表达式的值
[/Quote]
右键添加引用 Microsoft.JScript

lz不会还是不能添加吧
卧_槽 2009-12-28
  • 打赏
  • 举报
回复

try
{
string strOpSeparators = @"(\()|(\))|(\+)|(\-)|(\*)|(\/)|(#)";
string[,] strLev = new string[,] { { "(", "1" }, { "+", "2" }, { "-", "2" }, { "*", "3" }, { "/", "3" } };

string strInputOp = this.tbxResult.Text.Trim() + "#";
Regex reOp = new Regex(strOpSeparators);
string[] strSplitOp = reOp.Split(strInputOp);

//判断数字(这里只能判断整数和浮点数)
string strNumber = @"^-?[1-9]\d*$|^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$";
Regex reNumber = new Regex(strNumber);
//判断运算符和一些括号之类的
string strSymbol = @"\(|\)|\+|\-|\*|\/|#";
Regex reSymbol = new Regex(strSymbol);
Stack<string> stJudge = new Stack<string>();
Queue<string> quValue = new Queue<string>();
int intTest = 1;
test:
if (strSplitOp[strSplitOp.Length - intTest] == "")
{
intTest++;
goto test;

}
if (strSplitOp[strSplitOp.Length - intTest] != "#")
{

MessageBox.Show("#");
return;
}
#region 转换
foreach (string s in strSplitOp)//不能处理负数
{
if (s != "" && s != null)
{
if (reNumber.IsMatch(s))//如果是数字直接入队列
{
if (quValue.Count == 0)
{

quValue.Enqueue(s);
}
else
{
string strTest = "";
foreach (string strValueQue in quValue)
{
strTest = strValueQue;
}
if (reNumber.IsMatch(strTest))
{
quValue.Enqueue("&");//在两个连在一起的数字用&分隔
}
quValue.Enqueue(s);
}
}
else
{
if (reSymbol.IsMatch(s))//这里是运算符之类的
{
if (stJudge.Count == 0)//如果是第一个运算符之类的就直接入栈
{
stJudge.Push(s);
}
else
{
if (s == "#")//这里表示转化成逆波兰式已经结束了
{
foreach (string judge in stJudge)
{
quValue.Enqueue(judge);
}
}
else
{
if (s.Equals(")"))
{
PV:
string PopValue = stJudge.Pop();
if (PopValue != "(")
{

quValue.Enqueue(PopValue);
goto PV;
}
else
{
if (PopValue.Equals("("))
{
//什么都不做
}
}
}
else
{
if (s == "(")
{
stJudge.Push(s);
}
else
{
POP:
string strPop = stJudge.Pop();
int vOld = 0;
int vNew = 0;
for (int j = 0; j < strLev.GetLength(0); j++)
{
if (strPop == strLev[j, 0])
{
vOld = Convert.ToInt32(strLev[j, 1]);
}
if (s == strLev[j, 0])
{
vNew = Convert.ToInt32(strLev[j, 1]);
}
}
if (vNew > vOld)//如果新操作符的优先级大的话就进入栈
{
stJudge.Push(strPop);
stJudge.Push(s);
}
else
{


quValue.Enqueue(strPop);//把旧操作符(优先级低)放入队列

if (stJudge.Count > 0)
{
goto POP;
}
else
{
stJudge.Push(s);
}
}
}
}
}
}

}
else
{

MessageBox.Show("标准输入格式,如(10+20)*2");
return;
}
}
}
}
#endregion
//运算结果
Stack<string> stResult = new Stack<string>();
string strRexResult = @"\+|\-|\*|\/";
Regex reResult = new Regex(strRexResult);
foreach (string result in quValue)
{
if (result != "&")
{
if (reResult.IsMatch(result))
{
double dblSencond = Convert.ToDouble(stResult.Pop());
double dblFirst = Convert.ToDouble(stResult.Pop());
double dblResult = 0.0;
switch (result)
{
case "+":
dblResult = dblFirst + dblSencond;
break;
case "-":
dblResult = dblFirst - dblSencond;
break;
case "*":
dblResult = dblFirst * dblSencond;
break;
case "/":
dblResult = dblFirst / dblSencond;
break;
}
stResult.Push(dblResult.ToString());
}
else
{
stResult.Push(result);
}
}
}
foreach (string dblValue in stResult)
{

this.tbxResult.Text = dblValue;
}
}
catch (Exception)
{
MessageBox.Show("输入表达式格式不正确,暂不支持负数的运算!");
}
卧_槽 2009-12-28
  • 打赏
  • 举报
回复
原来你是要逆波兰式计算器的源码。
SlaughtChen 2009-12-28
  • 打赏
  • 举报
回复
糊涂了
nashina 2009-12-28
  • 打赏
  • 举报
回复
要求还真多,用拆分循环,这些基本的操作;
什么没有程序集的限制都无所谓啦
ChrisAK 2009-12-28
  • 打赏
  • 举报
回复
转成逆波兰表达式后求值.
ncjcz 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 slb00814 的回复:]
c#  语言  别和我说拆分  循环。。。。
[/Quote]

为什么不用拆分 循环。。。。?
楼上说的那些方法没有全看,不过应该都是用拆分 循环实现的
特别 2009-12-28
  • 打赏
  • 举报
回复
还有一个方法,不知道是引用什么东西,使用JAVASCRIPT脚本的EVAL方法计算表达式的值
qi19983593 2009-12-28
  • 打赏
  • 举报
回复
看看这个能不能对你有帮助
qi19983593 2009-12-28
  • 打赏
  • 举报
回复
http://www.52kxbc.com/article-14-19648.aspx
huwei001982 2009-12-28
  • 打赏
  • 举报
回复
http://www.cnblogs.com/michaelhuwei/archive/2007/12/29/1019658.html

这里倒是有一个简单的
slb00814 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lxcnn 的回复:]
引用 3 楼 slb00814 的回复:
方法是可以  但我们用的环境未引用 System.Data(原因不能说)。

汗,那就写全吧,又不是非要加引用

C# coderichTextBox2.Text=new System.Data.DataTable().Compute("22+44+8","").ToString();
[/Quote]
说白了 就是没有 System.Data的程序集引用
Dobzhansky 2009-12-24
  • 打赏
  • 举报
回复
今天要求算加法, 明天就可能要乘法,
接下来就可能会调用函数( sin 之类的)


所以, 找个库来吧,

muParser , C# 可用.
加载更多回复(17)

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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