忍者ブログ
私筆記
カテゴリー
* [心路歷程] 綜合百匯(4) * [心路歷程] 文章轉錄(2) * [程式設計] C.C++(0) * [程式設計] JAVA(2) * [網頁設計] Sever(0) * [網頁設計] HTML(1) * [網頁設計] PHP(0) * [網頁設計] SQL(0) * [網頁設計] AJAX(0) * [軟硬兼施] 應用軟體(0) * [心路歷程]攝紀路(14)
  カレンダー
04 2024/05 06
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
  最新記事
  アーカイブ
  カテゴリー
  研究紀錄
  RSS
收藏本站Add to Technorati Favorites
今天瀏覽人次
昨天瀏覽人次
總計瀏覽人次
  最新コメント
  最新トラックバック
      バーコード
      ブログ内検索
    [24] [22] [20] [21] [19] [18]
    2024/05/03 (Fri)
    ×

    [PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

    2008/11/07 (Fri)
    /**********************************************************************
    *    DS第一次作業  by  496530002 資管二 郭建廷                              *           
    *                                                                                                                   *
    ***********************************************************************/
    /*載入JAVA預設函式庫*/
    import java.io.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Collections;
    import java.util.List;
    import java.lang.String;
    import java.lang.Integer;
    import java.util.Scanner;
    import java.util.InputMismatchException;

    public class dshw1/*主要class宣告*/{
        /*建立Linklist物件*/
        static LinkedList<String> list = new LinkedList<String>();
        static LinkedList<String> list2 = new LinkedList<String>();
        static LinkedList<String> list3 = new LinkedList<String>();
        /*建立Scanner物件*/
        static Scanner keyboard = new Scanner(System.in);
        /*主程式main宣告*/
        public static void main(String[]args)throws IOException{
            /*宣告資料讀取流,載入檔案*/
            BufferedReader br1 = new BufferedReader(new FileReader("Name.dat"));
            BufferedReader br3 = new BufferedReader(new FileReader("Name.dat"));
            BufferedReader br2 = new BufferedReader(new FileReader("Skill.dat"));
            String[]a1; String[]a2;        //宣告字串儲存用字串陣列
            String Name=br1.readLine();        //宣告字串變數儲存讀入字串
            String Skill=br2.readLine();
            String count1=br3.readLine();
            /*設定計數器偵測檔案行數調整陣列大小*/
            int count=0;    //宣告計數器
            while(count1!=null){         //設定迴圈讀取檔案、計數   
                count1 = br3.readLine();
                count++;
            }
            /*設定以變數設定大小之陣列*/
            int i=0;
            a1=new String [count+1];
            a2=new String [count+1];
            /*利用迴圈把資料儲存起來*/
            while(Name!=null){            
                a1[i]=Name;            //設定陣列儲存資料   
                a2[i]=Skill;
                Name = br1.readLine();        //以行為單位儲存到字串陣列中
                Skill = br2.readLine();
                list.add(a1[i]);        //將陣列中之字串之料一筆筆加入List物件中
                list2.add(a2[i]);
                i++;
            }
            System.out.println("");        //換行
            System.out.println("");
            /*程式主要功能選項*/
            int sel=0;
            try {
                    System.out.println("Select 1<第一題> or 2<第二題> or 3<第三題> or <第四、五題>");
                    sel=keyboard.nextInt();
                } catch(InputMismatchException e) {
                    System.out.printf("Not a correctly number.\n");
                    keyboard.nextLine();
                    System.out.printf("Try again\n\n");
                }
            switch(sel){
                case 1:linearlistprint(a1,a2,i);
                    break;
                case 2:sllnext(count);
                    break;   
                case 3:binso(a1,a2,i);
                    break;
                case 4:doublink(i);
                    break;
            }
        }
        /*第一題,將linearlist印出來*/
        public static String linearlistprint(String[]a1,String[]a2,int i)throws IOException{   
            /*印出*/
            System.out.println("");
            System.out.println("Array");
            for(int j=0;j<i;j++){
                System.out.print(a2[j]+"        ");
            }       
            System.out.println("");       
            for(int j=0;j<i;j++){
                System.out.print(a1[j]+"   ");
            }
            System.out.println("\n\nList");
            System.out.println(list2+"\n"+list);   
            return "";    //傳變數回主程式
        }
        /*第二題,SingleLinkList*/
        public static int sllnext(int i)throws IOException{
            System.out.println("");    //換行
            System.out.println("");
            System.out.println("");
            int cou=0;int con=cou+1;int xxx=1;int sto;       
            System.out.println("\n\nNode "+con+"\n"+list.get(cou)+"\n"+list2.get(cou)+"\n\n");
            System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
            sto = keyboard.nextInt();
            cou++;con++;
            while(cou<=i-1&&sto!=3){
                System.out.println("\n\nNode "+con+"\n"+list.get(cou)+"\n"+list2.get(cou)+"\n\n");
                System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
                sto = keyboard.nextInt();       
                if(sto==1){
                    cou++;
                    con++;
                }
                else if(sto==3){
                    System.exit(0);
                }
                else
                    cou=cou+0;
            }
            return 0;
        }
        /*第三題,Binsorting*/
        public static String binso(String[]a1,String[]a2,int r)throws IOException{
           
            int i=list.size();
            int[]bins=new int[i];    //宣告變數
            for(int j=0;j<i;j++){
                bins[j]=Integer.parseInt(a2[j]);
            }
            String t,t2,t3;int xll;
            boolean flag = true;
            for(int h = 0; h < i+1 && flag; h++) {
                //flag = false;
                for(int j = 0; j+1 < i; j++) {
                    if((Integer.parseInt(list2.get(j+1)) < (Integer.parseInt(list2.get(j))))){             
                        t = list2.get(j+1);
                        t2= list.get(j+1);
                        list2.remove(j+1);
                        list.remove(j+1);
                        list2.add(j+1,list2.get(j));
                        list.add(j+1,list.get(j));
                        list.remove(j);
                        list2.remove(j);
                        list2.add(j,t);
                        list.add(j,t2);
                    }
                    if((Integer.parseInt(list2.get(j+1)) == (Integer.parseInt(list2.get(j))))){
                        if(((list.get(j+1)).compareTo(list.get(j)))<0){
                            t3=list.get(j+1);
                            list.remove(j+1);
                            list.add(j+1,list.get(j));
                            list.remove(j);
                            list.add(j,t3);
                        }
                    }
                }
            }
            /*印出list*/
            System.out.println(list2);
            System.out.println(list);
           
            int cou=0;int con=cou+1;int xxx=1;int sto=0;
            System.out.println("\n\nNode "+con+"\n"+list.get(cou)+"\n"+list2.get(cou)+"\n\n");
           
           
            try {
                    System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
                    sto = keyboard.nextInt();
                } catch(InputMismatchException e) {   
                    System.out.printf("Not a correctly number.\n");
                    keyboard.nextLine();
                    System.out.printf("Try again\n\n");
                }
           
            cou++;con++;
            while(cou<=i-1&&sto!=3){
                System.out.println("\n\nNode "+con+"\n"+list.get(cou)+"\n"+list2.get(cou)+"\n\n");
                System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
                sto = keyboard.nextInt();       
                if(sto==1){
                    cou++;
                    con++;
                }
                else if(sto==3){
                    System.exit(0);
                }
                else
                    cou=cou+0;
            }
            return "";
        }
        /*第四、五題,DoublyLinkList*/
        public static String doublink(int r)throws IOException{
            /*設定選項*/
            int arc=0;
            try {
                    System.out.println("Select 1<AddNode> or 2<RemoveNode> or 3<Continue>");
                    arc = keyboard.nextInt();
                } catch(InputMismatchException e) {
                   
                    System.out.printf("Not a correctly number.\n");
                    keyboard.nextLine();
                    System.out.printf("Try again\n\n");
                }
            int sizeup=((list.size())+1);int rd;        //設定變數等於list大小
            while(arc!=3){
               
                //選項敘述
                switch(arc){
                    case 1:System.out.println("Add Name:");        //新增Node功能
                           String adn=keyboard.next();
                           System.out.println("Add Skill:");
                           String ads=keyboard.next();
                           list.add(adn);
                           list2.add(ads);
                           System.out.println(list);
                           break;
                    case 2:System.out.println(list2+"\n"+list);        //刪除node功能
                           System.out.println("Remove Which Node?");
                           rd=keyboard.nextInt();                     
                           System.out.println("Remove"+list.get(rd)+""+list2.get(rd)+"!!");
                           list.remove(rd);
                           list2.remove(rd);
                           System.out.println(list+"\n"+list2);
                           break;               
                    default:
                }
                try {
                    System.out.println("Select 1<AddNode> or 2<RemoveNode> or 3<Continue>");
                    arc = keyboard.nextInt();
                } catch(InputMismatchException e) {
                    System.out.printf("Not a correctly number.\n");
                    keyboard.nextLine();
                    System.out.printf("Try again\n\n");
                }
            }       
            int i=list.size();        //設定迴圈範圍
            System.out.println("");    //換行
            System.out.println("");
            System.out.println("");
            int cou=0;int con=cou+1;int xxx=1;int sto;        //變數宣告
            System.out.println("\n\nFirstNode \n"+list.getFirst()+"\n"+list2.getFirst()+"\n\n");        //印出結果
            System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
            sto = keyboard.nextInt();
            cou++;con++;
            while(sto!=3){
                while(cou<=i-2&&sto!=3){
                    System.out.println("\n\nNode "+con+"\n"+list.get(cou)+"\n"+list2.get(cou)+"\n\n");
                    System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
                    sto = keyboard.nextInt();       
                    if(sto==1){
                        cou++;
                        con++;
                    }
                    else if(sto==3){
                        System.exit(0);
                    }
                    else
                        cou=cou+0;
                }
               
                System.out.println("\n\nLastNode \n"+list.getLast()+"\n"+list2.getLast()+"\n\n");
                System.out.println("Select 1<Prev> or 2<Stop> or 3<exit>");
                sto = keyboard.nextInt();
                while(cou>1&&sto!=3){
                    System.out.println("\n\nNode "+(con-1)+"\n"+list.get(cou-1)+"\n"+list2.get(cou-1)+"\n\n");
                    System.out.println("Select 1<Prev> or 2<Stop> or 3<exit>");
                    sto = keyboard.nextInt();       
                    if(sto==1){
                        cou--;
                        con--;
                    }
                    else if(sto==3){
                        System.exit(0);
                    }
                    else
                        cou=cou+0;
                }   
                System.out.println("\n\nFirstNode "+(con-1)+"\n"+list.get(cou-1)+"\n"+list2.get(cou-1)+"\n\n");
                System.out.println("Select 1<Next> or 2<Stop> or 3<exit>");
                sto = keyboard.nextInt();
            }       
            return "";    //傳變數回主程式
        }       
    }
    PR
    この記事にコメントする
    name*
    title*
    color*
    mail*
    URL*
    comment*
    password* Vodafone絵文字 i-mode絵文字 Ezweb絵文字
    この記事へのトラックバック
    この記事にトラックバックする:
    忍者ブログ [PR]

    * ILLUSTRATION BY nyao *