java数据结构八、通过栈根据后缀表达式计算值

//postfix.java
//parses postfix arithmetic expressions
//to run this program: C>java PostfixApp
import java.io.*;              // for I/O

class StackX
{
private int maxSize;
private int[] stackArray;
private int top;
//--------------------------------------------------------------
public StackX(int size)      // constructor
   {
   maxSize = size;
   stackArray = new int[maxSize];
   top = -1;
   }
//--------------------------------------------------------------
public void push(int j)     // put item on top of stack
   { stackArray[++top] = j; }
//--------------------------------------------------------------
public int pop()            // take item from top of stack
   { return stackArray[top--]; }
//--------------------------------------------------------------
public int peek()           // peek at top of stack
   { return stackArray[top]; }
//--------------------------------------------------------------
public boolean isEmpty()    // true if stack is empty
   { return (top == -1); }
//--------------------------------------------------------------
public boolean isFull()     // true if stack is full
   { return (top == maxSize-1); }
//--------------------------------------------------------------
public int size()           // return size
   { return top+1; }
//--------------------------------------------------------------
public int peekN(int n)     // peek at index n
   { return stackArray[n]; }
//--------------------------------------------------------------
public void displayStack(String s)
   {
   System.out.print(s);
   System.out.print("Stack (bottom-->top): ");
   for(int j=0; j<size(); j++)
      {
      System.out.print( peekN(j) );
      System.out.print(' ');
      }
   System.out.println("");
   }
//--------------------------------------------------------------
}  // end class StackX

class ParsePost
{
private StackX theStack;
private String input;
//--------------------------------------------------------------
public ParsePost(String s)
   { input = s; }
//--------------------------------------------------------------
public int doParse()
   {
   theStack = new StackX(20);             // make new stack
   char ch;
   int j;
   int num1, num2, interAns;

   for(j=0; j<input.length(); j++)       // for each char,
      {
      ch = input.charAt(j);              // read from input
      theStack.displayStack(""+ch+" ");  // *diagnostic*
      if(ch >= '0' && ch <= '9')         // if it's a number
         theStack.push( (int)(ch-'0') ); //   push it
      else                               // it's an operator
         {
         num2 = theStack.pop();          // pop operands
         num1 = theStack.pop();
         switch(ch)                      // do arithmetic
            {
            case '+':
               interAns = num1 + num2;
               break;
            case '-':
               interAns = num1 - num2;
               break;
            case '*':
               interAns = num1 * num2;
               break;
            case '/':
               interAns = num1 / num2;
               break;
            default:
               interAns = 0;
            }  // end switch
         theStack.push(interAns);        // push result
         }  // end else
      }  // end for
   interAns = theStack.pop();            // get answer
   return interAns;
   }  // end doParse()
}  // end class ParsePost

class PostfixApp
{
public static void main(String[] args) throws IOException
   {
   String input;
   int output;

   while(true)
      {
      System.out.print("Enter postfix: ");
      System.out.flush();
      input = getString();         // read a string from kbd
      if( input.equals("") )       // quit if [Enter]
         break;
                                   // make a parser
      ParsePost aParser = new ParsePost(input);
      output = aParser.doParse();  // do the evaluation
      System.out.println("Evaluates to " + output);
      }  // end while
   }  // end main()
//--------------------------------------------------------------
public static String getString() throws IOException
   {
   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
   String s = br.readLine();
   return s;
   }
//--------------------------------------------------------------
}  // end class PostfixApp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
做一门精致,全面详细的 java数据结构与算法!!!让天下没有难学的数据结构,让天下没有难学的算法,不吹不黑,我们的讲师及其敬业,可以看到课程视频,课件,代码的录制撰写,都是在深夜,如此用心,其心可鉴,他不掉头发,谁掉头发???总之你知道的,不知道的,我们都讲,并且持续更新,走过路过,不要错过,不敢说是史上最全的课程,怕违反广告法,总而言之,言而总之,这门课你得拥有,好吃不贵,对于你知识的渴求,我们管够管饱话不多说,牛不多吹,我们要讲的本门课程内容:稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值