导航:首页 > 编程语言 > evaljava

evaljava

发布时间:2023-07-21 05:04:43

㈠ 加减乘除运算(java)

实际上这相当于javascript的eval方法,以下是该方法的java实现:

//Eval.java
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Eval {
public int eval(String exp){
List<String> list = infixExpToPostExp(exp);//转化成后缀表达式
return doEval(list);//真正求值
}

//遇到操作符压栈,遇到表达式从后缀表达式中弹出两个数,计算出结果,压入堆栈
private int doEval(List<String> list) {
Stack<String> stack = new Stack<String>();
String element;
int n1,n2,result;
try{
for(int i = 0; i < list.size();i++){
element = list.get(i);
if(isOperator(element)){
n1 = Integer.parseInt(stack.pop());
n2 = Integer.parseInt(stack.pop());
result = doOperate(n1,n2,element);
stack.push(new Integer(result).toString());
}else{
stack.push(element);
}
}
return Integer.parseInt(stack.pop());
}catch(RuntimeException e){
throw new IllegalExpressionException(e.getMessage());
}
}

private int doOperate(int n1, int n2, String operator) {
if(operator.equals("+"))
return n1 + n2;
else if(operator.equals("-"))
return n1 - n2;
else if(operator.equals("*"))
return n1 * n2;
else
return n1 / n2;
}

private boolean isOperator(String str){
return str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/");
}

private List<String> infixExpToPostExp(String exp){//将中缀表达式转化成为后缀表达式
List<String> postExp = new ArrayList<String>();//存放转化的后缀表达式的链表
StringBuffer numBuffer = new StringBuffer();//用来保存一个数的
Stack<Character> opStack = new Stack<Character>();//操作符栈
char ch,preChar;
opStack.push('#');
try{
for(int i = 0; i < exp.length();){
ch = exp.charAt(i);
switch(ch){
case '+':
case '-':
case '*':
case '/':
preChar = opStack.peek();
// 如果栈里面的操作符优先级比当前的大,则把栈中优先级大的都添加到后缀表达式列表中
while(priority(preChar) >= priority(ch)){
postExp.add(""+preChar);
opStack.pop();
preChar = opStack.peek();
}
opStack.push(ch);
i++;
break;
case '(':
// 左括号直接压栈
opStack.push(ch);
i++;
break;
case ')':
// 右括号则直接把栈中左括号前面的弹出,并加入后缀表达式链表中
char c = opStack.pop();
while(c != '('){
postExp.add("" + c);
c = opStack.pop();
}
i++;
break;
// #号,代表表达式结束,可以直接把操作符栈中剩余的操作符全部弹出,并加入后缀表达式链表中
case '#':
char c1;
while(!opStack.isEmpty()){
c1 = opStack.pop();
if(c1 != '#')
postExp.add("" + c1);
}
i++;
break;
//过滤空白符
case ' ':
case '\t':
i++;
break;
// 数字则凑成一个整数,加入后缀表达式链表中
default:
if(Character.isDigit(ch)){
while(Character.isDigit(ch)){
numBuffer.append(ch);
ch = exp.charAt(++i);
}
postExp.add(numBuffer.toString());
numBuffer = new StringBuffer();
}else{
throw new IllegalExpressionException("illegal operator");
}
}
}
}catch(RuntimeException e){
throw new IllegalExpressionException(e.getMessage());
}
return postExp;
}

private int priority(char op){//定义优先级
switch(op){
case'+':
case'-':
return 1;
case'*':
case'/':
return 2;
case'(':
case'#':
return 0;
}
throw new IllegalExpressionException("Illegal operator");
}

}

Main.java 主函数所在类
public class Main
{
public static void main(String[] args) {

try {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String exp=br.readLine();

int result = eval.eval(exp);
System.out.println(result);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

IllegalExpressionException异常类
public class IllegalExpressionException extends RuntimeException{

public IllegalExpressionException(){

}

public IllegalExpressionException(String info){
super(info);
}
}

㈡ java 调用 js函数

function是属于javascript里面的语法,而<%%>里面调用的是java的语法,所以想要用<%%>调用alert()是不行了,因为他们不是同一个东西。
不知道你的意思是不是页面一加载就进行调用alert(),如果是这样的话,建议使用onload事件,表示页面加载的时候调用alert();
或者如果是点击的时候调用,那么久调用onclick()..总而言之可以使用事件完成。

㈢ 【高分悬赏】Java实现一个方法计算一个只有加减法运算的表达式的值

这个问题很简单,只有加减运算,没有运算优先级,不需要用到堆栈就可以。下面是代码:

publicclassApp{

publicstaticintA(Stringex)throwsException{

intresult=0;

Stringstr="";

for(inti=0;i<ex.length();i++){

charch=ex.charAt(i);

if(ch>='0'&&ch<='9'){

str+=ch;

}elseif(ch=='+'||ch=='-'){

result+=Integer.parseInt(str);
str=""+ch;

}elseif(ch==''){
continue;
}else{
thrownewException("无效的表达式。");
}
}

if(str!=""){
result+=Integer.parseInt(str);
}

returnresult;
}

publicstaticvoidmain(String[]args)throwsException{

intresult=A("1-2+3+4-23");

System.out.println("result="+result);
}
}

运行结果:

阅读全文

与evaljava相关的资料

热点内容
饥荒存档移到云服务器 浏览:916
婴儿pdf 浏览:767
钉钉网络加密在哪里设置 浏览:727
如何把安卓系统装到sd卡 浏览:735
python字典获取值方法 浏览:244
android手机安装失败 浏览:28
云计算没有服务器 浏览:67
怎么显示android的APP 浏览:121
c编译器怎么删除空格 浏览:695
php自动释放内存 浏览:220
golang编译库 浏览:794
oracle数据字符串加密 浏览:603
研究生去上海当程序员 浏览:90
u8电脑服务器连接失败怎么解决 浏览:569
bat脚本创建日期命名文件夹 浏览:104
将图片转换为pdf格式 浏览:980
java中形参 浏览:83
枚举类型编译器 浏览:519
oraclejava包 浏览:569
手机定位手机怎么定位安卓 浏览:523