Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    có bài tập nhờ a e giúp

    import java.io.*;
    import java.util.*;
    class Person
    {String name;
    int age;
    Person(String xName, int xAge)
    {name=xName; age = xAge;
    }
    public String toString()
    {return(name+", " + age);
    }
    }

    class Node
    {Person info;
    Node next;
    Node(Person x, Node p)
    {info=x;next=p;
    }
    Node(Person x)
    {this(x,null);
    }
    }

    class PersonList
    {Node head,tail;

    PersonList() {head=tail=null;}

    boolean isEmpty()
    {return(head==null);
    }

    void clear() {head=tail=null;}

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    int size()
    {int k=0;
    Node p = head;
    while(p!=null)
    {k++;
    p = p.next;
    }
    return(k);
    }

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    void visit(Node p)
    {System.out.println(" " + p.info);
    }

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    void fvisit(Node p, RandomAccessFile f) throws Exception
    {f.writeBytes(p.info + "
    ");
    }

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    void traverse()
    {Node p=head;
    while(p!=null)
    {visit(p);
    p=p.next;
    }
    //System.out.println();
    }

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    void ftraverse(String fname) throws Exception
    {File g = new File(fname);
    if(g.exists()) g.delete();
    RandomAccessFile f;
    f = new RandomAccessFile(fname, "rw");
    Node p = head;
    while(p!=null)
    {fvisit(p,f); // You will use this statement to write information of the node p to the file
    p=p.next;
    }
    f.close();
    }

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    void viewFile(String fname) throws Exception
    {File g = new File(fname);
    if(!g.exists())
    {System.out.println(" The file " + fname + " does not exist!");
    return;
    }
    RandomAccessFile f;
    String s;
    f = new RandomAccessFile(fname, "r");
    System.out.println(" Content of the file " + fname + ":");
    while((s = f.readLine()) != null ) System.out.println(" " + s);
    f.close();
    }

    // YOU DO NOT NEED TO EDIT THE FOLLOWING FUNCTION
    void loadFileToLast(String fname) throws Exception
    {RandomAccessFile f;
    String s,sName,sAge;
    String xName; int xAge;
    StringTokenizer t;
    clear();
    f = new RandomAccessFile(fname, "r");
    while(true)
    {s = f.readLine();
    if(s==null) break;
    s = s.trim();
    if(s.length()<3) continue;
    t = new StringTokenizer(s,",");
    sName = t.nextToken();
    sAge = t.nextToken();
    xName = sName.trim();
    xAge = Integer.parseInt(sAge.trim());
    addLast(xName,xAge);
    }
    f.close();
    }
    //================================================== ===============
    // YOU SHOULD COMPLETE THE FOLLOWING FUNCTIONS
    Node search(String xName)
    {//You should write here appropriate statements to complete this function.
    return(null);
    }

    void addLast(Person x)
    {//You should write here appropriate statements to complete this function.
    }
    void addLast(String xName, int xAge) // xAge must be less than 30
    {//You should write here appropriate statements to complete this function.
    }

    void f1() throws Exception
    {/* You do not need to edit this function. Your task is to complete addLast functions
    above only. You should check data when using addLast functions to add data to
    the end of the list. However, because the bus1.txt does not contain invalid data,
    you get 1 mark even if you do not check data.
    */
    clear();
    loadFileToLast("person1.txt");
    ftraverse("f1.txt");
    }

    void f2() throws Exception
    {/* You do not need to edit this function. Your task is to complete addLast functions
    above only. You should check data when using addLast functions to add data to
    the end of the list. Because the bus2.txt contains invalid data, thus you will get 0
    mark if you do not check data.
    */
    clear();
    loadFileToLast("person2.txt");
    ftraverse("f2.txt");
    }

    void f3() throws Exception // display all persons with age=xAge to the file f3.txt
    {int xAge = 21;// xAge is 21, but can be changed to other value
    clear();
    loadFileToLast("person3.txt");

    File g = new File("f3.txt");
    if(g.exists()) g.delete();
    RandomAccessFile f;
    f = new RandomAccessFile("f3.txt", "rw");
    /* You should write here appropriate statements to complete this function.
    You use statement:
    fvisit(p,f);
    to write the node p to the file f3.txt
    (You can read the ftraverse(String fname) function to know how to use the
    fvisit(p,f);
    statement).
    */
    f.close();
    }


    void f4() throws Exception // delete the first node having name=xName
    {String xName = "N11";
    clear();
    loadFileToLast("person1.txt");
    //You should write here appropriate statements to complete this function.
    ftraverse("f4.txt");
    }

    void f5() throws Exception // sort by name
    {//You should write here appropriate statements to complete this function.
    clear();
    loadFileToLast("person3.txt");
    //You should write here appropriate statements to complete this function.
    ftraverse("f5.txt");
    }

    }

    /*=============================================
    YOU MUST NOT EDIT THE FOLLOWING STATEMENTS
    =============================================*/
    class Main
    {public static void main(String args[]) throws Exception
    {Scanner sc = new Scanner(System.in);
    int choice;
    Marking w = new Marking();
    PersonList t = new PersonList();
    String xName, xDriver;
    Person x;
    int k;
    while(true)
    {System.out.println();
    System.out.println(" 1. Test f1 (1 mark)");
    System.out.println(" 2. Test f2 (1 mark)");
    System.out.println(" 3. Test f3 (1 mark)");
    System.out.println(" 4. Test f4 (1 mark)");
    System.out.println(" 5. Test f5 (1 mark)");
    System.out.println(" 9. Test all cases");
    System.out.println(" 10. View your grade");
    System.out.println(" 0. Exit ");
    System.out.print(" Your selection (0 -> 10): ");
    choice = sc.nextInt();
    if(choice==0) break;
    switch(choice)
    {
    case 1: System.out.println("
    1. Test f1 (1 mark)");
    // (1)
    t.f1();
    System.out.println("Your output:");
    t.viewFile("f1.txt");
    System.out.println("Correct output:");
    t.viewFile("R_f1.txt");
    break;

    case 2: System.out.println("
    2. Test f2 (1 mark)");
    // (2)
    t.f2();
    System.out.println("Your output:");
    t.viewFile("f2.txt");
    System.out.println("Correct output:");
    t.viewFile("R_f2.txt");
    break;

    case 3: System.out.println("
    3. Test f3 (1 mark)");
    // (3)
    t.f3();
    System.out.println("Your output:");
    t.viewFile("f3.txt");
    System.out.println("Correct output:");
    t.viewFile("R_f3.txt");
    break;

    case 4: System.out.println("
    4. Test f4 (1 mark)");
    // (4)
    t.f4();
    System.out.println("Your output:");
    t.viewFile("f4.txt");
    System.out.println("Correct output:");
    t.viewFile("R_f4.txt");
    break;

    case 5: System.out.println("
    5. Test f5 (1 mark)");
    // (5)
    t.f5();
    System.out.println("Your output:");
    t.viewFile("f5.txt");
    System.out.println("Correct output:");
    t.viewFile("R_f5.txt");
    break;

    case 9:
    t.f1();
    t.f2();
    t.f3();
    t.f4();
    t.f5();
    break;

    case 10: w.marking();
    }
    }
    System.out.println();
    }
    }
    //==============================================


    tình hình có bài tập này,em cũng chưa hiểu cách code lắm,nhờ a em chỉ giùm cách code.hoặc là hướng dẫn em làm đc k?

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    person1

    N01, 10
    N07, 15
    N08, 17
    N02, 18
    N03, 11
    N05, 24
    N04, 29

    person2

    N01, 10
    N07, 15
    N11, 12
    N08, 17
    N11, 11
    N05, 34
    N11, 29
    N07, 33

    person3

    N01, 10
    N07, 21
    N08, 17
    N02, 18
    N03, 21
    N05, 24
    N04, 21

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Đọc luật lệ tham gia + Cách thức post bài đi đã
    Rồi sẽ có người giúp
    :-w

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •