progJ.pdf 記事一覧 |
List のサブクラス Que のメソッド |
progJ.pdf に [#30] ~ [#37] を追加しました.
補足:(1) P34.java
public class P34{
public static void main(String[] args) {
String s = "-123"; int k;
k = Integer.parseInt(s);
s = Double.toString(k + 4.5);
System.out.println(k + ", " + s);
//
//k = Integer.parseInt(" " + s);
//k = Integer.parseInt(s + ".6");
System.out.println(s + ", " + k);
//
Object obj = k;
System.out.println(s=Integer.toHexString(k));
Integer i = new Integer(k);
System.out.println(i.intValue());
//System.out.println(k.intValue());
}
}
(2) P35.java
import java.util.*;
public class P35{
public static void main(String[ ] args) {
String s = "123 + 4 + 56 + =";
//
Que q = new Que( );
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens( )){
q.insert(st.nextToken( ));
}
//
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 (デフォルトパッケージ,別ファイル可)
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 insert(String s, int k){
if(ksz+1){return;}
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 delete(int k){
if(ksz){return;}
Cell c1 = top;
while(k > 1){c1 = c1.n; k--;}
c1.n = c1.n.n; sz--;
}
String show(int k){
if(ksz){return "?";}
Cell c1 = top.n; int i = k;
while(k > 1){c1 = c1.n; k--;}
System.out.println(i + ": " + c1.d);
return c1.d;
}
void show(){
Cell c1 = top.n; int i;
if(sz < 1){System.out.print("empty");}
for(i = 1; i < sz + 1; i++){
System.out.print(c1.d + " ");
if(i < sz ){c1 = c1.n;}
}
System.out.println();
}
}
class Que extends List{
Cell last;
Que( ){ 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;
}
}
(3) P36.java
class Node{
String d; int ix; Node ln, rn;
Node(String s, int k){
d = s; ix = k; ln = rn = null;
}
}
/* abstract */ class Tree{
Node root;
Tree( ){root = new Node("", 0);}
//abstract void insert(String s, int k);
//abstract void delete( );
void show(String s, Node n){
if(n == null) return;
show(s+"L", n.ln);
System.out.println(s + ": " + n.ix + " " + n.d);
show(s+"R", n.rn);
}
}
public class P36{
public static void main(String[ ] args) {
Tree t = new Tree(); Node rootR = t.root.rn;
Node tmp; String s = "a";
tmp = new Node(s, 7); rootR = tmp;
tmp = new Node(s, 5); rootR.ln = tmp;
tmp = new Node(s, 9); rootR.rn = tmp;
tmp = new Node(s, 2); rootR.ln.ln = tmp;
t.show("", rootR);
}
}
※コメント投稿者のブログIDはブログ作成者のみに通知されます