数据结构实验1

问题描述】

  参加计算机设计大赛的n个学校编号为1~n,赛事分成m个项目,项目的编号为1~m.比赛获奖按照得分降序,取前三名,写一个统计程序产生各种成绩单和得分报表。

【基本要求】

1)每个比赛项目至少有10支参赛队;每个学校最多有6支队伍参赛;

2)能统计各学校的总分;

3)可以按照学校编号或名称,学校的总分、各项目的总分排序输出;

4)可以按学校编号查询学校某个项目的获奖情况;可以按项目编号查询取得前三名的学校;

5)数据存入文件并能随时查询

【设计要求】

1)输入数据形式和范围:可以输入学校的名称,赛事项目的名称。

2)输出形式:有中文提示,各学校分数为整数

3)界面要求:交互设计要合理,每个功能可以设立菜单,根据提示,可以完成相关功能的要求。

4)存储结构:学生自己根据系统功能要求自己设计,但是赛事相关数据要存储在文件中。

【测试数据】

  要求使用全部合法数据,整体非法数据,局部非法数据。进行程序测试,以保证程序的稳定。

【实现提示】

  假设3<赛事项目数量<=10,学校名称长度不超过20个字符。每个赛事结束时,将其编号、名称输入,并依次输入参赛学校编号、学校名称和成绩。

计思路
考虑到本系统数据结构是线性表,故考虑使用java中的ArrayList进行数据的增删改查,同时可以使用ArrayList的一系列内部方法进行操作,使系统开发相对简洁。同时用户登录模块也可以使用ArrayList进行编辑。

值得一提的是,在之前的学习中,发现ArrayList配getInstance使用可以完美契合在外部类对原始数据的一系列操作,希望在本次系统的开发过程中也可以发挥作用。

对于数据的存储,可以调用BufferedWriter系列功能。

在排序模块中,或将使用基础的直接排序或冒牌排序等功能。

针对4.2中的问题,问题1初步考虑按学校编号进行二次排序;问题2初步考虑单独对队伍创建ArrayList进行不同赛事结果的存储。

总体思路:不同数据组使用不同的List,各功能也创建不同的类,最后集成在main方法中。

import java.sql.SQLOutput;
import java.util.*;

class test {
    static int m;
    //项目数
    static int n;
    //学校数
    static Project[] project = new Project[10];
    //项目
    static School[] school = new School[10];
    //学校


    static class Project {
        int projectCode;
        //项目编码
        String projectName;
        //项目名称
        int teamnum;
        //参赛队伍数量
        int[] jointeam = new int[10];
        //参赛队伍编号
        int[] score = new int[10];
        //参赛队伍成绩
    }

    static class School {
        int schoolCode;
        //学校编码
        String schoolName;
        //学校名称
        int sum;
        //项目总分
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入项目的数目:");
        m = sc.nextInt();
        while(m<=3||m>10){ System.out.println("输入有误!");
            System.out.println("请重新输入");
            Scanner s = new Scanner(System.in);
            m = s.nextInt();
        }
        while(m>0&&m<=10){
            System.out.println("输入成功!一共有"+m+"个项目");
            break;
        }
        System.out.println("请输入学校的数目:");
        n = sc.nextInt();
        if (n <= 0) {
            System.out.println("输入有误!");
            System.out.println("请重新输入");
            Scanner c = new Scanner(System.in);
            n = c.nextInt();
        }
        while (true) {
            System.out.println("**********************");
            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("0.退出");
            System.out.println("**********************");
            System.out.println("请输入你的选择:");

            String nothing = sc.nextLine();
            int option = sc.nextInt();
            if (option != 0 && option != 1 && option != 2 && option != 3 && option != 4 && option != 5) {
                System.out.println("输入有误!");
                System.exit(0);
            }

            switch (option) {
                case 1:
                    addInformation();
                    break;
                case 2:
                    printByCodes();
                    break;
                case 3:
                    printByScore();
                    break;
                case 4:
                    findByCodes();
                    break;
                case 5:
                    getTopThree();
                    break;
                case 0:
                    System.out.println("谢谢使用");
                    System.exit(0);
            }
        }
    }

    public static void addInformation() {
        //录入信息
        //录入项目
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++) {
            project[i] = new Project();
            project[i].projectCode = i + 1;
            System.out.println("请输入第" + (i + 1) + "个项目的名称:");
            project[i].projectName = sc.nextLine();
            System.out.println("添加成功!");
        }
        for (int i = 0; i < m; i++) {
            System.out.println("项目编号" + project[i].projectCode +
                    "项目名称" + project[i].projectName);
        }
        //录入学校
        for (int i = 0; i < n; i++) {
            school[i] = new School();
            school[i].schoolCode = i + 1;
            System.out.println("请输入第" + (i + 1) + "个学校的名称:");
            school[i].schoolName = sc.nextLine();
            System.out.println("添加成功!");
        }
        for (int i = 0; i < n; i++) {
            System.out.println("学校编号" + school[i].schoolCode +
                    "学校名称" + school[i].schoolName);
        }
        for (int i = 0; i < m; i++) {
            System.out.println("请输入参加" + project[i].projectName + "的学校数量:");
            project[i].teamnum = sc.nextInt();
            System.out.println("请依次输入参加" + project[i].projectName + "的学校编号:");
            for (int j = 0; j < project[i].teamnum; j++) {
                project[i].jointeam[j] = sc.nextInt();
                System.out.println("输入成功!");
            }
            System.out.println("参加项目" + project[i].projectName + "的学校有");
            for (int j = 0; j < project[i].teamnum; j++) {
                System.out.println(project[i].jointeam[j] + " ");
            }
            //录入分数
            System.out.println("请依次输入参加" + project[i].projectName + "的学校的分数");
            for (int j = 0; j < project[i].teamnum; j++) {
                int score = sc.nextInt();
                if (score >= 0 && score <= 100) {
                    project[i].score[j] = score;
                    System.out.println("输入成功!");
                } else {
                    System.out.println("您输入的有误!");
                    System.exit(0);
                }
            }
            System.out.println("显示如下:");
            for (int j = 0; j < project[i].teamnum; j++) {
                System.out.println("编号" + project[i].jointeam[j] + "的学校取得的成绩:" + project[i].score[j]);
            }
        }
    }

    public static void printByCodes() {
        //按学校编号排序输出
        int sum = 0;
        for (int i = 0; i < n; i++) {
            System.out.println("学校编号:" + school[i].schoolCode);
            System.out.println("学校名称:" + school[i].schoolName);
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < project[j].jointeam[k]; k++) {
                    if (project[j].jointeam[k] == school[i].schoolCode) {
                        System.out.println("参赛项目:" + project[j].projectName);
                        System.out.println("取得成绩:" + project[j].score[k]);
                        System.out.println("*****");
                        sum += project[j].score[k];
                    }
                }
            }
            System.out.println("学校取得的总分是:" + sum);
            school[i].sum = sum;
            sum = 0;
            System.out.println("**********************");
        }
    }

    public static void printByScore() {
        //按总分排序
        int temp = 0;
        int[] temps = new int[n];
        for (int i = 0; i < n; i++) {
            temps[i] = school[i].sum;
        }
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                if (temps[i] > temps[j]) {
                    temp = temps[i];
                    temps[i] = temps[j];
                    temps[j] = temp;
                }
            }
        }
        System.out.println("按照总分排序学校如下:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (temps[i] == school[j].sum) {
                    System.out.println("总分第" + (n - i) + "名的学校是" + school[j].schoolName);
                    System.out.println("其总分是:" + school[j].sum);
                    System.out.println("*******");
                }
            }
        }
    }

    public static void findByCodes() {
        //按学校编号查询学校某个项目的获奖情况
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你想要查询的学校编号");
        int scode = sc.nextInt();
        System.out.println("请输入你想要查询的项目编号");
        int pcode = sc.nextInt();

        int[] temps = new int[project[pcode - 1].teamnum];
        int len = temps.length;
        for (int i = 0; i < len; i++) {
            temps[i] = project[pcode - 1].score[i];
        }
        int temp = 0;
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                if (temps[i] > temps[j]) {
                    temp = temps[i];
                    temps[i] = temps[j];
                    temps[j] = temp;
                }
            }
        }
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if (project[pcode - 1].score[i] == temps[j]) {
                    System.out.println("该学校在该项目中排第" + j + "名");
                }
            }
        }
    }

    public static void getTopThree() {
        //按项目编号查询取得前三名的学校
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想要查询的项目的编号:");
        int code = sc.nextInt();
        int[] temps = new int[project[code - 1].teamnum];
        int len = temps.length;
        for (int i = 0; i < len; i++) {
            temps[i] = project[code - 1].score[i];
        }
        int temp = 0;
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                if (temps[i] > temps[j]) {
                    temp = temps[i];
                    temps[i] = temps[j];
                    temps[j] = temp;
                }
            }
        }
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < n; j++) {
                if (temps[len - 1] == project[code - 1].score[i]) {
                    if (school[j].schoolCode == project[code - 1].jointeam[i]) {
                        System.out.println("第一名的学校是:" + school[j].schoolName);
                    }
                }
                if (temps[len - 2] == project[code - 1].score[i]) {
                    if (school[j].schoolCode == project[code - 1].jointeam[i]) {
                        System.out.println("第二名的学校是:" + school[j].schoolName);
                    }
                }
                if (temps[len - 3] == project[code - 1].score[i]) {
                    if (school[j].schoolCode == project[code - 1].jointeam[i]) {
                        System.out.println("第三名的学校是:" + school[j].schoolName);
                    }
                }
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值