0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
 小马嘟嘟骑:3:0    4:1    1:1
0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
  投递牛客等公司
投递牛客等公司0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
 DNCBA-曹:==============实体类=================
package com.newcoder_10;
import java.io.Serializable;
public class Student implements Serializable {
    /**
     * 实体类:
     */
    private static final long serialVersionUID = 4888503192302777566L;
    private String id; // 学号字段
    private String name; // 名称
    private String age; // 年龄
    private String adderss; // 地址
    private String score; // 分数
    public Student() {
    }
    // 为了方便封装数据提供了有参构造
    public Student(String id, String name, String age, String adderss, String score) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.adderss = adderss;
        this.score = score;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getScore() {
        return score;
    }
    public void setScore(String score) {
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAdderss() {
        return adderss;
    }
    public void setAdderss(String adderss) {
        this.adderss = adderss;
    }
}
===============主程序===========
package com.newcoder_10;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class StudentManager {
    public static void main(String[] args) throws ClassNotFoundException, IOException {
        // 程序开始加载本地数据
        ArrayList<Student> array = new ArrayList<Student>();
        File file = new File("Date.txt");
        if (file.exists() && file.length() > 0) {
            // 本地数据存在则加载数据(判断大小是因为,如果空文件会报空指针异常)
            ArrayList<Student> list = load();
            array = list;
        } else {
            // 如果本地不存在数据,则创建文件
            file.createNewFile();
        }
        while (true) {
            // 主界面 1.打印表头 2.输入信息 3.
            System.out.println("--------欢迎使用学生成绩管理系统--------");
            System.out.println("1查询所有学生");
            System.out.println("2增加学生");
            System.out.println("3根据学号查询");
            System.out.println("4按学号排序查询");
            System.out.println("5按照分数排序查询");
            System.out.println("6退出");
            // 录入信息
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你想要选择的功能:");
            String choice = sc.nextLine();
            // 选择
            switch (choice) {
            case "1":
                // 1查询所有学生
                findAllStudent(array);
                break;
            case "2":
                // 2增加学生
                addStudent(array);
                store(array);
                break;
            case "3":
                // 3根据学号查询
                selectById(array);
                break;
            case "4":
                // 按学号排序查询
                orderById(array);
                break;
            case "5":
                // 5按照分数排序查询
                orderByScore(array);
                break;
            case "6":
                // 退出
                store(array);
                System.out.println("谢谢你的使用");
                System.exit(0);
                break;
            default:
                System.out.println("此功能暂未开放");
                break;
            }
        }
    }
    // 根据分数排序
    private static void orderByScore(ArrayList<Student> array) {
        if (array.size() <= 0) {
            System.out.println("数据库为空!!");
            return;
        }
        // 为了保护原始数据,采用克隆副本进行排序
        List<Student> cList = (ArrayList<Student>) array.clone();
        // 排序按照分数
        Collections.sort(cList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                Double id1 = new Double(s1.getScore());
                Double id2 = new Double(s2.getScore());
                Integer result = id1 > id2 ? 1 : id1 < id2 ? -1 : 0;
                return result;
            }
        });
        // 打印输出分数
        System.out.println("学号\t\t姓名\t年龄\t地址\t成绩");
        for (int i = 0; i < cList.size(); i++) {
            Student st = cList.get(i);
            System.out.println(st.getId() + "\t\t" + st.getName() + "\t" + st.getAge() + "\t" + st.getAdderss() + "\t"
                    + st.getScore());
        }
    }
    // 根据学号排序
    private static void orderById(ArrayList<Student> array) {
        if (array.size() <= 0) {
            System.out.println("数据库为空!!");
            return;
        }
        List<Student> cList = (ArrayList<Student>) array.clone();
        // Collections.copy(array, cList);
        Collections.sort(cList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                Integer id1 = Integer.parseInt(s1.getId());
                Integer id2 = Integer.parseInt(s2.getId());
                Integer result = id1 > id2 ? 1 : id1 < id2 ? -1 : 0;
                return result;
            }
        });
        System.out.println("学号\t\t姓名\t年龄\t地址\t成绩");
        for (int i = 0; i < cList.size(); i++) {
            Student st = cList.get(i);
            System.out.println(st.getId() + "\t\t" + st.getName() + "\t" + st.getAge() + "\t" + st.getAdderss() + "\t"
                    + st.getScore());
        }
    }
    // 根据学号查询
    private static void selectById(ArrayList<Student> array) {
        System.out.println("请输入要查询的学号");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();
        if (id == null) {
            System.out.println("输入为空!");
            return;
        }
        if (array.size() <= 0) {
            System.out.println("数据库为空!!");
            return;
        }
        boolean flag = false;
        Student st = null;
        for (int i = 0; i < array.size(); i++) {
            st = array.get(i);
            if (id.equals(st.getId())) {
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println(st.getId() + "\t\t" + st.getName() + "\t" + st.getAge() + "\t" + st.getAdderss());
        } else {
            System.out.println("数据库中未查询到数据!请修改学号");
        }
    }
    // 存储数据
    public static void store(ArrayList<Student> array) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Date.txt"));
        oos.writeObject(array);
        oos.flush();
        oos.close();
    }
    // 加载数据:
    public static ArrayList<Student> load() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Date.txt"));
        Object obj = ois.readObject();
        ois.close();
        ArrayList<Student> list = (ArrayList<Student>) obj;
        return list;
    }
    // 查询所有学生信息方法
    public static void findAllStudent(ArrayList<Student> array) {
        if (array.size() <= 0) {
            System.out.println("当前管理系统暂时没有信息");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t地址\t成绩");
            for (int i = 0; i < array.size(); i++) {
                Student st = array.get(i);
                System.out.println(st.getId() + "\t\t" + st.getName() + "\t" + st.getAge() + "\t" + st.getAdderss()
                        + "\t" + st.getScore());
            }
        }
    }
    // 添加学生信息的方法
    public static void addStudent(ArrayList<Student> array) {
        // 键盘录入信息
        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入学号");
            boolean flag = false;
            id = sc.nextLine();
            // 添加判断是否已经有了这个学号
            for (int i = 0; i < array.size(); i++) {
                Student s = array.get(i);
                String userId = s.getName();
                if (userId.equals(id)) {
                    flag = true;
                    break;
                }
            }
            if (flag == true) {
                System.out.println("当前学号已经被占用,请重新输入");
                // continue;
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入年龄");
        String age = sc.nextLine();
        System.out.println("请输入住址");
        String adderss = sc.nextLine();
        System.out.println("请输入成绩");
        String score = sc.nextLine();
        // 创建学生对象
        Student st = new Student(id, name, age, adderss, score);
        // 添加到集合中
        array.add(st);
    }
}
0 点赞 评论 收藏   
分享
  投递牛客等公司
投递牛客等公司0 点赞 评论 收藏   
分享
 cj-123:#include "BSTree.h"
#include<iostream>
using namespace std;
//前序遍历二叉树
template<class T>
void BSTree<T>::preOrder(BSTNode<T> *tree) const
{
 if(tree!=nullptr)
 {
 cout<<tree->key<<" ";
 preOrder(tree->left);
 preOrder(tree->right);
 }
}
template<class T>
void BSTree<T>::preOrder()
{
 preOrder(mRoot);
}
//中序遍历二叉树
template<class T>
void BSTree<T>::inOrder(BSTNode<T> *tree) const
{
 if(tree!=nullptr)
 {
 inOrder(tree->left);
 cout<<tree->key<<" ";
 inOrder(tree->right);
 }
}
template<class T>
void BSTree<T>::inOrder()
{
 inOrder(mRoot);
}
//后序遍历二叉树
template<class T>
void BSTree<T>::postOrder(BSTNode<T> *tree) const
{
 if(tree!=nullptr)
 {
 postOrder(tree->left);
 postOrder(tree->right);
 cout<<tree->key<<" ";
 }
}
template<class T>
void BSTree<T>::postOrder()
{
 postOrder(mRoot);
}
//递归实现查找二叉树中键值为key的节点
template<class T>
BSTNode<T>* BSTree<T>::search(BSTNode<T> *x, T key)const
{
 if(x->key==key)
 return x;
 else if(x->key>key)
 return search(x->left,key);
 else
 return search(x->right,key);
}
template<class T>
BSTNode<T>* BSTree<T>::search(T key)
{
 return search(mRoot,key);
}
//非递归实现查找二叉树中键值为key的节点
template<class T>
BSTNode<T>* BSTree<T>::iterativeSearch(BSTNode<T> *x, T key)const
{
 while(x!=nullptr)
 {
 if(x->key==key)
 return x;
 else if(x->key>key)
 x=x->left;
 else
 x=x->right;
 }
 return x;
}
template<class T>
BSTNode<T>* BSTree<T>::iterativeSearch(T key)
{
 return iterativeSearch(mRoot,key);
}
//查找最小节点,返回最小节点
template<class T>
BSTNode<T>* BSTree<T>::minimum(BSTNode<T> *tree)
{
 while(tree->left!=nullptr)
 {
 tree=tree->left;
 }
 return tree;
}
template<class T>
T BSTree<T>::minimum()
{
 return minimum(mRoot)->key;
}
//查找最大节点,返回最大节点
template<class T>
BSTNode<T>* BSTree<T>::maximum(BSTNode<T> *tree)
{
 while(tree->right!=nullptr)
 {
 tree=tree->right;
 }
 return tree;
}
template<class T>
T BSTree<T>::maximum()
{
 return maximum(mRoot)->key;
}
//插入节点到二叉搜索树
template<class T>
void BSTree<T>::insert(BSTNode<T> *&tree, BSTNode<T> *z)
{
 if(tree==nullptr)
 {
 tree=z;
 return;
 }
 BSTNode<T>* p=nullptr;
 BSTNode<T>* tmp=tree;
 while(tmp)
 {
 if(tmp->key==z->key)
 return;
 p=tmp;
 if(tmp->key>z->key)
 {
 tmp=tmp->left;
 }
 else
 {
 tmp=tmp->right;
 }
 }
 if(p->key>z->key)
 p->left=z;
 else
 p->right=z;
}
template<class T>
void BSTree<T>::insert(T key)
{
 BSTNode<T>* node=new BSTNode<T>(key);
 insert(mRoot,node);
}
//删除节点
template<class T>
BSTNode<T>* BSTree<T>::remove(BSTNode<T> *&tree, BSTNode<T> *z)
{
 if(tree==nullptr) return;
 BSTNode<T>* pcur=tree;
 while(pcur!=nullptr && pcur->key!=z->key)
 {
 if(pcur->key>z->key)
 pcur=pcur->left;
 else
 pcur=pcur->right;
 }
 if(pcur==nullptr)
 return;
 if(!pcur->left && !pcur->right)
 {
 delete pcur;
 pcur=nullptr;
 return;
 }
 if(pcur->left && pcur->right)
 {
 BSTNode<T>* tmp=pcur->right;
 while(tmp->left)
 tmp=tmp->left;
 if(pcur->right=tmp)
 {
 pcur->right=tmp->right;
 delete tmp;
 tmp=nullptr;
 return;
 }
 BSTNode<T>* node=pcur;
 pcur=tmp;
 tmp=node;
 delete tmp;
 tmp=nullptr;
 return;
 }
 if(pcur->left)
 {
 BSTNode<T>* p=pcur->parent;
 if(p==nullptr)
 {
 tree=pcur->left;
 delete pcur;
 pcur=nullptr;
 return;
 }
 if(p->right==pcur)
 {
 p->right=pcur->left;
 }
 if(p->left==pcur)
 {
 p->left=pcur->left;
 }
 return;
 }
 if(pcur->right)
 {
 BSTNode<T>* p=pcur->parent;
 if(p==nullptr)
 {
 tree=pcur->right;
 delete pcur;
 pcur=nullptr;
 return;
 }
 if(p->right==pcur)
 {
 p->right=pcur->right;
 }
 if(p->left==pcur)
 {
 p->left=pcur->right;
 }
 return;
 }
}
template<class T>
void BSTree<T>::remove(T key)
{
 BSTNode<T>* node=new BSTNode<T>(key);
 remove(mRoot,node);
}
//销毁二叉树
template<class T>
void BSTree<T>::destory(BSTNode<T> *&tree)
{
 if(tree)
 {
 BSTNode<T>* l=tree->left;
 BSTNode<T>* r=tree->right;
 delete tree;
 destory(l);
 destory(r);
 }
}
template<class T>
void BSTree<T>::destory()
{
 destory(mRoot);
}
//查找节点x的后继节点
template<class T>
BSTNode<T>* BSTree<T>::successor(BSTNode<T> *x)
{
 if(x==nullptr || x->right==nullptr) return nullptr;
 BSTNode<T>* node=x->right;
 while(node->left)
 node=node->left;
 return node;
}
//查找节点x的前驱节点
template<class T>
BSTNode<T>* BSTree<T>::predecessor(BSTNode<T> *x)
{
 if(x==nullptr) return nullptr;
 if(x->left)
 {
 x=x->left;
 while(x->right)
 x=x->right;
 return x;
 }
 BSTNode<T>* p=x->parent;
 if(p==nullptr) return p;
 if(p->right==x) return p;
 if(p->left==x)
 {
 BSTNode<T>* q=p->parent;
 while(q->right!=p)
 {
 p=q;
 q=p->parent;
 }
 return q;
 }
}
0 点赞 评论 收藏   
分享
 和三石sama一起养...:别问,问就是老干爹
0 点赞 评论 收藏   
分享
 0 点赞 评论 收藏   
分享
 温酒写bug:学学习&&投投简历
0 点赞 评论 收藏   
分享
 创作者周榜
更多 
 关注他的用户也关注了: