ぼんさい塾

ぼんさいノートと補遺に関する素材や注釈です.ミスが多いので初稿から1週間を経た重要な修正のみ最終更新日を残しています.

NetBeansによる演習 (7)

2013-02-02 19:52:29 | 暮らし
progJ.pdf
progJ-s.pdf
progJ-e.pdf

記事一覧

                                 java.util の利用

progJ-e.pdf にprogJ.pdf の[#32]-[#35]に対応する部分を追加しました.

//#38%2
//Queue.java
package myproj;
import java.util.*;
class Queue extends List {
  Cell last;
  Queue( ){super( ); last = top;}
  void insert(String s){
    last.n = new Cell(s);
    last = last.n; sz++;
  }
  String delete( ){
    Cell c = top.n;
    delete(1); if(sz == 0){last = top;}
    return c.d;
  }
  void str2queue(String s, Queue q){
    StringTokenizer st;
    st = new StringTokenizer(s);
    while(st.hasMoreTokens( )) //-
    {q.ins(st.nextToken( ));}
  }
//-------------------------------------------
  public static void main(String[] args) {
    String s = "123 + 4 + 56 + =";
    Queue q = new Queue( );
    q.str2queue(s, q); q.show( );
    /* 総和の計算{ */
    int x = 0, y = 0; String sq = q.delete( );
    while(sq.charAt(0) != '='){
      if(sq.charAt(0) == '+'){y += x;}
      else{x = Integer.parseInt(sq);}
      sq = q.delete( );
    }
    System.out.println(y);
    /* }総和の計算 */
  }
//-------------------------------------------
}
/////////////////////////////////////////////
//List.java
package myproj;
class Cell{
  String d; Cell n;
  Cell(String s){d = s; n = null;}
}
class List{
  Cell top; int sz;
  List(){top = new Cell(""); sz = 0;}
  void ins(String s, int k){
    Cell c1 = top, c2 = new Cell(s);
    while(k > 1){c1 = c1.n; k--;}
    if(c1.n != null){c2.n = c1.n;}
    c1.n = c2; sz++;
  }
  void del(int k){
    Cell c1 = top;
    while(k > 1){c1 = c1.n; k--;}
    c1.n = c1.n.n; sz--;
  }
  void show(){
    Cell c1 = top.n;
    for(int i = 1; i       System.out.print(c1.d + " ");
      c1 = c1.n;
    }
    System.out.println( );
  }
}