我正在努力理解我的家庭作业,java Lisp算术表达式

我正在努力理解我的家庭作业,java Lisp算术表达式,java,postfix-notation,stack,Java,Postfix Notation,Stack,我正试图理解人们到底要求我做什么,也许你能给我一些直觉,让我思考如何解决这个问题。我需要用Java编写一个程序来计算Lisp算术表达式,但解决方案必须遵循特定的说明 我需要评估如下内容: (+\t(- 6)\n\t(/\t(+ 3)\n\t\t(- \t(+ 1 1)\n\t\t\t3\n\t\t\t1)\n\t\t(*))\n\t(* 2 3 4)) 我应该实现一个上下文堆栈,就是这样 Stack contextStack 所以我猜这是一堆队列。我还有一个类ExpressionScanner

我正试图理解人们到底要求我做什么,也许你能给我一些直觉,让我思考如何解决这个问题。我需要用Java编写一个程序来计算Lisp算术表达式,但解决方案必须遵循特定的说明

我需要评估如下内容:

(+\t(- 6)\n\t(/\t(+ 3)\n\t\t(- \t(+ 1 1)\n\t\t\t3\n\t\t\t1)\n\t\t(*))\n\t(* 2 3 4))
我应该实现一个上下文堆栈,就是这样

Stack contextStack

所以我猜这是一堆队列。我还有一个类ExpressionScanner,它扫描字符串并查找运算符和操作数:

public class ExpressionScanner
{
  private String e;
  private int position;

  public ExpressionScanner(String e)
  {
    this.e = e;
    this.position = 0;
  }


 public boolean hasNextOperator()
  {
    skipWhiteSpace();
    return position < e.length() && isOperator(e.charAt(position));
  }

  public char nextOperator()
  {
    skipWhiteSpace();
    return e.charAt(position++);
  }

  public boolean hasNextOperand()
  {
    skipWhiteSpace();
    return position < e.length() && isDigit(e.charAt(position));
  }

  public int nextOperand()
  {
    skipWhiteSpace();
    int operand = 0;
    while (e.charAt(position) >= '0' && e.charAt(position) <='9')
      operand = 10 * operand + e.charAt(position++) - '0';
    return operand;
  }

  private void skipWhiteSpace()
  {
    char c;
    while (position < e.length() && ((c = e.charAt(position)) == ' ' || c == '\t' || c == '\n'))
      position++;
    return;
  }

  private static boolean isOperator(char c)
  {
    return c == '(' || c == '+' || c == '-' || c == '*' || c == '/' || c == ')';
  }

  private static boolean isDigit(char c)
  {
    return c >= '0' && c <= '9';
  }
} /*201340*/
公共类ExpressionScanner
{
私有字符串e;
私人职位;
公共表达式扫描程序(字符串e)
{
这个。e=e;
这个位置=0;
}
公共布尔值hasNextOperator()
{
skipWhiteSpace();
返回位置而(e.charAt(position)>='0'和&e.charAt(position)='0'和&c这是问题的解决方案

import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;
import java.util.ArrayList;


class IterativeEvaluator2
{ 
    private ExpressionScanner expression;

     public IterativeEvaluator2 (String expression)
    {
        this.expression = new ExpressionScanner(expression);
    }

    public double evaluate(Queue<Double> operandqueue)
    {
            // write your code here to create an explicit context stack
        Stack<Queue> temp_stack = new Stack<Queue>();

        char operator = ' ';
            double operand = 0.0;
        int a;

            // write your code here to evaluate the LISP expression iteratively
            // you will need to use an explicit stack to push and pop context objects

        while ( expression.hasNextOperator() || expression.hasNextOperand() )
        {

            // Get the open bracket         

            if ( expression.hasNextOperator())    
                {
                operator = expression.nextOperator() ;

                if (operator == '(')
                { 
                    temp_stack.push(operandqueue);
                    operandqueue = new LinkedList<Double>();
                }

                // push the list into the stack after the closing bracket appears       
                else if (operator == ')')
                {   
                    if (operandqueue.size() > 1 ) {
                        //System.out.println("new opqueue");
                        //System.out.println(operandqueue); 
                        operand = calculate(operandqueue);
                        System.out.println(operand);

                        if (temp_stack.peek() != null)
                        {   
                            operandqueue = temp_stack.pop();
                        //System.out.println(operandqueue);
                            operandqueue.offer(operand);
                        }
                    }
                    else if (operandqueue.size() == 1)
                        operandqueue = temp_stack.pop();
                }
            // if it is another operator then it must be +,-,/,*
                else 
                {
                    operandqueue.offer( (double) operator );
                }
            }
            // else it is an operand so just put it in the queue
            else 
            {
                a= expression.nextOperand() ; 
                //System.out.println(a);
                operandqueue.offer( (double) a );

            }
        }
        return operand;
    }

    private double calculate(Queue<Double> some_queue)
    {
            char operator = (char) some_queue.remove().intValue();
            //System.out.println(operator);
            double operand1 = 0;
            double operand2;            
            switch(operator){
            case '+' :  while( !some_queue.isEmpty() )
                    {
                        operand2 = some_queue.remove();
                        operand1 = operand1 + operand2;
                    }
                    break;

            case '-' :  operand1 = some_queue.remove();
                    //checks for negative numbers
                    if (some_queue.isEmpty() ){
                        operand1 = 0  -  operand1;
                        } 
                    else{
                        while ( !some_queue.isEmpty() )
                        {
                            operand2 = some_queue.remove();
                            operand1 =  operand1 - operand2;
                            System.out.println(operand1);
                        }

                    }
                    break;

            case '*' :  if (!some_queue.isEmpty() )
                    {
                        operand1 = 1;
                        while ( !some_queue.isEmpty() )
                        {
                            operand2 = some_queue.remove();
                            operand1 = operand1*operand2;
                        }
                    }
                    break;

            case '/' :  operand1 = some_queue.remove();
                    if (some_queue.isEmpty() )
                                                operand1 = 1/operand1 ;
                                    else{
                                            while (!some_queue.isEmpty() )
                        {
                                operand2 = some_queue.remove();
                            operand1 = operand1/operand2;                                                                                               }
                    }
                    break;
            }
        return operand1;
    }

    public static void main(String [] args)
    {
        String s =  
        "(+\t(- 6)\n\t(/\t(+ 3)\n\t\t(- \t(+ 1 1)\n\t\t\t3\n\t\t\t1)\n\t\t(*))\n\t(* 2 3 4))";  // = 16.5
        IterativeEvaluator2 myEvaluator = new IterativeEvaluator2(s);
        System.out.println("Evaluating LISP Expression:\n" + s);
        System.out.println("Value is: " + myEvaluator.evaluate(null)); 
    }
}  /* 201340 */
import java.util.Queue;
导入java.util.LinkedList;
导入java.util.Stack;
导入java.util.ArrayList;
类迭代计算器2
{ 
私人表达浏览者表达;
公共迭代计算器2(字符串表达式)
{
this.expression=新的ExpressionScanner(表达式);
}
公共双重求值(队列操作数队列)
{
//在此处编写代码以创建显式上下文堆栈
堆栈温度=新堆栈();
字符运算符=“”;
双操作数=0.0;
INTA;
//在此处编写代码以迭代方式计算LISP表达式
//您需要使用显式堆栈来推送和弹出上下文对象
while(expression.hasnetoptoperator()| | expression.hasnetoptoperand())
{
//打开支架
if(表达式.hasNextOperator())
{
运算符=表达式.nextOperator();
if(运算符=='(')
{ 
临时堆栈推送(操作数队列);
操作数队列=新的LinkedList();
}
//在右括号出现后,将列表推入堆栈
else if(运算符=='))
{   
如果(操作数队列.size()>1){
//System.out.println(“新操作队列”);
//System.out.println(操作数队列);
操作数=计算(操作数队列);
System.out.println(操作数);
if(temp_stack.peek()!=null)
{   
OperatorQueue=temp_stack.pop();
//System.out.println(操作数队列);
操作数队列。提供(操作数);
}
}
else if(操作数queue.size()==1)
OperatorQueue=temp_stack.pop();
}
//如果它是另一个运算符,则它必须是+,-,/*
其他的
{
操作数queue.offer((双)运算符);
}
}
//否则它是一个操作数,所以只需将其放入队列中即可
其他的
{
a=表达式.nextOperand();
//系统输出打印项次(a);
报价((双)a);
}
}
返回操作数;
}
私有双重计算(队列某些队列)
{
char运算符=(char)some_queue.remove().intValue();
//系统输出打印项次(操作员);
双操作数1=0;
双操作数2;
开关(操作员){
大小写“+”:while(!some_queue.isEmpty())
{
操作数2=某些队列。删除();
操作数1=操作数1+操作数2;
}
打破
大小写'-':操作数1=some_queue.remove();
//检查负数
if(some_queue.isEmpty()){
操作数1=0-操作数1;
} 
否则{
而(!some_queue.isEmpty())
{
操作数2=某些队列。删除();
操作数1=操作数1-操作数2;
系统输出打印项次(操作数1);
}
}
打破
案例“*”:如果(!some_queue.isEmpty())
{
操作数1=1;
而(!some_queue.isEmpty())
{
操作数2=某些队列。删除();
操作数1=操作数1*操作数2;
}
}
打破
大小写“/”:操作数1=some_queue.remove();
if(some_queue.isEmpty())
操作数1=1/操作数1;
否则{
而(!some_queue.isEmpty())
{
操作数2=某些队列。删除();
操作数1=操作数1/操作数2;}
}
打破
}
返回操作数1;
}
import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;
import java.util.ArrayList;


class IterativeEvaluator2
{ 
    private ExpressionScanner expression;

     public IterativeEvaluator2 (String expression)
    {
        this.expression = new ExpressionScanner(expression);
    }

    public double evaluate(Queue<Double> operandqueue)
    {
            // write your code here to create an explicit context stack
        Stack<Queue> temp_stack = new Stack<Queue>();

        char operator = ' ';
            double operand = 0.0;
        int a;

            // write your code here to evaluate the LISP expression iteratively
            // you will need to use an explicit stack to push and pop context objects

        while ( expression.hasNextOperator() || expression.hasNextOperand() )
        {

            // Get the open bracket         

            if ( expression.hasNextOperator())    
                {
                operator = expression.nextOperator() ;

                if (operator == '(')
                { 
                    temp_stack.push(operandqueue);
                    operandqueue = new LinkedList<Double>();
                }

                // push the list into the stack after the closing bracket appears       
                else if (operator == ')')
                {   
                    if (operandqueue.size() > 1 ) {
                        //System.out.println("new opqueue");
                        //System.out.println(operandqueue); 
                        operand = calculate(operandqueue);
                        System.out.println(operand);

                        if (temp_stack.peek() != null)
                        {   
                            operandqueue = temp_stack.pop();
                        //System.out.println(operandqueue);
                            operandqueue.offer(operand);
                        }
                    }
                    else if (operandqueue.size() == 1)
                        operandqueue = temp_stack.pop();
                }
            // if it is another operator then it must be +,-,/,*
                else 
                {
                    operandqueue.offer( (double) operator );
                }
            }
            // else it is an operand so just put it in the queue
            else 
            {
                a= expression.nextOperand() ; 
                //System.out.println(a);
                operandqueue.offer( (double) a );

            }
        }
        return operand;
    }

    private double calculate(Queue<Double> some_queue)
    {
            char operator = (char) some_queue.remove().intValue();
            //System.out.println(operator);
            double operand1 = 0;
            double operand2;            
            switch(operator){
            case '+' :  while( !some_queue.isEmpty() )
                    {
                        operand2 = some_queue.remove();
                        operand1 = operand1 + operand2;
                    }
                    break;

            case '-' :  operand1 = some_queue.remove();
                    //checks for negative numbers
                    if (some_queue.isEmpty() ){
                        operand1 = 0  -  operand1;
                        } 
                    else{
                        while ( !some_queue.isEmpty() )
                        {
                            operand2 = some_queue.remove();
                            operand1 =  operand1 - operand2;
                            System.out.println(operand1);
                        }

                    }
                    break;

            case '*' :  if (!some_queue.isEmpty() )
                    {
                        operand1 = 1;
                        while ( !some_queue.isEmpty() )
                        {
                            operand2 = some_queue.remove();
                            operand1 = operand1*operand2;
                        }
                    }
                    break;

            case '/' :  operand1 = some_queue.remove();
                    if (some_queue.isEmpty() )
                                                operand1 = 1/operand1 ;
                                    else{
                                            while (!some_queue.isEmpty() )
                        {
                                operand2 = some_queue.remove();
                            operand1 = operand1/operand2;                                                                                               }
                    }
                    break;
            }
        return operand1;
    }

    public static void main(String [] args)
    {
        String s =  
        "(+\t(- 6)\n\t(/\t(+ 3)\n\t\t(- \t(+ 1 1)\n\t\t\t3\n\t\t\t1)\n\t\t(*))\n\t(* 2 3 4))";  // = 16.5
        IterativeEvaluator2 myEvaluator = new IterativeEvaluator2(s);
        System.out.println("Evaluating LISP Expression:\n" + s);
        System.out.println("Value is: " + myEvaluator.evaluate(null)); 
    }
}  /* 201340 */