求助!!在C#中如何计算字符串的值? 希望众高手帮帮忙啦,给点提示也好,谢谢了,我找遍了MSDN都找不到有用的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 反正最后都要用到System.Convert.ToInt32( );哈哈 在字符串中,运算自动进行,结果还是字符串,你可用MessageBox.show(mysr)察看结果。若转换为整数可用System.Convert.ToInt32(string *)a = System.Convert.ToInt32(mysr) 我是用替换的方法来实现的,效率比较差。把括号中的先算好,再用结果替换掉字符串的括号部分。下面是java写的一部分:/** * 进行+/-*运算 * String formula 表达式 * return String . * 12-tan30-89/cos20*65+78*sin30 */ public String SimpleOperation(String formula){ this.equation =new Equation (formula); Vector vOperation=this.equation.getEquationOperators (formula,true); //Vector vNum=this.equation.getEquationOperators (formula,false); Enumeration enum=vOperation.elements (); String oper=""; String formula1=formula; //先处理sin,cos ,tan... while(enum.hasMoreElements ()){ oper=(String)enum.nextElement (); if((this.operatortype .getType (oper)>this.operatortype.DIGIT)&&(this.operatortype .getType (oper)<this.operatortype.PLUS)){ int pos1=formula1.indexOf (oper); int pos2=pos1+oper.length(); char ch; while(pos2<formula1.length ()){ ch=formula1.charAt (pos2);//formula.indexOf ("",pos1+1); if(!Character.isDigit(ch) ){ if((ch=='.')){//sin0.68 pos2++; continue; } if((ch=='-')&& (Character.isLetter (formula1.charAt (pos2-1)))){//sin85--9 pos2++; continue; } break; } pos2++; } pos2--; String strtemp=formula1; strtemp=formula1.substring (pos1,pos2+1); Equation equ=new Equation (strtemp); if(equ.parse()){ strtemp=""+this.Equationcalculate (equ); formula1=this.StringRelace(pos1,pos2,formula1,strtemp); } } }我的程序能运算//$fraction=3//$aaa=100.783516//$bbb=$aaa+200//$formula=$aaa+$bbb+100//$ccc=$aaa//$fraction=5//$p0=103//$p1=108.56//$p2=104.18//$up1=0.01//$formula=$up1*SQRT(POW(((LN$p0-LN$p2)/($p1*(POW((LN$p1-LN$p2),2)))),2)+POW(((LN$p1-LN$p0)/($p2*(POW((LN$p1-LN$p2),2)))),2))输出结果和下面的类似:?------ Results Content ------result:$formula=$aaa+$bbb+100=501.568result:$formula=$up1*SQRT(POW(((LN$p0-LN$p2)/($p1*(POW((LN$p1-LN$p2),2)))),2)+POW(((LN$p1-LN$p0)/($p2*(POW((LN$p1-LN$p2),2)))),2))=0.00303------ Completed! ------ a = System.Convert.ToInt32(mysr)这方法不好用,无法识别的字符,括号与运算符都无法识别啊。 1、方法一,用newman0708(nch)2、方法二,用string str = "select " + "int1 = (1+2+3+4)*5-6+7/8",当做一条sql语句执行,需要连接数据库3、方法三, 用Datatable的select 结合2(没试过,可以试试) 用javascript呵呵,里面有个语句直接可以转化为表达式,恩,我忘记了怎么写不知道对你有否帮助 差不多是个编译器1、词法分析 分析各个元素,包括数字和运算符(正规表达式)2、语法分析 分析是否为有效的表达式 (BNF)3、语义分析 分析表达式的含义,如何运算 (运算符优先级)4、运算 可以用栈实现我写过一个用System.Collection.Stack类计算表达式结果的程序可以输出每一步的演算过程。 为什么我的配置是2.0,却能用3.0的新东西? c#学习交流群 关于打开文件显示文件名和后缀名 高手求救 如何获取gridView 中的dorpDownList 名称(ID)?? 我的电脑配置高不高啊? 用c#寫代碼,如何設置excel的存儲格式呀,例如如把某一列設為文字格式。急急急。 还是C# windows编程下的把数据库文件导出为Access文件的问题 如何判断DataTable执行到第一条记录? 开启多个异步 在DataGrid中放了两个LinkButton,请教怎么触发他们的click事件 100分求FTP终端
System.Convert.ToInt32( );
哈哈
若转换为整数可用System.Convert.ToInt32(string *)
a = System.Convert.ToInt32(mysr)
把括号中的先算好,再用结果替换掉字符串的括号部分。下面是java写的一部分:
/**
* 进行+/-*运算
* String formula 表达式
* return String .
* 12-tan30-89/cos20*65+78*sin30
*/
public String SimpleOperation(String formula){
this.equation =new Equation (formula);
Vector vOperation=this.equation.getEquationOperators (formula,true);
//Vector vNum=this.equation.getEquationOperators (formula,false);
Enumeration enum=vOperation.elements ();
String oper="";
String formula1=formula;
//先处理sin,cos ,tan...
while(enum.hasMoreElements ()){
oper=(String)enum.nextElement ();
if((this.operatortype .getType (oper)>this.operatortype.DIGIT)&&(this.operatortype .getType (oper)<this.operatortype.PLUS)){
int pos1=formula1.indexOf (oper);
int pos2=pos1+oper.length();
char ch;
while(pos2<formula1.length ()){
ch=formula1.charAt (pos2);//formula.indexOf ("",pos1+1);
if(!Character.isDigit(ch) ){
if((ch=='.')){//sin0.68
pos2++;
continue;
}
if((ch=='-')&&
(Character.isLetter (formula1.charAt (pos2-1)))){//sin85--9
pos2++;
continue;
}
break;
}
pos2++;
}
pos2--;
String strtemp=formula1;
strtemp=formula1.substring (pos1,pos2+1);
Equation equ=new Equation (strtemp);
if(equ.parse()){
strtemp=""+this.Equationcalculate (equ);
formula1=this.StringRelace(pos1,pos2,formula1,strtemp);
}
}
}我的程序能运算
//$fraction=3//$aaa=100.783516
//$bbb=$aaa+200
//$formula=$aaa+$bbb+100
//$ccc=$aaa//$fraction=5
//$p0=103
//$p1=108.56
//$p2=104.18
//$up1=0.01
//$formula=$up1*SQRT(POW(((LN$p0-LN$p2)/($p1*(POW((LN$p1-LN$p2),2)))),2)+POW(((LN$p1-LN$p0)/($p2*(POW((LN$p1-LN$p2),2)))),2))输出结果和下面的类似:
?
------ Results Content ------
result:$formula=$aaa+$bbb+100=501.568
result:$formula=$up1*SQRT(POW(((LN$p0-LN$p2)/($p1*(POW((LN$p1-LN$p2),2)))),2)+POW(((LN$p1-LN$p0)/($p2*(POW((LN$p1-LN$p2),2)))),2))=0.00303
------ Completed! ------
2、方法二,用string str = "select " + "int1 = (1+2+3+4)*5-6+7/8",当做一条sql语句执行,需要连接数据库
3、方法三, 用Datatable的select 结合2(没试过,可以试试)
不知道对你有否帮助
1、词法分析 分析各个元素,包括数字和运算符(正规表达式)
2、语法分析 分析是否为有效的表达式 (BNF)
3、语义分析 分析表达式的含义,如何运算 (运算符优先级)
4、运算 可以用栈实现我写过一个用System.Collection.Stack类计算表达式结果的程序
可以输出每一步的演算过程。