2022/10/20 9:13

This commit is contained in:
zyx 2022-10-20 09:13:42 +08:00
parent a4bdf0730e
commit 8376def1d7
8 changed files with 281 additions and 0 deletions

1
.idea/modules.xml generated
View File

@ -8,6 +8,7 @@
<module fileurl="file://$PROJECT_DIR$/Week5/Week5.iml" filepath="$PROJECT_DIR$/Week5/Week5.iml" />
<module fileurl="file://$PROJECT_DIR$/Week6/Week6.iml" filepath="$PROJECT_DIR$/Week6/Week6.iml" />
<module fileurl="file://$PROJECT_DIR$/Week7/Week7.iml" filepath="$PROJECT_DIR$/Week7/Week7.iml" />
<module fileurl="file://$PROJECT_DIR$/Week8/Week8.iml" filepath="$PROJECT_DIR$/Week8/Week8.iml" />
<module fileurl="file://$PROJECT_DIR$/java_class.iml" filepath="$PROJECT_DIR$/java_class.iml" />
<module fileurl="file://$PROJECT_DIR$/week1/week1.iml" filepath="$PROJECT_DIR$/week1/week1.iml" />
<module fileurl="file://$PROJECT_DIR$/work10/work10.iml" filepath="$PROJECT_DIR$/work10/work10.iml" />

11
Week8/Week8.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

50
Week8/src/Exercise1.java Normal file
View File

@ -0,0 +1,50 @@
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Exercise1 {
public static void main(String[] args) {
String regex = "((add)|(sub)|(max)|(min)|(doubleMe))\\(\\d+(.\\d+)?,\\d+(.\\d+)?\\)";
Pattern p = Pattern.compile(regex);
String str = new Scanner(System.in).nextLine();
String expressionStr = str;
Matcher matcher = p.matcher(str);
String res = "";
while (matcher.find()){
String subStr = matcher.group();
res = calcExpression(subStr)+"";
str = str.replaceAll(regex,res);
matcher = p.matcher(str);
}
System.out.println(expressionStr + "=" + res);
}
public static double calcExpression(String expression){
double res = 0;
String exp = expression.substring(0, expression.indexOf("("));
if ("doubleMe".equals(exp)){
double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")")));
res = num*2;
} else {
double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(",")));
double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")")));
switch (exp){
case "add":
res = num1+num2;
break;
case "sub":
res = num1-num2;
break;
case "max":
res = Math.max(num1, num2);
break;
case "min":
res = Math.min(num1, num2);
break;
}
}
return res;
}
}

61
Week8/src/Exercise2.java Normal file
View File

@ -0,0 +1,61 @@
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author : 张宇轩
* @createTime : 2022/10/20 - 8:28
*/
public class Exercise2 {
public static void main(String[] args) {
String htmlStr = "<html><head><title>欢迎访问武汉纺织大学主页</title></head>\n" +
"<body><img src='1.jpg'/>\n" +
"<a href='1.htm'>首页</a>\n" +
"<a href='2.htm'>教务处</a>\n" +
"<a href='3.htm'>数计学院</a>\n" +
"<img src='2.jpg'/>\n" +
"<img src='3.jpg'/>\n" +
"</body></html>";
System.out.println("网页标题:" + getContext(htmlStr, "title").get(0));
List<String> img = getAttribute(htmlStr, "img", "src");
System.out.print("网页中共" + img.size() + "个图片,文件名为");
for (int i = 0; i < img.size(); i++) {
System.out.print(img.get(i));
if(i != img.size()-1)
System.out.print(",");
}
System.out.println();
List<String> linkContext = getContext(htmlStr, "a");
List<String> linkHref = getAttribute(htmlStr, "a", "href");
System.out.println("网页中包含" + linkContext.size() + "个超链接,超链接信息如下:\n名称\t地址");
for (int i = 0; i < linkContext.size(); i++) {
System.out.println(linkContext.get(i) + "\t" + linkHref.get(i));
}
}
public static List<String> getContext(String htmlStr, String tagName){
List<String> list = new ArrayList<>();
String regex = "<" + tagName + "(.*?)>(.*?)</" + tagName + ">";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(htmlStr);
while (matcher.find()){
list.add(matcher.group(2));
}
return list;
}
public static List<String> getAttribute(String htmlStr,String tagName,String attributeName){
List<String> list = new ArrayList<>();
String regex = "<" + tagName + ".+?" + attributeName + "='(.*?)'.+?>";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(htmlStr);
while (matcher.find()){
list.add(matcher.group(1));
}
return list;
}
}

27
Week8/src/Exercise3.java Normal file
View File

@ -0,0 +1,27 @@
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author : 张宇轩
* @createTime : 2022/10/20 - 8:47
*/
public class Exercise3 {
public static void main(String[] args) {
String htmlStr = "<font face=\"Arial Serif\" size=\"10px\" color=\"red\" />";
Pattern p1 = Pattern.compile("<\\s*font\\s*([^>]*)\\s*>",Pattern.CASE_INSENSITIVE);
Matcher m1 = p1.matcher(htmlStr);
if(m1.find()) {
String fontStr = m1.group();
Pattern p2 = Pattern.compile("([a-z]+)\\s*=\\s*\"([^\"]+)\"",Pattern.CASE_INSENSITIVE);
Matcher m2 = p2.matcher(fontStr);
while (m2.find()) {
String eachAttribute = m2.group();
eachAttribute = eachAttribute.replaceAll("=",":");
eachAttribute = eachAttribute.replaceAll("\"","");
System.out.println(eachAttribute);
}
}
}
}

32
Week8/src/Test1.java Normal file
View File

@ -0,0 +1,32 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author : 张宇轩
* @createTime : 2022/10/19 - 9:09
*/
public class Test1 {
public static void main(String[] args) {
String htmlStr = "<html><head></head>...<div><IMG alt ='xxx' SRC='1.jpg'/><img alt='xxx' src='2.jpg'/><img alt='xxx'\n" +
"src='3.jpg'></img></div></html>";
Pattern p = Pattern.compile("<img.*?>", Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(htmlStr);
int cnt=0;
while (matcher.find()){
String imgStr = matcher.group().toLowerCase();
System.out.println(imgStr);
String src = getSrc(imgStr);
System.out.println(src);
cnt++;
}
}
public static String getSrc(String imgStr){
Pattern p = Pattern.compile("(src)=(\"|\')(.*?)(\"|\')", Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(imgStr);
if(matcher.find()){
return matcher.group();
}
return "";
}
}

53
Week8/src/Test2.java Normal file
View File

@ -0,0 +1,53 @@
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author : 张宇轩
* @createTime : 2022/10/19 - 10:01
*/
public class Test2 {
public static void main(String[] args) {
String regex = "((add)|(sub)|(max)|(min)|(doubleMe))\\(\\d+(.\\d+)?,\\d+(.\\d+)?\\)";
Pattern p = Pattern.compile(regex);
String str = new Scanner(System.in).nextLine();
Matcher matcher = p.matcher(str);
String res = "";
while (matcher.find()){
String subStr = matcher.group();
res = calcExpression(subStr)+"";
System.out.print(subStr + " " + res + "->");
str = str.replaceAll(regex,res);
System.out.println(str);
matcher = p.matcher(str);
}
System.out.println("运算结果:" + res);
}
public static double calcExpression(String expression){
double res = 0;
String exp = expression.substring(0, expression.indexOf("("));
if ("doubleMe".equals(exp)){
double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")")));
res = num*2;
} else {
double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(",")));
double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")")));
switch (exp){
case "add":
res = num1+num2;
break;
case "sub":
res = num1-num2;
break;
case "max":
res = Math.max(num1, num2);
break;
case "min":
res = Math.min(num1, num2);
break;
}
}
return res;
}
}

46
Week8/src/Test3.java Normal file
View File

@ -0,0 +1,46 @@
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author : 张宇轩
* @createTime : 2022/10/19 - 10:40
*/
public class Test3 {
public static void main(String[] args) {
String htmlStr = "<html><head><title>欢迎访问武汉纺织大学主页</title></head>\n" +
"<body><img src='1.jpg'/>\n" +
"<a href='1.htm'>首页</a>\n" +
"<a href='2.htm'>教务处</a>\n" +
"<a href='3.htm'>数计学院</a>\n" +
"<img src='2.jpg'/>\n" +
"<img src='3.jpg'/>\n" +
"</body></html>";
System.out.println(getContext(htmlStr, "a"));
System.out.println(getAttribute(htmlStr,"a"));
}
public static List<String> getContext(String htmlStr, String tagName){
List<String> list = new ArrayList<>();
String regex = "<" + tagName + "(.*?)>(.*?)</" + tagName + ">";
System.out.println(regex);
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(htmlStr);
while (matcher.find()){
list.add(matcher.group(1));
}
return list;
}
public static List<String> getAttribute(String htmlStr,String tagName){
List<String> list = new ArrayList<>();
String regex = "<" + tagName + "(.*?)>(.*?)</" + tagName + ">";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(htmlStr);
while (matcher.find()){
list.add(matcher.group(1).split("=")[1]);
}
return list;
}
}