学生无课课表查询实例

package aa;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.stream.IIOByteBuffer;
/*例外:周一第一节大课为a同学课程:具体时间为7-15周,只在单周上课。
    周五第四节大课为a同学课程:具体时间为1-6周,只在双周上课。
    周五第四节大课为b同学课程:具体时间为4-14周,只在双周上课。

但在dis(去除重复时),程序出现错误,合并不了。
例如:周一 第一大节people应为b。*/

/*有三个同学,分别为abc.
    方法 :  利用容器思想,用户输入非数字异常处理。
      注:table为原课表,table2为无课课表,有五大节课课,输入1代表1-2节,输入2代表3-5节,输入3代表6-7节,输入4代表8-10节,输入5代表11-12节。
    0代表单双周,1代表单周,2代表双周。
    步骤:
   1.原课表保存在table中,先让用户选择要查询的周数,分单双周,单双周的课程有所不同,将本周的课表的无课课表保存在table2中,并将table2打印出来
   2.在table2的基础上让用户选择要查询的星期数,将此保存在table3中,并将table3打印出来
   3.在table3的基础上让用户选择要查询的具体时间,将此保存在table4中,并将table4打印出来
   4.循环1,2,3步骤,在此之前需将每个容器清空。退出程序为0
 */
public class query{
    public static void main(String args[]){
        while(true){
            try{
                course.selectWeek();
                }catch(InputMismatchException e){        //用户输入非数字处理
                    System.out.println("您输入的不是数字,请重新输入:");
                    continue;
            }
        }
    }
}

class course{
    static ArrayList<course> table=new ArrayList<course>();//原所有课表
    static ArrayList<course> table2=new ArrayList<course>();//原所有无课课表
    static ArrayList<course> table5=new ArrayList<course>();  //table5:合并之前 原 本周 无课课表
    static ArrayList<course> table6=new ArrayList<course>();  //table6:合并之后
    static ArrayList<course> table3=new ArrayList<course>(); //table3:用来存储查出的星期
    static ArrayList<course> table4=new ArrayList<course>(); //table4:用来存储查出的确切课程

    String week;
    String time;
    String people;
    static int cStart;//课程开始周
    static int cEnd;//课程结束周
    int calcu;  //1:单周上课,2:双周上课 ,0:单双周
    course(String w, String t,String p){
        week = w;
        time=t;
        people=p;
        cStart=1;
        cEnd=17;
        calcu=0;
    }
    course(String w, String t,String p,int cs,int ce,int cal){
        week = w;
        time=t;
        people=p;
        cStart=cs;
        cEnd=ce;
        calcu=cal;

    }
    static void init1(){
        table.add(new course("周一","1-2","c"));
        table.add(new course("周一","1-2","a",7,15,1));
        table.add(new course("周一","3-5","abc"));
        table.add(new course("周一","11-12","b"));

        table.add(new course("周二","1-2","b"));
        table.add(new course("周二","3-5","ac"));
        table.add(new course("周二","6-7","a"));    

        table.add(new course("周三","1-2","ac"));
        table.add(new course("周三","6-7","ab"));
        table.add(new course("周三","8-10","bc"));
        table.add(new course("周三","11-12","c"));

        table.add(new course("周四","1-2","c"));
        table.add(new course("周四","3-5","a"));
        table.add(new course("周四","6-7","bc"));
        table.add(new course("周四","8-10","ab"));
        table.add(new course("周四","11-12","ac"));

        table.add(new course("周五","1-2","ac"));
        table.add(new course("周五","3-5","c"));
        table.add(new course("周五","6-7","abc"));
        table.add(new course("周五","8-10","a",1,6,2));
        table.add(new course("周五","8-10","b",4,15,2));
        table.add(new course("周五","11-12","b"));    

        }
    static void show(ArrayList<course> tab){
        Iterator<course> i=tab.iterator();
        while(i.hasNext()){
            course j=i.next();
            System.out.printf("%-6s  %-5s  %-5s\n",j.week,j.time,j.people);
        }
    }
    static void revise(ArrayList<course> ttable,ArrayList<course> utable){ //将ttable的无课课表保存到utable中
        Iterator<course>m=ttable.iterator();
        while(m.hasNext()){
            course n=m.next();
            switch(n.people){
            case "a":
                n.people="bc";
                utable.add(n);
                //System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            case "b":
                n.people="ac";
                utable.add(n);
                //System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            case "c":
                n.people="ab";
                utable.add(n);
                ///System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            case "ab":
                n.people="c";
                utable.add(n);
                //System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            case "ac":
                n.people="b";
                //System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            case "bc":
                n.people="a";
                utable.add(n);
                //System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            case "abc":
                n.people="nobody";
                utable.add(n);
                //System.out.printf("%-6s  %-5s  %-5s\n",n.week,n.time,n.people);
                break;
            }
        }
    }
    static void selectWeek(){  //按周数查询
        course.init1();
        course.revise(table,table2);
        int read = 0;
        while(true)    {
        try{//异常处理
            System.out.println("请输入要查询的周(若要退出请输入0):");
            Scanner reader=new Scanner(System.in);//用户输入
            read=(int)reader.nextInt();
            if(read<0||read>18){          //用户输入周数越界处理:重新输入。
                System.out.println("您输入周数越界,请重新输入:");
                continue;
            }
        }catch(InputMismatchException e){        //用户输入非数字处理
            System.out.println("您输入的不是数字,请重新输入:");
            continue;
        }
            if (read==0) System.exit(0);
            else if(read%2!=0&&(read>=course.cStart&&read<=course.cEnd)){     //单周
                System.out.println("第"+read+"周无课课表(注:未查到为abc):");
                Iterator<course> ii=table2.iterator();
                while(ii.hasNext()){
                    course jj=ii.next();
                    if(jj.calcu==0||jj.calcu==1){
                        table5.add(jj);
                    }
                }
                dis(table5, table6); 

            }
            else if(read%2==0&&(read>=course.cStart&&read<=course.cEnd)){                    //双周
                System.out.println("第"+read+"周无课课表(注:未查到为abc):");
                Iterator<course> ii=table2.iterator();
                while(ii.hasNext()){
                    course jj=ii.next();
                    if(jj.calcu==0||jj.calcu==2){
                        table5.add(jj);
                    }
                }
                dis(table5, table6);
            }
            break;
        }
        selectTime();
        table.removeAll(table);
        table2.removeAll(table2);
        table3.removeAll(table3);
        table4.removeAll(table4);
        table5.removeAll(table5);
        table6.removeAll(table6);
    }
    static void dis(ArrayList<course> tab,ArrayList<course> tab1){//两个课程表作比较合并(当在同一时间段时)
        Iterator<course> i=tab.iterator();
        Iterator<course> j=tab.iterator();
        while(i.hasNext()){
            while(j.hasNext()){
                course ii=i.next();
                course jj=j.next();
                if(ii.week.equals(jj.week)&&ii.time.equals(jj.time)){// 两表中相同内容,直接添加在tab1中
                    if(ii.people.equals(jj.people)){
                        tab1.add(ii);
                    }
                    else{
                        for(int m=0;m<jj.people.length();m++){//两表中星期相同,具体时间相同,将people改正,合并。
                            char c=jj.people.charAt(m);//第二个表jj的每个字符,转化为字符串。
                            String s=Character.toString(c);
                            Pattern p=Pattern.compile(s);
                            Matcher mat=p.matcher(ii.people);//在ii.people对字符串进行模式匹配.
                            if(mat.find()) ii.people=s;
                            tab1.add(ii);                //匹配改正people并添加在tab1中
                        }
                    }
                }
                else table.add(ii);
            }
        }
        show(tab1);//将合并之后的表打印出来。
    }
    static void selectTime(){   //按星期查询
        int read=0;
        while(true)    {          //异常处理
            try{
                System.out.println("请输入要查询的星期数(若要退出请输入0):");
                Scanner reader=new Scanner(System.in);//用户输入
                read=(int)reader.nextInt();
                if(read<0||read>7){          //用户输入周数越界处理:重新输入。
                    System.out.println("您输入星期数越界,请重新输入(若要退出请输入0):");
                    continue;
                }
            }catch(InputMismatchException e){        //用户输入非数字处理
                System.out.println("您输入的不是数字,请重新输入:");
                continue;
            }
            Iterator<course> i=table5.iterator();
            if(read>0&&read<8)            System.out.println("星期"+read+"无课课表为:");
            switch(read){
                case 1:    //1代表周一
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周一"))          table3.add(readw);
                    }
                    break;
                case 2:
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周二"))            table3.add(readw);
                    }
                    break;
                case 3:
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周三"))            table3.add(readw);
                    }
                    break;
                case 4:
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周四"))            table3.add(readw);
                    }
                    break;
                case 5:
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周五"))            table3.add(readw);
                    }
                    break;
                case 6:
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周六"))            table3.add(readw);
                    }
                    break;
                case 7:
                    while(i.hasNext()){
                        course readw=i.next();
                        if(readw.week.equals("周日"))            table3.add(readw);
                    }
                    break;
                case 0:
                    System.exit(0);
                    break;
        }
            System.out.println("注:未查到为“abc”");
            show(table3);
            break;
        }
        selectCourse(); //在某天查询具体第几节课课程表
    }
    static void selectCourse(){
        int read = 0;
        while(true)    {
        try{//异常处理
            System.out.println("要查询第几大节课?(若要退出请输入0):");
            Scanner reader=new Scanner(System.in);//用户输入
            read=(int)reader.nextInt();
            if(read<0||read>6){          //用户输入具体时间数越界处理:重新输入。
                System.out.println("您没有本节课,请重新输入:");
                continue;
            }
        }catch(InputMismatchException e){        //用户输入非数字处理
            System.out.println("您输入的不是数字,请重新输入:");
            continue;
        }
        Iterator<course> i=table3.iterator();
        if(read>0&&read<6)            System.out.println("第"+read+"大节课无课课表为:");
        switch(read){
            case 1:    //1-2为第一大节,...
                while(i.hasNext()){
                    course readw=i.next();
                    if(readw.time.equals("1-2"))          table4.add(readw);
                }
                break;
            case 2:
                while(i.hasNext()){
                    course readw=i.next();
                    if(readw.time.equals("3-5"))            table4.add(readw);
                }
                break;
            case 3:
                while(i.hasNext()){
                    course readw=i.next();
                    if(readw.time.equals("6-7"))            table4.add(readw);
                }
                break;
            case 4:
                while(i.hasNext()){
                    course readw=i.next();
                    if(readw.time.equals("8-10"))            table4.add(readw);
                }
                break;
            case 5:
                while(i.hasNext()){
                    course readw=i.next();
                    if(readw.time.equals("11-12"))            table4.add(readw);
                }
                break;
            case 0:
                System.exit(0);
                break;
        }
        System.out.println("注:未查到为“abc”");
        show(table4);
        break;
        }
    }
}        
时间: 2024-08-03 12:27:19

学生无课课表查询实例的相关文章

SQLserver分页查询实例

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案.最简单.普通的方法: SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000 ID

斯坦福第十八课:应用实例:图片文字识别(Application Example: Photo OCR)

18.1  问题描述和流程图 18.2  滑动窗口 18.3  获取大量数据和人工数据 18.4  上限分析:哪部分管道的接下去做 18.1  问题描述和流程图 图像文字识别应用所作的事是,从一张给定的图片中识别文字.这比从一份扫描文档中 识别文字要复杂的多. 为了完成这样的工作,需要采取如下步骤: 为了完成这样的工作,需要采取如下步骤: 1. 文字侦测(Text detection)——将图片上的文字与其他环境对象分离开来 2. 字符切分(Character segmentation)——将文

PHP中XPATH 实现xml及html文件快速解析(附xml做小型数据库实现六级单词快速查询实例)

一.XPATH简单入门 XPATH ,XQUERY 专门查询XML语言,查询速度快 使用方法: (1)创建DOM工具,并且加载 xml文件 $xml = new  DOMDocument('1.0','utf-8'); $xml -> load('./dict.xml'); 注:若是对html文件进行快速解析, 同样的道理 此处使用   loadhtmlfile (‘dict.html’); (2)创建xpath解析工具 $xpath = new DOMXPATH($xml); (3)编写xpa

JBoss+Ant实现EJB无状态会话bean实例

EJB分为session bean.entity bean.message-driven bean,session bean又分为无状态会话bean和有状态会话bean. session bean负责与客户端交互,是编写业务逻辑的地方,在session bean中可以通过jdbc直接操作数据库,但大多数情况下都是通过entity bean来完成对数据库的操作的. 平常,我们使用最多的是无状态bean,因为它的bean实例可供多个用户使用,所以它的性能比有状态bean高.正因为一个bean实例被多

MYsqli 绑定插入与查询实例

<?php $conn = new mysqli('localhost','root','','orders'); //连接 $prepare = "insert into t100 values (?,?,?)";//预设插入sql $bind = $conn->prepare($prepare); $t1 = NULL; $t2 = 'Www'; $t3 = date('Y-m-d H:i:s'); $bind->bind_param('iss',$t1,$t2,

超过5名学生的课

有一个courses 表 ,有: student (学生) 和 class (课程). 请列出所有超过或等于5名学生的课. 例如,表: +---------+------------+ | student | class | +---------+------------+ | A | Math | | B | English | | C | Math | | D | Biology | | E | Math | | F | Computer | | G | Math | | H | Math

力扣——超过5名学生的课(数据库的题

有一个courses 表 ,有: student (学生) 和 class (课程). 请列出所有超过或等于5名学生的课. 例如,表: +---------+------------+ | student | class | +---------+------------+ | A | Math | | B | English | | C | Math | | D | Biology | | E | Math | | F | Computer | | G | Math | | H | Math

596. 超过5名学生的课

题目描述 有一个courses 表 ,有: student (学生) 和 class (课程). 请列出所有超过或等于5名学生的课. 例如,表: +---------+------------+ | student | class | +---------+------------+ | A | Math | | B | English | | C | Math | | D | Biology | | E | Math | | F | Computer | | G | Math | | H |

数据库专题-leetcode596. 超过5名学生的课

题目及分析 题目 分析 请列出所有超过或等于5名学生的课 1.需要计算某一门课的student数量 group by 然后count 2.student数量大于或等于5 需要对聚合函数进行进一步的筛选 having 注意:题目最下面的提示也说了学生在每个课中不应被重复计算,也就是表中的记录可能出现相同的学生和课程几条数据.所以需要对学生也要去重.开始没注意到,忘记去重了. 3.可能会出现多条sutdent对应class的记录.所以需要对分组后的student字段进行去重 distinct 结果示