Java笔记_19
- 一、综合练习
-
- 1.1、网络爬取(数据处理并写出)
- 1.2、利用胡涂包生成数据
- 1.3、随机点名器的四个需求
- 1.4、带权重的随机算法
一、综合练习
1.1、网络爬取(数据处理并写出)
package IO_LastTest;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Dome1 {
public static void main(String[] args) throws IOException {
String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&qq-pf-to=pcqq.c2c";
String BoyNameNet = "http://www.haoming8.cn/baobao/10881.html";
String GrilNameNet = "http://www.haoming8.cn/baobao/7641.html";
String familyName = getNet(familyNameNet);
String BoyName = getNet(BoyNameNet);
String GrilName = getNet(GrilNameNet);
String familyNameregex = "(.{4})(,|。)";
ArrayList<String> familyNameTemplist = getString(familyName, familyNameregex, 1);
String BoyNameregex = "([\\u4E00-\\u9FA5]{2})(、)";
ArrayList<String> BoyNameTempList = getString(BoyName,BoyNameregex,1);
String GrilNameregex = "([\\u4E00-\\u9FA5]{2})(\\s)";
ArrayList<String> GrilNameTempList = getString(GrilName, GrilNameregex, 1);
ArrayList<String> familyNameList = new ArrayList<>();
for (String s : familyNameTemplist) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
familyNameList.add(chars[i]+"");
}
}
ArrayList<String> BoyNameList = new ArrayList<>();
for (String s : BoyNameTempList) {
if (!BoyNameList.contains(s)){
BoyNameList.add(s);
}
}
ArrayList<String> GrilNameList = new ArrayList<>();
for (String s : GrilNameTempList) {
if (!GrilNameList.contains(s)){
GrilNameList.add(s);
}
}
ArrayList<String> name = getName(familyNameList, BoyNameList, GrilNameList, 50, 20);
Collections.shuffle(name);
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Java\\IDEA\\代码文件\\After_pintu_again\\a.txt"));
for (String s : name) {
bw.write(s);
bw.newLine();
}
bw.close();
}
public static ArrayList<String> getName(ArrayList<String> familyNameList,ArrayList<String> BoyNameList,ArrayList<String> GrilNameList,int boyNum,int grilNum){
ArrayList<String> boyname = new ArrayList<>();
while (true){
if (boyname.size()==boyNum){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(BoyNameList);
boyname.add(familyNameList.get(0)+BoyNameList.get(0));
}
ArrayList<String> grilname = new ArrayList<>();
while (true){
if (grilname.size()==grilNum){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(GrilNameList);
grilname.add(familyNameList.get(0)+GrilNameList.get(0));
}
ArrayList<String> list = new ArrayList<>();
Random r = new Random();
for (String s : boyname) {
int age = r.nextInt(10)+18;
list.add(s+"-男-"+age);
}
for (String s : grilname) {
int age = r.nextInt(8)+18;
list.add(s+"-女-"+age);
}
return list;
}
public static ArrayList<String> getString(String name,String regex,int i){
ArrayList<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(name);
while (matcher.find()){
list.add(matcher.group(i));
}
return list;
}
public static String getNet(String str) throws IOException {
StringBuilder sb =new StringBuilder();
URL url = new URL(str);
URLConnection coon = url.openConnection();
InputStreamReader isr = new InputStreamReader(coon.getInputStream());
int len;
while ((len=isr.read())!=-1){
sb.append((char) len);
}
isr.close();
return sb.toString();
}
}
1.2、利用胡涂包生成数据
String listContent = HttpUtil.get("https://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=2");
List<String> titles = ReUtil.findAll("(.*?)", listContent, 1);
for (String title : titles) {
Console.log(title);
}
package IO_LastTest;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Dome2 {
public static void main(String[] args) throws IOException {
String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&qq-pf-to=pcqq.c2c";
String BoyNameNet = "http://www.haoming8.cn/baobao/10881.html";
String GrilNameNet = "http://www.haoming8.cn/baobao/7641.html";
String familyNamestr = HttpUtil.get(familyNameNet);
String BoyNamestr = HttpUtil.get(BoyNameNet);
String GrilNamestr = HttpUtil.get(GrilNameNet);
List<String> familyNameTemplist = ReUtil.findAll("(.{4})(,|。)", familyNamestr, 1);
List<String> BoyNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、)", BoyNamestr, 1);
List<String> GrilNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(\\s)",GrilNamestr,1);
ArrayList<String> familyNameList = new ArrayList<>();
for (String s : familyNameTemplist) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
familyNameList.add(chars[i]+"");
}
}
ArrayList<String> BoyNameList = new ArrayList<>();
for (String s : BoyNameTempList) {
if (!BoyNameList.contains(s)){
BoyNameList.add(s);
}
}
ArrayList<String> GrilNameList = new ArrayList<>();
for (String s : GrilNameTempList) {
if (!GrilNameList.contains(s)){
GrilNameList.add(s);
}
}
ArrayList<String> name = getName(familyNameList, BoyNameList, GrilNameList, 50, 20);
Collections.shuffle(name);
FileUtil.writeLines(name,"D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt","UTF-8");
}
public static ArrayList<String> getName(ArrayList<String> familyNameList,ArrayList<String> BoyNameList,ArrayList<String> GrilNameList,int boyNum,int grilNum){
ArrayList<String> boyname = new ArrayList<>();
while (true){
if (boyname.size()==boyNum){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(BoyNameList);
boyname.add(familyNameList.get(0)+BoyNameList.get(0));
}
ArrayList<String> grilname = new ArrayList<>();
while (true){
if (grilname.size()==grilNum){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(GrilNameList);
grilname.add(familyNameList.get(0)+GrilNameList.get(0));
}
ArrayList<String> list = new ArrayList<>();
Random r = new Random();
for (String s : boyname) {
int age = r.nextInt(10)+18;
list.add(s+"-男-"+age);
}
for (String s : grilname) {
int age = r.nextInt(8)+18;
list.add(s+"-女-"+age);
}
return list;
}
}
1.3、随机点名器的四个需求
package IO_LastTest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class Dome3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt"));
ArrayList<String> list = new ArrayList<>();
String len;
while ((len = br.readLine())!=null){
list.add(len);
}
br.close();
Random r = new Random();
int index = r.nextInt(list.size());
String str = list.get(index).split("-")[0];
System.out.println(str);
}
}
package IO_LastTest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Dome4 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt"));
ArrayList<String> manlist = new ArrayList<>();
ArrayList<String> womanlist = new ArrayList<>();
String str;
while ((str = br.readLine())!=null){
if (str.split("-")[1].equals("男")){
manlist.add(str);
}else {
womanlist.add(str);
}
}
br.close();
ArrayList<Integer> integers = new ArrayList<>();
Collections.addAll(integers,1,1,1,1,1,1,1);
Collections.addAll(integers,0,0,0);
Random r = new Random();
int index = r.nextInt(integers.size());
if (integers.get(index)==1){
Collections.shuffle(manlist);
System.out.println(manlist.get(0).split("-")[0]);
} else if (integers.get(index)==0) {
Collections.shuffle(womanlist);
System.out.println(womanlist.get(0).split("-")[0]);
}
}
}
package IO_LastTest;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Dome5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
ArrayList<String> list = new ArrayList<>();
String str;
while ((str = br.readLine())!=null){
list.add(str);
}
br.close();
BufferedReader br2 = new BufferedReader(new FileReader("c.txt"));
int count = Integer.parseInt(br2.readLine());
count++;
BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
bw.write(count+"");
br2.close();
bw.close();
if (count==3){
System.out.println("张三");
}else {
Collections.shuffle(list);
System.out.println(list.get(0).split("-")[0]);
}
}
}
package IO_LastTest;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Dome6 {
public static void main(String[] args) throws Exception {
File f1 = new File("b.txt");
File f2 = new File("d.txt");
ArrayList<String> list1 = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(f1));
String len;
while ((len = br.readLine())!=null){
list1.add(len);
}
br.close();
Collections.shuffle(list1);
String name = list1.get(0).split("-")[0];
System.out.println(name);
BufferedWriter bw2 = new BufferedWriter(new FileWriter(f1));
BufferedWriter bw = new BufferedWriter(new FileWriter(f2,true));
bw.write(list1.get(0));
bw.newLine();
list1.remove(0);
for (String s : list1) {
bw2.write(s);
bw2.newLine();
}
bw.close();
bw2.close();
System.out.println("==============");
if (list1.size()==0){
FileUtils.copyFile(f2,f1);
writeFile(f2);
}
}
public static void writeFile(File f2) throws Exception{
FileWriter fw = new FileWriter (f2);
fw.write("");
fw.flush();
fw.close();
}
}
1.4、带权重的随机算法
package IO_LastTest;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Dome7 {
public static void main(String[] args) throws IOException {
ArrayList<Student> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("Dome7.txt"));
String len;
while ((len = br.readLine())!=null){
String[] split = len.split("-");
list.add(new Student(split[0],split[1],Integer.parseInt(split[2]),Double.parseDouble(split[3])));
}
br.close();
System.out.println(list);
double weight = 0;
for (Student student : list) {
weight = weight + student.getWeight();
}
double[] arr = new double[list.size()];
int index = 0;
for (Student student : list) {
arr[index] = student.getWeight()/weight;
index++;
}
for (int i = 1; i < arr.length; i++) {
arr[i] = arr[i] + arr[i-1];
}
System.out.println(Arrays.toString(arr));
double number = Math.random();
int result = -Arrays.binarySearch(arr,number) - 1;
Student student = list.get(result);
System.out.println(student);
double sw = student.getWeight() / 2;
student.setWeight(sw);
BufferedWriter bw = new BufferedWriter(new FileWriter("Dome7.txt"));
for (Student student1 : list) {
bw.write(student1.toString());
bw.newLine();
}
bw.close();
}
}