From a8db0f52a37bda43ad72b86fae614d32a570bca1 Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:34:11 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4Java=E7=BB=83=E4=B9=A0?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 练习题1 --- EffectiveLines.java | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 EffectiveLines.java diff --git a/EffectiveLines.java b/EffectiveLines.java new file mode 100644 index 0000000..61ae759 --- /dev/null +++ b/EffectiveLines.java @@ -0,0 +1,59 @@ +package com.EffectiveLines; + +import java.io.*; + +/** + * 统计有效文件行数,不考虑多行注释 + */ +public class EffectiveLines { + private int couners; //统计有效行数的计数器 + + public EffectiveLines() { + this.couners = 0; + } + + /* + 读入文件,统计有效行数 + filename为文件名 + 返回有效行数 + */ + public int readFile(String filename) { + File file = new File(filename); + try { + FileInputStream fileInputStream = new FileInputStream(file); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String line = null; + while((line = bufferedReader.readLine()) != null) { + int cursor = 0; //游标,遍历一行数据用 + line = line.replace(" ",""); //去掉所有空格 + if(line.length() == 0) { //全是空格的,不是有效行 + continue; + } + //去掉空格后,剩下的是注释的话,也不是有效行 + if(line.charAt(0)=='/' && (line.charAt(1)=='/' || line.charAt(1)=='*')) { + continue; + } + couners++; + } + bufferedReader.close(); + inputStreamReader.close(); + fileInputStream.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return this.couners; + } + + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("this program needs 1 arguments"); + System.exit(0); + } + EffectiveLines effectiveLines = new EffectiveLines(); + int answer = effectiveLines.readFile(args[0]); + System.out.println(answer); + } +} From cddc3cf015b5999ddfd44b3a2c91068b01b557ee Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:34:33 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4Java=E7=BB=83=E4=B9=A0?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 练习题2 --- ExchangeRate.java | 163 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 ExchangeRate.java diff --git a/ExchangeRate.java b/ExchangeRate.java new file mode 100644 index 0000000..52d6498 --- /dev/null +++ b/ExchangeRate.java @@ -0,0 +1,163 @@ +package com.ExchangeRate; + +import jxl.Cell; +import jxl.Sheet; +import jxl.Workbook; +import jxl.read.biff.BiffException; +import jxl.write.Label; +import jxl.write.WritableSheet; +import jxl.write.WritableWorkbook; +import jxl.write.WriteException; +import jxl.write.biff.RowsExceededException; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +/** + * 从网站下载30天的所有人民币汇率数据,存入webData.xls + * 读取该表格文档,计算一个月内人民币汇率中间价,并输出至analysis.xls + * 需要导入jxl.jar包 + */ +public class ExchangeRate { + + private double dollar; //美元汇率 + private double euro; //欧元汇率 + private double hk_dollar; //港元汇率 + + public ExchangeRate() { + this.dollar = 0; + this.euro = 0; + this.hk_dollar = 0; + } + + /* + *从网站下载30天的所有人民币汇率数据,存入webData.xls + */ + public void getWebData() { + InputStream inputStream = null; + FileOutputStream fileOutputStream = null; + URLConnection urlConnection = null; + + Calendar calendar = Calendar.getInstance(); + Date endDate = calendar.getTime(); //当前日期 + calendar.add(Calendar.DATE,-30); + Date startDate = calendar.getTime(); //30天前日期 + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //日期格式 + String startDateStr = simpleDateFormat.format(startDate); + String endDateStr = simpleDateFormat.format(endDate); + +// System.out.println(startDateStr); +// System.out.println(endDateStr); + + //发送“中国外汇管理局”的导出表单请求 + String url = "http://www.safe.gov.cn/AppStructured/view/project_exportRMBExcel.action?projectBean.startDate=" + + startDateStr + "projectBean.endDate=" + endDateStr + "queryYN=true"; + System.out.println(url); + try { + urlConnection = new URL(url).openConnection(); + inputStream = urlConnection.getInputStream(); + fileOutputStream = new FileOutputStream("webData.xls"); //注意,因为周末没有数据,所以里面不是30行数据 + + byte [] bytes = new byte[1024]; + int len = -1; + while ((len = inputStream.read(bytes)) != -1) { //输出内容到本地表格上 + fileOutputStream.write(bytes,0,len); + fileOutputStream.flush(); + } + inputStream.close(); + fileOutputStream.close(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + * 从webData.xls中读入数据,并分析计算汇率 + */ + public void readExcel() { + Workbook workbook = null; + try { + InputStream inputStream = new FileInputStream("webData.xls"); + workbook = Workbook.getWorkbook(inputStream); + Sheet sheet = workbook.getSheet(0); //找到文件第一张表格 + int rows = sheet.getRows(); //获取总行数 + + //计算美元、欧元、港元汇率 + for (int i = 1; i < rows; ++i) { + Cell dollar_cell = sheet.getCell(1,i); + dollar += Double.parseDouble(dollar_cell.getContents()); + + Cell euro_cell = sheet.getCell(2,i); + euro += Double.parseDouble(euro_cell.getContents()); + + Cell hk_dollar_cell = sheet.getCell(4,i); + hk_dollar += Double.parseDouble(hk_dollar_cell.getContents()); + } + dollar = dollar/rows; + euro = euro/rows; + hk_dollar = hk_dollar/rows; + + inputStream.close(); + workbook.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (BiffException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + * 创建analysis.xls文件,并将分析结果输出到文件中 + */ + public void writeExcel() { + try { + OutputStream os = new FileOutputStream(new File("analysis.xls")); + WritableWorkbook writableWorkbook = Workbook.createWorkbook(os); + WritableSheet writableSheet = writableWorkbook.createSheet("人民币汇率分析",0); //创建表格文件中第一页表格 + + //输出表格第一行内容 + Label jieshao = new Label(0,0,"人民币汇率中间价"); + writableSheet.addCell(jieshao); + Label meiyuan = new Label(1,0,"美元"); + writableSheet.addCell(meiyuan); + Label ouyuan = new Label(2,0,"欧元"); + writableSheet.addCell(ouyuan); + Label gangyuan = new Label(3,0,"港元"); + writableSheet.addCell(gangyuan); + + //输出计算结果到表格中 + Label l_dollar = new Label(1,1,""+dollar); + writableSheet.addCell(l_dollar); + Label l_euro = new Label(2,1,""+euro); + writableSheet.addCell(l_euro); + Label l_hk = new Label(3,1,""+hk_dollar); + writableSheet.addCell(l_hk); + + writableWorkbook.write(); + writableWorkbook.close(); + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } catch (RowsExceededException e) { + e.printStackTrace(); + } catch (WriteException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + ExchangeRate er = new ExchangeRate(); + er.getWebData(); + er.readExcel(); + er.writeExcel(); + } +} From 474bb541f922ee5345e71aea849ab03618f5ab62 Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:34:48 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4Java=E7=BB=83=E4=B9=A0?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 练习题3 --- CountMostImport.java | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 CountMostImport.java diff --git a/CountMostImport.java b/CountMostImport.java new file mode 100644 index 0000000..925d1e5 --- /dev/null +++ b/CountMostImport.java @@ -0,0 +1,103 @@ +package com.CountMostImport; + +import java.io.*; +import java.util.*; + + +/** + * 递归读取目录下所有文件,统计import的类的个数 + * 这个版本没考虑“import java.io.*”这种带*号的情况,只是将这种情况视作一个类来看待。 + * 如果“import java.io.*”排名第9,且没有其它io包的类被引用。如何确定io包有哪几个类占据第9,第10? + */ +public class CountMostImport { + private HashMap hashMap = new HashMap(); //将所有类放到该字典里统计 + + /* + * 递归读取目录下所有文件 + */ + public void getfile(String filename) { + File file = new File(filename); + if (file.isDirectory()) { + File []files = file.listFiles(); //查看子目录下所有文件 + for (File ff : files) { + this.getfile(String.format("%s", ff)); //递归调用子目录下文件 + } + } + if (file.isFile()){ + countImport(String.format("%s", file)); //调用读取文件的函数 + } + } + + /* + * 统计文件中被引用的类的个数 + */ + public void countImport(String filename) { + try { + FileReader fin = new FileReader(filename); + BufferedReader bufferedReader = new BufferedReader(fin); //打开文件 + + String line = null; + while ((line = bufferedReader.readLine()) != null) { + line = line.trim(); + if (line.startsWith("import")) { //如果文件是引用类语句 + int begin = 6; + while (line.charAt(begin) == ' ') { + ++begin; + } + String classpath = line.substring(begin,line.length()-1); + if (hashMap.containsKey(classpath)) { //字典中已经有这个类了,次数+1 + int value = hashMap.get(classpath); + value++; + hashMap.put(classpath,value); + } else { //字典中没有这个类,把这个类加入字典中 + hashMap.put(classpath,1); + } + } + //如果遇到类声明了,则确认import语句块已经结束 + if (line.startsWith("public") || line.startsWith("class")) { + break; + } + } + bufferedReader.close(); + fin.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + * 字典按值排降序,将前十输出 + */ + public void sortAndPrintTop10() { + List> list = new ArrayList>(hashMap.entrySet()); + Collections.sort(list, new Comparator>() { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + if (o1.getValue() == o2.getValue()) { + return o1.getKey().compareTo(o2.getKey()); //如果次数一样, + } + return o1.getValue() - o2.getValue(); //降序排列 + } + }); + int cursor = 0; //计数器,只输出前十 + for (Map.Entry map: list) { + System.out.println(map.getKey()+" : "+map.getValue()); + cursor++; + if (cursor >= 10) { + break; + } + } + } + + public static void main(String[] args) { + CountMostImport countMostImport = new CountMostImport(); + if (args.length < 1) { + System.out.println("this program need 1 arguments"); + System.exit(0); //以后看情况决定是否选1 + } + countMostImport.getfile(args[0]); //输入文件名 + countMostImport.sortAndPrintTop10(); + } +} From 816c9650f5312813fbce3f8e8b023b67cbefbd95 Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:37:28 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4linux=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 练习题1 --- Top10Ip.sh | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Top10Ip.sh diff --git a/Top10Ip.sh b/Top10Ip.sh new file mode 100644 index 0000000..18aa770 --- /dev/null +++ b/Top10Ip.sh @@ -0,0 +1,6 @@ +# tomcataccess logиʽ£ +# 127.0.0.1 37 [05/Jun/2012:17:23:43 +0800] -- 200 2806 127.0.0.1 8080 GET/cdbleasing/message.listMessagePrompt.action?_dc=1338888223333 HTTP/1.1 +# õһֶεip㹻 +# ļƣlocalhost_access_log*.txtļ + +awk -F" " '{print $1}' *access_log*.txt|sort|uniq -c|sort -nr|head -n10 \ No newline at end of file From 67354972474fc5eb23def10715a8e3a9c4c4efde Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:41:57 +0800 Subject: [PATCH 05/11] =?UTF-8?q?linux=E7=BB=83=E4=B9=A0=E9=A2=982?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 提交linux作业 --- linux2.sh | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 linux2.sh diff --git a/linux2.sh b/linux2.sh new file mode 100644 index 0000000..9fe8937 --- /dev/null +++ b/linux2.sh @@ -0,0 +1,5 @@ +# Զscp +# scp ļ Զû@Զ̻ļַ +# ֪Զûʲôдroot + +scp /dir1 root@l-test.dev.cn1:/tmp \ No newline at end of file From 95371b67219dd12ca5f2d2e4c1b4ee1d99118468 Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:42:31 +0800 Subject: [PATCH 06/11] Revert "Initial commit" This reverts commit fa8a91201e8db55f71f1c9a0325b371b63c4c4f0. --- README.md | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 158d27d..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# campus2016 -git repository for qunar students 2016 From 46354841aed0a6c83c5631e03c42c485d8b8a15a Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:43:02 +0800 Subject: [PATCH 07/11] Revert "Revert "Initial commit"" This reverts commit 95371b67219dd12ca5f2d2e4c1b4ee1d99118468. --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..158d27d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# campus2016 +git repository for qunar students 2016 From 091e8a6c52b2492e0959d5da80de80a3359fbe2b Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:52:21 +0800 Subject: [PATCH 08/11] =?UTF-8?q?Java=E7=BB=83=E4=B9=A0=E9=A2=981?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EffectiveLines.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EffectiveLines.java b/EffectiveLines.java index 61ae759..bcd79f9 100644 --- a/EffectiveLines.java +++ b/EffectiveLines.java @@ -13,9 +13,9 @@ public EffectiveLines() { } /* - 读入文件,统计有效行数 - filename为文件名 - 返回有效行数 + * 读入文件,统计有效行数 + * filename为文件名 + * 返回有效行数 */ public int readFile(String filename) { File file = new File(filename); From 1728eeb623da83c352241ea20abffb797c06a07e Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:52:29 +0800 Subject: [PATCH 09/11] =?UTF-8?q?Java=E7=BB=83=E4=B9=A0=E9=A2=982?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExchangeRate.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ExchangeRate.java b/ExchangeRate.java index 52d6498..6409f6d 100644 --- a/ExchangeRate.java +++ b/ExchangeRate.java @@ -20,7 +20,8 @@ /** * 从网站下载30天的所有人民币汇率数据,存入webData.xls - * 读取该表格文档,计算一个月内人民币汇率中间价,并输出至analysis.xls + * 分析表格数据: + * 读取该表格文档,计算一个月内人民币汇率中间价,并输出至analysis.xls * 需要导入jxl.jar包 */ public class ExchangeRate { From fe863afc34424982210e2cf62c744e84f0417f61 Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:52:39 +0800 Subject: [PATCH 10/11] =?UTF-8?q?Java=E7=BB=83=E4=B9=A0=E9=A2=983?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CountMostImport.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CountMostImport.java b/CountMostImport.java index 925d1e5..9d5c56d 100644 --- a/CountMostImport.java +++ b/CountMostImport.java @@ -76,7 +76,7 @@ public void sortAndPrintTop10() { @Override public int compare(Map.Entry o1, Map.Entry o2) { if (o1.getValue() == o2.getValue()) { - return o1.getKey().compareTo(o2.getKey()); //如果次数一样, + return o1.getKey().compareTo(o2.getKey()); //如果次数一样,按字典序列排序 } return o1.getValue() - o2.getValue(); //降序排列 } From f9453dc98f151e0886074fe7f634c426aa3fbd56 Mon Sep 17 00:00:00 2001 From: XingyuFu <417133251@qq.com> Date: Thu, 16 Jun 2016 10:54:21 +0800 Subject: [PATCH 11/11] =?UTF-8?q?linux=E7=BB=83=E4=B9=A0=E9=A2=981?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Top10Ip.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Top10Ip.sh b/Top10Ip.sh index 18aa770..ddcd716 100644 --- a/Top10Ip.sh +++ b/Top10Ip.sh @@ -1,6 +1,6 @@ # tomcataccess logиʽ£ # 127.0.0.1 37 [05/Jun/2012:17:23:43 +0800] -- 200 2806 127.0.0.1 8080 GET/cdbleasing/message.listMessagePrompt.action?_dc=1338888223333 HTTP/1.1 -# õһֶεip㹻 -# ļƣlocalhost_access_log*.txtļ +# õһֶεip㹻ˡͳip +# ļƣlocalhost_access_log*.txtļͨ *access_log*.txt awk -F" " '{print $1}' *access_log*.txt|sort|uniq -c|sort -nr|head -n10 \ No newline at end of file