数据结构课程实验——学生管理系统——源代码

  • 时间:
  • 浏览:
  • 来源:互联网

源代码

screen类(用来与用户交互)

import java.util.*;

class screen
{
    void maindan()
    {
        student1 pg1=new student1();
        pg1.add();
        int qp=1;
        while(qp!=0) {
            Scanner sc=new Scanner(System.in);
            System.out.println("输入一个数字进行操作");
            System.out.println("1.用学号查找学生");
            System.out.println("2.修改学生成绩");
            System.out.println("3.查看所有学生成绩");
            System.out.println("4.用学号删除学生");
            System.out.println("5.排序查看学生");
            int s=sc.nextInt();
            switch (s) {
                case 1:
                    pg1.cha();
                    break;
                case 2:
                    pg1.gai();
                    break;
                case 3:
                    pg1.look();
                    break;
                case 4:
                    pg1.delete();
                    break;
                case 5:
                    pg1.paixu();
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
            System.out.println("请输入一个数字,如果为0,则退出系统");
            int ppgr=sc.nextInt();
            qp=ppgr;
        }
    }
}

student类

class student
{
    int score1;
    int score2;
    int score3;
    int score4;
    int xuehao;
    String xingming;
    String zhuanye;
    student(int s1,int s2,int s3,int s4,int x,String xin,String zhuanye1)
    {
        score1=s1;
        score2=s2;
        score3=s3;
        score4=s4;
        xuehao=x;
        xingming=xin;
        zhuanye=zhuanye1;
    }
}

student1类(存储student的信息,并有增删改查函数)

class student1
{
    ArrayList<student> aa=new ArrayList<student>();
    void add()
    {
        Scanner sc=new Scanner(System.in);
        int p=1;
        while(p!=0){
            System.out.println("输入学生的姓名:");
            String s5=sc.next();
            System.out.println("输入学生的专业");
            String s6=sc.next();
            System.out.println("请输入学生的学号");
            int s7=sc.nextInt();
            System.out.println("输入学生的成绩一");
            int s1=sc.nextInt();
            System.out.println("输入学生的成绩二");
            int s2=sc.nextInt();
            System.out.println("输入学生的成绩三");
            int s3=sc.nextInt();
            System.out.println("输入学生的成绩四");
            int s4=sc.nextInt();
            aa.add(new student(s1,s2,s3,s4,s7,s5,s6));
            System.out.println("请输入一个数字,如果为0则退出输入学生");
            int gg=sc.nextInt();
            p=gg;
        }
    }


    void cha()
    {
        Scanner sc1=new Scanner(System.in);
        int g=1;
        int p=0;
        int t=aa.size();
        while(g!=0)
        {
            System.out.println("请输入学生的学号");
            int gg=sc1.nextInt();
            for(int i=0;i<=aa.size()-1;i++)
            {
                if(gg==aa.get(i).xuehao)
                {
                    System.out.println("已成功查找到");
                    System.out.println("姓名为"+aa.get(i).xingming+"学号为"+aa.get(i).xuehao+"专业为"+aa.get(i).zhuanye+"成绩一"+aa.get(i).score1+"成绩二"+aa.get(i).score2+"成绩三"+aa.get(i).score3+"成绩四"+aa.get(i).score4);
                    break;
                }
                if(gg!=aa.get(i).xuehao)
                {
                    t--;
                }
                if(t==0){
                    System.out.println("请重新输入学号,未查找到");
                    cha();
                }
            }
            System.out.println("输入一个数字,如果是0退出查找");
            int g1=sc1.nextInt();
            g=g1;
        }
    }


    void gai()
    {
        int pp=1;
        int x=aa.size();
        boolean tt=false;
        Scanner sc2=new Scanner(System.in);
        while(pp!=0)
        {
            System.out.println("请输入学生的学号");
            int g2=sc2.nextInt();
            for(int ii=0;ii<=aa.size()-1;ii++) {
                x--;
                if (g2 == aa.get(ii).xuehao) {
                    System.out.println("以查找到学生");
                    System.out.println("请输入学生成绩1");
                    int s1 = sc2.nextInt();
                    System.out.println("请输入学生成绩2");
                    int s2 = sc2.nextInt();
                    System.out.println("请输入学生成绩3");
                    int s3 = sc2.nextInt();
                    System.out.println("请输入学生成绩4");
                    int s4 = sc2.nextInt();
                    aa.get(ii).score1 = s1;
                    aa.get(ii).score2 = s2;
                    aa.get(ii).score3 = s3;
                    aa.get(ii).score4 = s4;
                    tt=true;
                    break;
                }
                if(x==0&&tt==false){
                    System.out.println("请重新输入学号,未查找到");
                    cha();
                }
            }
            System.out.println("请输入一个数字,如果为0则退出修改学生成绩");
            int pp1=sc2.nextInt();
            pp=pp1;
        }
    }



    void look()
    {
        Scanner sc3=new Scanner(System.in);
        int gg=1;
        while(gg!=0) {
            for (int ii = 0; ii <= aa.size() - 1; ii++) {
                System.out.println("姓名为" + aa.get(ii).xingming + "学号为" + aa.get(ii).xuehao + "专业为" + aa.get(ii).zhuanye + "成绩一" + aa.get(ii).score1 + "成绩二" + aa.get(ii).score2 + "成绩三" + aa.get(ii).score3 + "成绩四" + aa.get(ii).score4);
            }
            System.out.println("输入一个数字,如果为0退出查看");
            int sgg=sc3.nextInt();
            gg=sgg;
        }
    }


    void delete()
    {
        int bianli=aa.size();
        boolean tp=false;
        int xx=aa.size();
        Scanner sc3=new Scanner(System.in);
        int t=aa.size(),pg=1;
        while(pg!=0)
        {
            System.out.println("请输入要删除学生的学号");
            int xue=sc3.nextInt();

            for(int i=0;i<=aa.size()-1;i++)
            {
                bianli--;
                if(xue==aa.get(i).xuehao&&t!=0)
                {
                    aa.remove(i);
                    t--;
                    System.out.println("已成功删除该学生");
                    tp=true;
                }
                if(t==0)
                {
                    System.out.println("已全部删除");
                    break;
                }
                if(bianli==0&&tp!=true){
                    System.out.println("未查找到该学生,请重新输入");
                    delete();
                }
            }
            System.out.println("请输入一个数字,如果为0退出删除");
            int gh=sc3.nextInt();
            pg=gh;
        }
    }

    void paixu() {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        System.out.println("1.进行希尔排序");
        System.out.println("2.进行冒泡排序");
        System.out.println("请输入数字");
        switch(t)
        {
            case 1:
                System.out.println("进行希尔排序");
                xierpaixu1();
                break;
            case 2:
                        maopao();
                            break;

            default:
                              paixu();


        }

        System.out.println("根据成绩1排序后的结果");
        for (int jjj = 0; jjj <= aa.size() - 1; jjj++) {
            System.out.println("姓名" + aa.get(jjj).xingming + " 学号" + aa.get(jjj).xuehao + " 专业" + aa.get(jjj).zhuanye + "  成绩1 " + aa.get(jjj).score1 + "  成绩2 " + aa.get(jjj).score2 + "  成绩3 " + aa.get(jjj).score3 + "  成绩4 " + aa.get(jjj).score4);
        }

    }

    void maopao(){
        for (int i = 0; i <= aa.size() - 2; i++) {
            for (int j = 1; j <= aa.size() - 1; j++) {
                if (aa.get(j - 1).score1 < aa.get(j).score1) {
                    int pq = aa.get(j - 1).score1;
                    aa.get(j - 1).score1 = aa.get(j).score1;
                    aa.get(j).score1 = pq;

                    int pg1 = aa.get(j - 1).score2;
                    aa.get(j - 1).score2 = aa.get(j).score2;
                    aa.get(j).score2 = pg1;

                    int pg2 = aa.get(j - 1).score3;
                    aa.get(j - 1).score3 = aa.get(j).score3;
                    aa.get(j).score3 = pg2;

                    int pg3 = aa.get(j - 1).score4;
                    aa.get(j - 1).score4 = aa.get(j).score4;
                    aa.get(j).score4 = pg3;

                    String pg4 = aa.get(j - 1).xingming;
                    aa.get(j - 1).xingming = aa.get(j).xingming;
                    aa.get(j).xingming = pg4;

                    String pg5 = aa.get(j - 1).zhuanye;
                    aa.get(j - 1).zhuanye = aa.get(j).zhuanye;
                    aa.get(j).zhuanye = pg5;

                    int pg6 = aa.get(j - 1).xuehao;
                    aa.get(j - 1).xuehao = aa.get(j).xuehao;
                    aa.get(j).xuehao = pg6;
                }
            }
        }


        System.out.println("根据成绩1排序后的结果");
        for (int jjj = 0; jjj <= aa.size() - 1; jjj++) {
        System.out.println("姓名" + aa.get(jjj).xingming + " 学号" + aa.get(jjj).xuehao + " 专业" + aa.get(jjj).zhuanye + "  成绩1 " + aa.get(jjj).score1 + "  成绩2 " + aa.get(jjj).score2 + "  成绩3 " + aa.get(jjj).score3 + "  成绩4 " + aa.get(jjj).score4);
        }

        }




    void xierpaixu1()//希尔排序
    {
        student temp=new student(1,2,3,4,1,"ui","er");//这是变量
        int d,ii,jj;
        int n=aa.size()-1;
        for(d=n/2;d>=1;d=d/2)//d=5,3,1,进行分组
        {
            for(ii=d;ii<=n;ii++)
            {
                temp=aa.get(ii);
            }
            for(jj=ii-d;jj>=0&&temp.score1>aa.get(jj).score1;jj-=d)
            {
                aa.set(jj+d,aa.get(jj));
            }
            aa.set(jj+d,temp);
        }
    }


    public void heapSort(ArrayList<student> list){//建立初始根堆,父节点要大于两个子节点或小于
        int i = 0;
        int n=list.size()-1;
        //初始建堆,从最后一个分支结点至根结点
        for (i = n/2; i >= 1; i--) {
            Sift(list, i, n);
        }
        //重复执行移走堆顶及重建堆的操作
        for (i=0; i<n; i++) {
            //交换0和末尾(n-i)的位置
            student temp=list.get(0);
            list.set(0,list.get(n-i));
            list.set(n-i,temp);
            Sift(list, 0, n-i-1);
        }
    }


    void Sift(ArrayList<student> list, int k, int m)
    {
        //i指向被筛选结点,j指向结点i的左孩子
        int i = k, j = 2 * i;
        if(i==0){//特殊情况
            j=1;
        }
        while (j <= m) {//j指向更小的结点,所以根结点和j比就行了
            //比较i的左右孩子,j指向两者中的较小者
            if (j < m &&list.get(j).score1< list.get(j+1).score1) {
                j++;
            }
            //若根结点已经小于左右孩子中的较小者
            if (list.get(i).score1 < list.get(j).score1) {
                break;
            } else {
                student temp=list.get(i);//如果不是这样就交换
                list.set(i,list.get(j));
                list.set(j,temp);
                //被筛结点位于原来结点j的位置
                i = j; j = 2 * i;
            }
        }
    }


    void quicksort(ArrayList<student> pp ,int left,int right){//快速排序
        if (left > right) {//要求从大到小   i        j
                           //              小       大
            return;
        }
        int i = left;
        int j = right;
        student t = aa.get(left);

        while (i != j){
            while (aa.get(j).score1 <= t.score1&& (j > i)) {//j没找到一个比i大的,就往左一步
                j--;
            }
            while (aa.get(i).score1 >= t.score1&& (j > i)) {//i没找到一个比j小的,就往右一步
                i++;
            }
            //交换位置
            if (i < j) {
                student temp=aa.get(i);
                aa.set(i,aa.get(j));
                aa.set(j,temp);
            }
        }
        aa.set(left,aa.get(i));
        aa.set(i,t);

        //递归左子序列
        quicksort(aa,left, --i);
        //递归右子序列
        quicksort(aa,++j, right);
        return;
    }


    public void duplexSort(ArrayList<student> li){//双向冒泡
        int left=0,right = li.size()-1;
        while(left<right){//小的在前面,大的在后面s

            //左侧扫描
            for(int j = left+1; j <= right; j++) {//右边移
                if(li.get(left).score1<li.get(j).score1) {
                    student temp=li.get(left);
                    li.set(left,li.get(j));
                    li.set(j,temp);
                }
            }
            left++;

            if(left>=right){
                break;
            }

            //右侧扫描
            for(int j = right-1; j >= left; j--) {//右边移
                if(li.get(right).score1>li.get(j).score1) {
                    student temp=li.get(right);
                    li.set(right,li.get(j));
                    li.set(j,temp);
                }
            }
            right--;
        }
    }
}

main函数

public class ww{
    public static void main(String[] args)
    {
        screen s=new screen();
        s.maindan();
    }
}

本文链接http://www.dzjqx.cn/news/show-617095.html