hooyantsing's Blog

第37次课程

字数统计: 1.9k阅读时长: 11 min
2019/07/14

源辰74班

第37次课程

07.14.2019

**内容    **

API部分作业检查、复习及指导[蒋斌]

1.作业检查

双向链表的实现

**Class Node

**

1
2
3
4
5
6
7
8
9
10
11
12
13
package ShuangXiangLinkedList;
public class Node<E> {
    E item;
    Node<E> parent;
    Node<E> next;
    public Node() {
       super();
    }
    public Node(E o) {
       super();
       this.item = o;
    }   
}

Class DoubleLinkedList

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ShuangXiangLinkedList;
public class DoubleLinkedList<E> {
    Node<E> first;
    Node<E> last;
    int size;
    
    public boolean add(E item) {
       Node<E> l = last;
       Node<E> node = new Node<E>();
       node.item = item;
       if(l!=null) {
           l.next = node;
           node.parent = l;
       }
       if(first==null) {
           first=node;
       }
       last = node;
       size++;
       return true;
    }
    
    public void addFirst(E item) {
       Node<E> node = new Node<E>();
       node.item = item;
       node.next = first;
       first.parent = node;
       first = node;
       size++;
    }
    
    public void remove(int index) {
       if(index<0 || index>=size) {
           throw new  IndexOutOfBoundsException("index"+index+"size");
       }
       Node<E> pre = first;
       if(index==0) {
           first = first.next;
           first.parent = null;
           size--;
           return;
       }
       for(int i=0;i<index-1;i++) {
           pre = pre.next;
       }
       Node<E> self = pre.next;
       Node<E> next = self.next;
       if(next==null) {
           pre.next = null;
           self = null;
           last = pre;
       }else {
           pre.next = next;
           next.parent = pre;
           self = null;
       }
       size--;
    }
    
    public E removeLast() {
       if(size==0 || first==null ||  last==null) {
           throw new  IndexOutOfBoundsException("请先创建链表");
       }
       Node<E> pre = first;
       for(int i=0;i<size-1;i++) {
           pre = pre.next;
       }
       pre.parent.next = null;
       size--;
       return pre.item;
    }
    public int getSize() {
       return size;
    }
    
    public E get(int index) {
       if(index<0 || index>=size) {
           throw new  IndexOutOfBoundsException("index"+index+"size");
       }
       Node<E> result = first;
       for(int i=0;i<index;i++) {
           result = result.next;
       }
       return result.item;
    }
    
    public int indexOf(Object o) {
       if(o==null) {
           throw new RuntimeException("不能为空");
       }
       if(!(o instanceof Node))  {//o.getClass().equals(Node.class
           throw new RuntimeException("类型异常");
       }
       int index = 0;
       //Node<E> node = new Node<E>((E)o);
       Node<E> node = (Node<E>)o;
       Node<E> flag = first;
       for(int i=0;i<size;i++) {
           if(node.item==flag.item) {
               return index;
           }
           flag = flag.next;
           index++;
       }
       return -1;
    }
}

Main Test

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
package ShuangXiangLinkedList;
import java.util.Random;
public class Test {
    public static void main(String[] args) {
       DoubleLinkedList<Integer> dll = new  DoubleLinkedList<Integer>();
       dll.add(11); //0
       dll.add(22); //1
       dll.add(33); //2
       dll.add(44); //3
       dll.add(55); //4
       dll.add(66); //5
       dll.add(77); //6
       //删除下标元素
       dll.remove(3);
       //将元素添加到表头
       dll.addFirst(9);
       //删除最后一个元素并返回其值
       int test1 = dll.removeLast();
       System.out.println("删除最后一个元素并返回值:"+test1);
       //通过节点node查找位置
       Random r = new Random();
       Node<Integer> test2 = new  Node<Integer>(55);
       int test3 = dll.indexOf(test2);
       System.out.println("查找第几个元素:"+test3);
       //打印双向链表
       for(int i = 0;i<dll.size;i++) {
           System.out.print(dll.get(i)+" ");
       }
    }
}

基于Calendar类实现万年历

Main WanNianLi

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
package course.pm.dos.homework;
import java.text.*;
import java.util.*;
public class WanNianLi {
    public static void main(String[] args)  throws ParseException {
       Scanner sc = new Scanner(System.in);
       Calendar cal =  Calendar.getInstance();
       System.out.println("请输入年份:");
       int year = sc.nextInt();
       System.out.println("请输入月份:");
       int month = sc.nextInt();
       Calendar calendar =  Calendar.getInstance();
       calendar.set(year, month-1, 1);
       int day =  calendar.get(calendar.DAY_OF_WEEK);//获取某年某月开始的第一天是周几
       int month_of_day =  calendar.getActualMaximum(calendar.DAY_OF_MONTH);//获取每个月有多少天
       System.out.println("星期日" + "\t" +  "星期一" + "\t" + "星期二" + "\t" + "星期三"  + "\t" + "星期四" + "\t" + "星期五" + "\t" +  "星期六");  
       int num = 1;
       for(int i=1;i<day+month_of_day;i++)  {
           if(i<day) {
               System.out.print("\t");
               continue;
           }
           System.out.print((num++)+"\t");
           if(i%7==0) {
               System.out.println();
           }
       }
    }
}

文件夹(目录)复制

Main WenJianFuZhi

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package course.pm.dos.homework;
import java.io.*;
public class WenJianFuZhi {
    public static void main(String[] args) {
       copyDir("G:\\YC74\\190713\\code\\cs","G:\\YC74\\190713\\code\\cd");
       System.out.println("复制完成");
    }
    
    public static void copyDir(String  ob,String obCopy) {
       File obf = new File(ob);
       File obfCopy = new File (obCopy);
       if(obf.exists()) {
           //存在
           if(obf.isDirectory()) {
               //是目录
               if(!(obfCopy.exists())) {
                  obfCopy.mkdirs();
               }
               File[] listFile =  obf.listFiles();
               for(int  i=0;i<listFile.length;i++) {
                  copyDir(listFile[i].getPath(),obCopy+"\\"+listFile[i].getName());
               }
           }else {
               //是文件
               Reader r = null;
               Writer w = null;
               try {
                  r = new FileReader(ob);
                  w = new  FileWriter(obCopy);
                  BufferedReader br = new  BufferedReader(r);
                  BufferedWriter bw = new  BufferedWriter(w);
                  String value = null;
                  while((value=br.readLine())!=null) {
                      bw.write(value);
                      bw.newLine();
                  }
                  bw.flush();
                  r.close();
                  w.close();
               } catch (IOException e) {
                  e.printStackTrace();
               }
           }
       }
    }
}

DOS copy方法的实现

Main DOS

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package course.pm.dos.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class DOS {
    public static void main(String[] args) throws IOException {
        System.out.println("HooySkyNet-DOS[Version : 1.0]");
        System.out.println("(c) 2019 Hooyantsing 保留所有权利");
        System.out.println("");
        //展示盘符
        showRoot();
        System.out.println("\n");
        //获取C盘
        File[] listRoots = File.listRoots();
        String path = listRoots[1].getPath();
        while(true) {
            showCRoot(path);
            String cmd = getCmd();
            if(cmd.equalsIgnoreCase("help")) {
                helpOp();
            }
            if(cmd.equalsIgnoreCase("dir")){
                dirOp(path);
            }
            if(cmd.endsWith(":")) {
                if(new File(cmd).exists()) {
                    path = cmd+"\\".toUpperCase();
                }else {
                    System.out.println("找不到指定的驱动器");
                }
            }
            if(cmd.startsWith("cd ") || cmd.startsWith("CD ")) {
                path = cdop(cmd,path);
            }
            if(cmd.startsWith("copy ")){
                copy(cmd,path);
            }
        }

    }
    //DOS copy的实现
    private static void copy(String cmd, String path) {
        String[] address = cmd.split(" ");
        File str = new File(address[1]);
        File strCopy = new File(address[2]);
        if(!(str.exists())){
            System.out.println("不存在 "+str.getName()+" 文件");
            return;
        }
        Reader re = null;
        Writer wr = null;
            Scanner sc = new Scanner(System.in);
            System.out.print("改写 "+strCopy.getName()+" 吗?<yes/no/all>");
            String flag = sc.nextLine();
            if(flag.equals("yes") || flag.equals("y")) {
                try {
                    re = new FileReader(path+"\\"+str);
                    wr = new FileWriter(path+"\\"+strCopy);
                    BufferedReader br = new BufferedReader(re);
                    BufferedWriter bw = new BufferedWriter(wr);
                    String value = null;
                    while((value=br.readLine())!=null) {
                        bw.write(value);
                        bw.newLine();
                }
                    bw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(re!=null) {
                        try {
                            re.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(wr!=null) {
                        try {
                            wr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("已经复制\t 1个文件。");
            }
    }
    private static String cdop(String cmd, String path) {
        //cd .
        if(cmd.equals("cd .")) {
            return path;
        }else if(cmd.equals("cd ..")){ //cd ..
            File f = new File(path);
            String parent = f.getParent();
            if(parent!=null) {
                return parent;
            }else {
                return path;
            }
        }else {//cd D:\AA
            String[] ss = cmd.split(" ");
            File f = new File(ss[1]);
            if(f.exists() && f.isDirectory()) {
                return f.getPath();
            }else {
                System.out.println("未能找到"+f.getName()+"目录");
                return path;
            }
        }
    }
    private static void dirOp(String path) {
        int dirCount = 0;
        int fileCount = 0;
        File f = new File(path);
        File[] childFiles = f.listFiles();
        for(int i=0;i<childFiles.length;i++) {
            File file = childFiles[i];
            if(file.isDirectory()) {
                dirCount++;
            }
            long time = file.lastModified();
            Date d = new Date(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
            System.out.print(sdf.format(d)+"\t"+(file.isFile()?"   ":"<DIR>"));
            if(file.isFile()) {
                fileCount++;
                long len = file.length();
                String changLength = changeLength(len);
                System.out.print("\t    "+changLength);    
            }else {
                System.out.print("\t");
            }
            System.out.print("\t\t"+file.getName());
            System.out.println();
        }
        System.out.println("\n\t\t文件:"+fileCount+"\t目录:"+dirCount+"\n\n");
    }
    private static String changeLength(long len) {
        String s = "";
        if(len/1024/1024==0) {
            if(len/1024==0) {
                s = len + "B";
            }else {
                s = len/1024 + "K";
            }
        }else {
            s = len/1024/1024 + "M";
        }
        return s;
    }
    private static void helpOp() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("help.txt")) ;
        String value = null;
        StringBuffer sb = new StringBuffer();
        while((value=br.readLine())!=null) {
            sb.append(value+"\n");
        }
        System.out.println(sb.toString());
        
    }
    private static String getCmd() {
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }
    private static void showCRoot(String path) {
        System.out.print(path+">");
        
    }
    private static void showRoot() {
        File[] listRoots = File.listRoots();
        for(int i=0;i<listRoots.length;i++) {
            System.out.print(listRoots[i].getPath()+"  ");
        }
    }
}
CATALOG