永发信息网

java求解表达式

答案:5  悬赏:40  手机版
解决时间 2021-04-13 04:44
  • 提问者网友:沦陷
  • 2021-04-12 08:39

编写程序,计算任何一个四则表达式的值。
(除法取整即可,不必考虑括号)
题:变量a的值为“23+3*32/2-65”。运行程序后输出此表达式的结果。
提示:综合应用字符串和Vector的相关知识。

最好有解释...我是JAVA初学者

最佳答案
  • 五星知识达人网友:十鸦
  • 2021-04-12 08:50
这个代码只能操作整数,基本满足你的要求:

import java.util.Vector;

class token {
String tokenString;
int idx;
public token( String s ) { tokenString = s; idx = 0; }
public char getNext() {
if ( idx < tokenString.length() )
return tokenString.charAt(idx++);
else
return 0;
}
public void putBack() {
--idx;
}
public char peekNext() {
if ( idx < tokenString.length() )
return tokenString.charAt(idx);
else
return 0;
}
}

class eval {
String expression;
Vector<Integer> opd;
Vector<Character> opt;

public eval( String s ) {
expression = s;
opd = new Vector<Integer>();
opt = new Vector<Character>();
}

public int getValue() {
token t = new token( expression );

char c;
while( (c = t.getNext()) != 0 ) {
if ( Character.isDigit(c) ) {
int m = c - '0';
while ( Character.isDigit(c = t.peekNext()) ) {
t.getNext();
m = m * 10 + c - '0';
}
opd.add(m);
} else if ( c == '+' || c == '-' ||
c == '*' || c == '/' ) {
while ( opt.size() != 0
&& getP( opt.lastElement() ) >= getP( c ) ) {
doCalc();
}
opt.add( c );
}
}
while ( opt.size() != 0 )
doCalc();
return opd.firstElement();
}

private void doCalc()
{
char op = opt.lastElement();
opt.remove( opt.size() - 1 );
int r = opd.lastElement();
opd.remove( opd.size() - 1 );
int l = opd.lastElement();
opd.remove( opd.size() - 1 );
int result = operate( l, r, op );
opd.add( result );
}

private int operate( int a, int b, char op ) {
switch ( op )
{
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}

private int getP ( char c ) {
switch ( c )
{
case '+':case '-': return 1;
case '*':case '/': return 2;
}
return 0;
}
}

public class test
{
public static void main(String args[])
{
eval e = new eval( "23+3*32/2-65" );
int n = e.getValue();
System.out.println( n + "\t" + (23+3*32/2-65));
}
}
全部回答
  • 1楼网友:逃夭
  • 2021-04-12 12:17
按照你的要求写了一个,但还不够完善,比如没有进行参数安全性检查等等,你可以自己再改一改。 一共两个类:Main.java Calculator.java 具体代码: Main.java: public class Main { public static void main(String[] args) throws Exception{ String expression = "23+3*32/2-65"; System.out.println(expression + "=" + Calculator.calculate(expression)); } } Calculator.java: import java.util.Vector; import java.util.StringTokenizer; public class Calculator { //运算数 private static Vector<Integer> operands = new Vector<Integer>(); //运算符 private static Vector<String> operators = new Vector<String>(); public static int calculate(String expression) throws Exception { StringTokenizer tokenizer = new StringTokenizer(expression, "+-*/", true); while (tokenizer.hasMoreTokens()) { //System.out.println(tokenizer.nextToken()); String token = tokenizer.nextToken(); if (!token.equals("+") && !token.equals("-") && !token.equals("*") && !token.equals("/")) { operands.add(Integer.parseInt(token)); } else { operators.add(token); } } //System.out.println(operands); //System.out.println(operators); int p = 0; int resault = 0; while (operators.size() > 1) { if (priority(operators.get(p)) < priority(operators.get(p + 1))) { p++; } else { resault = doOperate(operands.remove(p), operands.remove(p), operators.remove(p)); operands.add(p, resault); p = 0; //System.out.println("-------------------------------"); //System.out.println(operands); //System.out.println(operators); } } //只剩下两个操作数和一个运算符,直接计算即结果 resault = doOperate(operands.get(p), operands.get(p + 1), operators.get(p)); return resault; } //计算运算符(+ - * /)的优先级,+和-的优先级为1,*和/的优先级为2 private static int priority(String operator) { if (operator.equals("+") || operator.equals("-")) { return 1; } else { return 2; } } //对+ - * /运算进行封装 private static int doOperate(int operand1, int operand2, String operator) throws Exception { if (operator.equals("+")) { return operand1 + operand2; } if (operator.equals("-")) { return operand1 - operand2; } if (operator.equals("*")) { return operand1 * operand2; } if (operator.equals("/")) { return operand1 / operand2; } throw new Exception("+ - * /以外的非法操作"); } }
  • 2楼网友:春色三分
  • 2021-04-12 10:39
按照你的要求写了一个,但还不够完善,比如没有进行参数安全性检查等等,你可以自己再改一改。 一共两个类:Main.java Calculator.java 具体代码: Main.java: public class Main { public static void main(String[] args) throws Exception{ String expression = "23+3*32/2-65"; System.out.println(expression + "=" + Calculator.calculate(expression)); } } Calculator.java: import java.util.Vector; import java.util.StringTokenizer; public class Calculator { //运算数 private static Vector<Integer> operands = new Vector<Integer>(); //运算符 private static Vector<String> operators = new Vector<String>(); public static int calculate(String expression) throws Exception { StringTokenizer tokenizer = new StringTokenizer(expression, "+-*/", true); while (tokenizer.hasMoreTokens()) { //System.out.println(tokenizer.nextToken()); String token = tokenizer.nextToken(); if (!token.equals("+") && !token.equals("-") && !token.equals("*") && !token.equals("/")) { operands.add(Integer.parseInt(token)); } else { operators.add(token); } } //System.out.println(operands); //System.out.println(operators); int p = 0; int resault = 0; while (operators.size() > 1) { if (priority(operators.get(p)) < priority(operators.get(p + 1))) { p++; } else { resault = doOperate(operands.remove(p), operands.remove(p), operators.remove(p)); operands.add(p, resault); p = 0; //System.out.println("-------------------------------"); //System.out.println(operands); //System.out.println(operators); } } //只剩下两个操作数和一个运算符,直接计算即结果 resault = doOperate(operands.get(p), operands.get(p + 1), operators.get(p)); return resault; } //计算运算符(+ - * /)的优先级,+和-的优先级为1,*和/的优先级为2 private static int priority(String operator) { if (operator.equals("+") || operator.equals("-")) { return 1; } else { return 2; } } //对+ - * /运算进行封装 private static int doOperate(int operand1, int operand2, String operator) throws Exception { if (operator.equals("+")) { return operand1 + operand2; } if (operator.equals("-")) { return operand1 - operand2; } if (operator.equals("*")) { return operand1 * operand2; } if (operator.equals("/")) { return operand1 / operand2; } throw new Exception("+ - * /以外的非法操作"); } }
  • 3楼网友:杯酒困英雄
  • 2021-04-12 10:20
这个群里有很多高手。可以去这里看看。应该可以找到答案,38615496
  • 4楼网友:拾荒鲤
  • 2021-04-12 09:11

Vector是List的实现集合类,线程安全!

你要它来成放数据吗~?

此题比较简单!用vector显的用牛刀啦!

直接运算不得行吗?

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯