• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一个简单的表达式求值类,java

servlet/jsp 搞代码 7年前 (2018-06-18) 160次浏览 已收录 0个评论

import java.util.Date;
public class testcalc2
{
String a;
int len_of_str;
int err; //err 用于发现哪个字符是出错字符就是第一个出错字符串的下标,初始值为-1,表示无错

http://www.gaodaima.com/41079.html一个简单的表达式求值类,java

int index;

public testcalc2(String str)
{
a = str + "+";
len_of_str = a.length();
err = -1;
index = 0;
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%% " + a + len_of_str);

}

public double getnextnum()
{
int preindex = index, countofdot = 0;
if(a.charAt(index) == ‘-‘)
index++;
if(!Character.isDigit(a.charAt(index)))
{
err = index;
return 0;
}
while((index < len_of_str) &&
(Character.isDigit(a.charAt(index)) || a.charAt(index) == ‘.’))
{
if(a.charAt(index) == ‘.’)
{
countofdot++;
}
index++;
if(countofdot == 2)
{
err = index;

return 0;
}
}
return Double.valueOf(a.substring(preindex, index)).doubleValue();
}
public char getnextop()
{
char ch = a.charAt(index);
if((ch != ‘+’) && (ch != ‘-‘) && (ch != ‘*’) && (ch != ‘/’))
{
err = index;
ch = ‘ ‘;
return ch;
}
index++;
return ch;

}
public double calcuvalue()
{
long start = System.currentTimeMillis();
Date d = new Date();
long now = d.getTime(), dif;
char ch;
boolean minusflag = false, multiflag = false, dividflag = false;
double total = 0, next = 0, num = 0; //next used to be * or /
while(index < len_of_str)
{
num = getnextnum();
if(err != -1)
{
System.out.println("err!?! try to getnextnum but " +
" the char at index " + err + " is wrong ");
return 0;
}
System.out.println("index after " + index);

ch = getnextop();
if(err != -1)
{
System.out.println("err!?! try to getnextop but " +
" the char at index " + err + " is wrong ");
return 0;
}
System.out.println("index after " + index);

switch (ch)
{
case ‘+’:

if(multiflag)
{
next = next * num;
}
else if(dividflag)
{
next = next / num;
}
else
{
next = num;
}
if(minusflag)
{
next = -next;
}
total = total + next;
minusflag = false;
multiflag = false;
dividflag = false;
break;
case ‘-‘:
if(multiflag)
{
next = next * num;
}
else if(dividflag)
{
next = next / num;
}
else
{
next = num;
}
if(minusflag)
{
next = -next;
}
total = total + next;
minusflag = true;
multiflag = false;
dividflag = false;
break;
case ‘*’:
if(multiflag)
{
next = next * num;
}
else if(dividflag)
{
next = next / num;
}
else
{
next = num;
}
multiflag = true;
dividflag = false;
break;

case ‘/’:
if(multiflag)
{
next = next * num;
}
else if(dividflag)
{
next = next / num;
}
else
{
next = num;
}
dividflag = true;
multiflag = false;
break;
default: //impossible ,already has err
}
System.out.println("ch " + ch + " num " + num + " total " + total +
" next " + next + " -" + minusflag + " *" +
multiflag + " /" + dividflag);
} //while

return total;

}

欢迎大家阅读《一个简单的表达式求值类,java,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一个简单的表达式求值类,java

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址