大家好,我有一个JAVA 超难题(对我来说)要求救!

09-10-12 qangel1000
这个是我们的作业, 要求把计算式的中缀式改成计算式的后缀式: 如果只是这样就不难,但是,老师居然要求我们创建一个新的类,然后用

public static String gen(Expression e)

这个方法来写,而输入的EXPRESSION 是一个已经递了归之后的算式. 这个Expression 是一个INTERFACE, 里面唯一的方法是这个:

public int teval()


下面是java 代码: 大家看看怎么帮我解决这个问题:

第一个 Expression.java:
package expressions;

public interface Expression {

public int eval();

}

class Constant implements Expression {

private int value;

Constant (int v) {
value = v;
}

public int eval() {
return value;
}

}

class Difference implements Expression {

private Expression leftOperand, rightOperand;

Difference(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() - rightOperand.eval();
}

}

class Product implements Expression {

private Expression leftOperand, rightOperand;

Product(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() * rightOperand.eval();
}

}

class Quotient implements Expression {

private Expression leftOperand, rightOperand;

Quotient(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() / rightOperand.eval();
}

}

class Sum implements Expression {

private Expression leftOperand, rightOperand;

Sum(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() + rightOperand.eval();
}

}


class Factorial implements Expression{

private Expression value;

Factorial(Expression number){

value=number;
}

public int eval(){

return fact(value.eval());
}

private int fact(int n){

if(n==0)
return 1;
else
return (n*fact(n-1));
}
}

class GCD implements Expression{

private Expression leftOperand, rightOperand;

GCD(Expression lop, Expression rop) {

leftOperand = lop;
rightOperand = rop;
}

public int eval(){

if(leftOperand.eval()>rightOperand.eval())
return getGcd(rightOperand.eval(),leftOperand.eval());

else
return getGcd(leftOperand.eval(),rightOperand.eval());
}

private int getGcd(int small, int big){

int gcdFactor=big % small;

while(gcdFactor!=0){
big=small;
small=gcdFactor;
gcdFactor=big % small;

}

return small;
}
}

class MaxThree implements Expression{

private Expression firstOperand, secondOperand, thirdOperand;

MaxThree(Expression fop, Expression sop, Expression top){

firstOperand=fop;
secondOperand=sop;
thirdOperand=top;

}

public int eval(){

int maxValue=firstOperand.eval();

if(maxValue<secondOperand.eval())
maxValue=secondOperand.eval();

if(maxValue<thirdOperand.eval())
maxValue=thirdOperand.eval();

return maxValue;
}
}

qangel1000
2009-10-12 08:06
这个是我们的作业, 要求把计算式的中缀式改成计算式的后缀式: 如果只是这样就不难,但是,老师居然要求我们创建一个新的类,然后用

public static String gen(Expression e)

这个方法来写,而输入的EXPRESSION 是一个已经递了归之后的算式. 这个Expression 是一个INTERFACE, 里面唯一的方法是这个:

public int teval()


下面是java 代码: 大家看看怎么帮我解决这个问题:

第一个 Expression.java:
package expressions;

public interface Expression {

public int eval();

}

class Constant implements Expression {

private int value;

Constant (int v) {
value = v;
}

public int eval() {
return value;
}

}

class Difference implements Expression {

private Expression leftOperand, rightOperand;

Difference(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() - rightOperand.eval();
}

}

class Product implements Expression {

private Expression leftOperand, rightOperand;

Product(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() * rightOperand.eval();
}

}

class Quotient implements Expression {

private Expression leftOperand, rightOperand;

Quotient(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() / rightOperand.eval();
}

}

class Sum implements Expression {

private Expression leftOperand, rightOperand;

Sum(Expression lop, Expression rop) {
leftOperand = lop;
rightOperand = rop;
}

public int eval() {
return leftOperand.eval() + rightOperand.eval();
}

}


class Factorial implements Expression{

private Expression value;

Factorial(Expression number){

value=number;
}

public int eval(){

return fact(value.eval());
}

private int fact(int n){

if(n==0)
return 1;
else
return (n*fact(n-1));
}
}

class GCD implements Expression{

private Expression leftOperand, rightOperand;

GCD(Expression lop, Expression rop) {

leftOperand = lop;
rightOperand = rop;
}

public int eval(){

if(leftOperand.eval()>rightOperand.eval())
return getGcd(rightOperand.eval(),leftOperand.eval());

else
return getGcd(leftOperand.eval(),rightOperand.eval());
}

private int getGcd(int small, int big){

int gcdFactor=big % small;

while(gcdFactor!=0){
big=small;
small=gcdFactor;
gcdFactor=big % small;

}

return small;
}
}

class MaxThree implements Expression{

private Expression firstOperand, secondOperand, thirdOperand;

MaxThree(Expression fop, Expression sop, Expression top){

firstOperand=fop;
secondOperand=sop;
thirdOperand=top;

}

public int eval(){

int maxValue=firstOperand.eval();

if(maxValue<secondOperand.eval())
maxValue=secondOperand.eval();

if(maxValue<thirdOperand.eval())
maxValue=thirdOperand.eval();

return maxValue;
}
}

qangel1000
2009-10-12 08:08
第二个代码, ExpressionParser.java
package expressions;

import java.util.*;

public class ExpressionParser {

Scanner scanner;

public ExpressionParser(String str) {
scanner = new Scanner(str);
}

public Expression parse() {

Expression result = parseTerm();
if (result == null)
return null;

Expression nextOperand;
while (scanner.hasNext("\\+") || scanner.hasNext("\\-"))
if (scanner.hasNext("\\+")) {
scanner.next("\\+");
nextOperand = parseTerm();
if (nextOperand == null)
return null;
result = new Sum(result, nextOperand);
}
else if (scanner.hasNext("\\-")) {
scanner.next("\\-");
nextOperand = parseTerm();
if (nextOperand == null)
return null;
result = new Difference(result, nextOperand);
}

return result;

}

public Expression parseTerm() {

Expression result = parseFactor();
if (result == null)
return null;

Expression nextOperand;
while (scanner.hasNext("\\*") || scanner.hasNext("\\/"))
if (scanner.hasNext("\\*")) {
scanner.next("\\*");
nextOperand = parseFactor();
if (nextOperand == null)
return null;
result = new Product(result, nextOperand);
}
else if (scanner.hasNext("\\/")) {
scanner.next("\\/");
nextOperand = parseFactor();
if (nextOperand == null)
return null;
result = new Quotient(result, nextOperand);
}

return result;

}





public Expression parseFactor() {

Expression result;
Expression nextOperand1;
Expression nextOperand2;

if (scanner.hasNextInt())
return new Constant(scanner.nextInt());

else if (scanner.hasNext("\\(") || scanner.hasNext("fact\\(") || scanner.hasNext("gcd\\(") || scanner.hasNext("maxthree\\(")) {
if(scanner.hasNext("fact\\(")){
scanner.next("fact\\(");
result = parse();
if(result==null)
return null;
result = new Factorial(result);
if(scanner.hasNext("\\)"))
scanner.next("\\)");
else
return null;
}

else if(scanner.hasNext("gcd\\(")){
scanner.next("gcd\\(");
result=parse();
if(result==null)
return null;
if(scanner.hasNext("\\,")){
scanner.next("\\,");
nextOperand1=parse();
if(nextOperand1==null)
return null;
result= new GCD(result,nextOperand1);
}
else
return null;
if(scanner.hasNext("\\)"))
scanner.next("\\)");
else
return null;
}

else if(scanner.hasNext("maxthree\\(")){
scanner.next("maxthree\\(");
result=parse();
if(result==null)
return null;
if(scanner.hasNext("\\,")){
scanner.next("\\,");
nextOperand1=parse();
if(nextOperand1==null)
return null;
}
else
return null;
if(scanner.hasNext("\\,")){
scanner.next("\\,");
nextOperand2=parse();
if(nextOperand2==null)
return null;
result=new MaxThree(result,nextOperand1,nextOperand2);
}
else
return null;
if(scanner.hasNext("\\)"))
scanner.next("\\)");
else
return null;
}

else {
scanner.next("\\(");
result = parse();
if (scanner.hasNext("\\)"))
scanner.next("\\)");
else
return null;
}
}

else
return null;

return result;

}

}

第三个代码 InfixCalculator.java
package expressions;

import java.util.*;

public class InfixCalculator {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Infix Calculator");
System.out.println("Use spaces as delimiters.");
System.out.print("> ");
String line = scanner.nextLine();

while (line.length() > 0) {

ExpressionParser parser = new ExpressionParser(line);
Expression result = parser.parse();
if (result != null)
System.out.println(result.eval());
else
System.out.println("Invalid expression.");

System.out.print("> ");
line = scanner.nextLine();

}

}

}