计算与软件工程作业四

作业要求 https://edu.cnblogs.com/campus/jssf/infor_computation17-31/homework/10534
课程目标 统计红楼梦,水浒传中人物出现次数,各章节
此作业在哪个具体方面帮我实现目标 java了解代码,更进一步学习
作业正文 https://www.cnblogs.com/limin123/p/12637452.html

作业1

8个评论:https://www.cnblogs.com/liziye/p/12443639.html#4538652
https://www.cnblogs.com/sunsijiao/p/12398371.html#4538855
https://www.cnblogs.com/wangyan1067/p/12376099.html#4538859
https://www.cnblogs.com/jiangziyi/p/12373286.html#4538865
https://www.cnblogs.com/yangqiuyan/p/12434874.html#4538870
https://www.cnblogs.com/chenyu666/p/12431633.html#4538878
https://www.cnblogs.com/li-lingling/p/12455057.html#4538882
https://www.cnblogs.com/zxy123456/p/12449427.html#4538887
总结:大家做作业基本按照要求,代码符合简明,易读,无二义性原则;缩进,行宽,括号,分行,等等都很规范,但都会出现小错误,只要认真仔细,就会避免这些错误的出现。

作业2

红楼梦中人物出现次数结果统计:

饼图柱状图:


表格:

水浒传中人物出现次数:

代码

红楼梦

package come.bean;
import java.io.*;
public class ComeBean {
  public static int count(String filename,String target)throws FileNotFoundException,IOException{
      FileReader fr=new FileReader(filename);
      BufferedReader br=new BufferedReader(fr);
      StringBuilder strb=new StringBuilder();
      while(true){
          String line=br.readLine();
          if(line==null){
              break;
          }
          strb.append(line);
      }
      String result=strb.toString();
      int count=0;
      int index=0;
      while(true){
          index=result.indexOf(target,index+1);
          if(index>0){
              count++;
          }
          else{
              break;
          }
      }
      br.close();
      return count;
  }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws FileNotFoundException,IOException {
        // TODO code application logic here
        String a="C:/Users/李敏/Desktop/软件工程/红楼梦.txt";
        String n=null;
        String b[]={"黛玉","妙玉","熙凤","宝玉","贾母","宝钗","探春","湘云","迎春","元春",
            "惜春","巧姐","李纨","可卿","平儿","晴雯","袭人","贾琏","贾政","香菱"};
        int j1=count(a,b[0]),j2=count(a,b[1]),j3=count(a,b[2]),j4=count(a,b[3]),j5=count(a,b[4]),j6=count(a,b[5]),
                j7=count(a,b[6]),j8=count(a,b[7]),j9=count(a,b[8]),j10=count(a,b[9]),j11=count(a,b[10]),j12=count(a,b[11]),
                j13=count(a,b[12]),j14=count(a,b[13]),j15=count(a,b[14]),j16=count(a,b[15]),
                j17=count(a,b[16]),j18=count(a,b[17]),j19=count(a,b[18]),j20=count(a,b[19]);
        int num []={j1,j2,j3};
        try{
            System.out.println("黛玉在文中出现的次数:"+count(a,b[0]));
             System.out.println("妙玉在文中出现的次数:"+count(a,b[1]));
              System.out.println("熙凤在文中出现的次数:"+count(a,b[2]));
              System.out.println("宝玉在文中出现的次数:"+count(a,b[3]));
              System.out.println("贾母在文中出现的次数:"+count(a,b[4]));
              System.out.println("宝钗在文中出现的次数:"+count(a,b[5]));
              System.out.println("探春在文中出现的次数:"+count(a,b[6]));
              System.out.println("湘云在文中出现的次数:"+count(a,b[7]));
              System.out.println("迎春在文中出现的次数:"+count(a,b[8]));
              System.out.println("元春在文中出现的次数:"+count(a,b[9]));
              System.out.println("惜春在文中出现的次数:"+count(a,b[10]));
              System.out.println("巧姐在文中出现的次数:"+count(a,b[11]));
              System.out.println("李纨在文中出现的次数:"+count(a,b[12]));
              System.out.println("可卿在文中出现的次数:"+count(a,b[13]));
              System.out.println("平儿在文中出现的次数:"+count(a,b[14]));
              System.out.println("晴雯在文中出现的次数:"+count(a,b[15]));
              System.out.println("袭人在文中出现的次数:"+count(a,b[16]));
              System.out.println("贾琏在文中出现的次数:"+count(a,b[17]));
              System.out.println("贾政在文中出现的次数:"+count(a,b[18]));
              System.out.println("香菱在文中出现的次数:"+count(a,b[19]));
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
       try //重定向输出流
       {  
           String path=System.getProperties().getProperty("user.dir")+"\\红楼梦统计结果\\"
                   +System.currentTimeMillis()+"红楼梦统计结果.txt";
           path="C:/Users/李敏/Desktop/软件工程/红楼梦统计结果.txt";
           try{
               new File(path).createNewFile();
               
           }catch(IOException e){
               e.printStackTrace();
           };
     PrintStream ps=new PrintStream(path);
     System.setOut(ps);
     System.setErr(ps);
      System.out.println("黛玉在文中出现的次数:"+count(a,b[0]));
             System.out.println("妙玉在文中出现的次数:"+count(a,b[1]));
              System.out.println("熙凤在文中出现的次数:"+count(a,b[2]));
              System.out.println("宝玉在文中出现的次数:"+count(a,b[3]));
              System.out.println("贾母在文中出现的次数:"+count(a,b[4]));
              System.out.println("宝钗在文中出现的次数:"+count(a,b[5]));
              System.out.println("探春在文中出现的次数:"+count(a,b[6]));
              System.out.println("湘云在文中出现的次数:"+count(a,b[7]));
              System.out.println("迎春在文中出现的次数:"+count(a,b[8]));
              System.out.println("元春在文中出现的次数:"+count(a,b[9]));
              System.out.println("惜春在文中出现的次数:"+count(a,b[10]));
              System.out.println("巧姐在文中出现的次数:"+count(a,b[11]));
              System.out.println("李纨在文中出现的次数:"+count(a,b[12]));
              System.out.println("可卿在文中出现的次数:"+count(a,b[13]));
              System.out.println("平儿在文中出现的次数:"+count(a,b[14]));
              System.out.println("晴雯在文中出现的次数:"+count(a,b[15]));
              System.out.println("袭人在文中出现的次数:"+count(a,b[16]));
              System.out.println("贾琏在文中出现的次数:"+count(a,b[17]));
              System.out.println("贾政在文中出现的次数:"+count(a,b[18]));
              System.out.println("香菱在文中出现的次数:"+count(a,b[19]));
       }catch(FileNotFoundException e)
       {
           e.printStackTrace();
       } 
    }
    }

水浒传

package javaapplication10;
import java.io.*;

public class JavaApplication10 {
public static int count(String filename,String target)throws FileNotFoundException,IOException{
      FileReader fr=new FileReader(filename);
      BufferedReader br=new BufferedReader(fr);
      StringBuilder strb=new StringBuilder();
      while(true){
          String line=br.readLine();
          if(line==null){
              break;
          }
          strb.append(line);
      }
      String result=strb.toString();
      int count=0;
      int index=0;
      while(true){
          index=result.indexOf(target,index+1);
          if(index>0){
              count++;
          }
          else{
              break;
          }
      }
      br.close();
      return count;
  }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws FileNotFoundException,IOException {
        // TODO code application logic here
        String a="C:/Users/李敏/Desktop/软件工程/水浒传.txt";
        String n=null;
        String b[]={"宋江","鲁智深","花荣","武松","吴用","高俅","史进","卢俊义","李逵","林冲",
            "杨志","戴宗","时迁","公孙胜","孙二娘","顾大嫂","扈三娘"};
        int j1=count(a,b[0]),j2=count(a,b[1]),j3=count(a,b[2]),j4=count(a,b[3]),j5=count(a,b[4]),j6=count(a,b[5]),
                j7=count(a,b[6]),j8=count(a,b[7]),j9=count(a,b[8]),j10=count(a,b[9]),j11=count(a,b[10]),j12=count(a,b[11]),
                j13=count(a,b[12]),j14=count(a,b[13]),j15=count(a,b[14]),j16=count(a,b[15]), j17=count(a,b[16]);
        int num []={j1,j2,j3};
        try{
            System.out.println("宋江在文中出现的次数:"+count(a,b[0]));
             System.out.println("鲁智深在文中出现的次数:"+count(a,b[1]));
              System.out.println("花荣在文中出现的次数:"+count(a,b[2]));
              System.out.println("武松在文中出现的次数:"+count(a,b[3]));
              System.out.println("吴用在文中出现的次数:"+count(a,b[4]));
              System.out.println("高俅在文中出现的次数:"+count(a,b[5]));
              System.out.println("史进在文中出现的次数:"+count(a,b[6]));
              System.out.println("卢俊义在文中出现的次数:"+count(a,b[7]));
              System.out.println("李逵在文中出现的次数:"+count(a,b[8]));
              System.out.println("林冲在文中出现的次数:"+count(a,b[9]));
              System.out.println("杨志在文中出现的次数:"+count(a,b[10]));
              System.out.println("戴宗在文中出现的次数:"+count(a,b[11]));
              System.out.println("时迁在文中出现的次数:"+count(a,b[12]));
              System.out.println("公孙胜在文中出现的次数:"+count(a,b[13]));
              System.out.println("孙二娘在文中出现的次数:"+count(a,b[14]));
              System.out.println("顾大嫂在文中出现的次数:"+count(a,b[15]));
              System.out.println("扈三娘在文中出现的次数:"+count(a,b[16]));
             
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
       
    }
    
}

码云链接:https://gitee.com/li_min_123/software_engineering/commit/86151bc718051c9ddaf4c749c245d7e92fe8e8c4

posted @ 2020-04-05 15:29  李敏17074222  阅读(156)  评论(0编辑  收藏  举报