第一题
修改程序清单21-7中的程序,如果关键字在注释或者字符串中,则不进行统计,将java文件名从命令行传递。假设java源代码是正确的,行注释和行段落注释不会交叉。
第二题
The four adjacent digits in the 1000-digit number that have the greatest product are 9*9*8=5832
答案
第一题
package ch13;
import java.util.*;
import java.io.*;
public class b {
public static void main(String[] args) {
String[] keywordString = {"abstract", "finally", "public",
"boolean", "float", "return", "break", "for", "short", "byte",
"goto", "static", "case", "if", "super", "catch", "implements",
"switch", "char", "import", "synchronized", "class",
"instanceof", "this", "const", "int", "throw", "continue",
"interface", "throws", "default", "long", "transient", "do",
"native", "try", "double", "new", "void", "else", "package",
"volatile", "extends", "private", "while", "final",
"protected", "true", "null"};
Set<String> keywordSet =
new HashSet<String>(Arrays.asList(keywordString));
int count = 0;
try {
//Scanner input = new Scanner(new File(args[0]));
Scanner input = new Scanner(new File("Ch21Q03_TestFile.java"));
String text = "";
while (input.hasNext()) {
String line = input.nextLine();
line = stripLineComments(line);
line = stripLineStringLiterals(line);
text += line + " ";
}
text = stripParagraghComments(text);
String[] tokens = text.split("[ \\[,()\\]]");
for (String token: tokens) {
if (keywordSet.contains(token)){
count++;
System.out.print(token+" ");}
}
System.out.println();
System.out.println("The number of keywords in the program is "
+ count);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/* Strip line comments */
private static String stripLineComments(String line) {
int index = line.indexOf("//");
if (index < 0)
return line;
else
return line.substring(0, index);
}
/* Strip string literals */
private static String stripLineStringLiterals(String line) {
int start = line.indexOf("\"");
int end = line.indexOf("\"", start + 1);
while (start > 0 && end > 0) {
line = line.substring(0, start) + line.substring(end + 1);
start = line.indexOf("\"");
end = line.indexOf("\"");
}
return line;
}
/* Strip paragraph comments */
private static String stripParagraghComments(String text) {
int start = text.indexOf("/*");
int end = text.indexOf("*/");
while (start > 0 && end > 0) {
text = text.substring(0, start) + text.substring(end + 2);
start = text.indexOf("/*");
end = text.indexOf("*/");
}
return text;
}
}
第二题
package ch13;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
public class a {
public static void main(String[] args) {
ArrayList numList = new ArrayList();
List v = new Vector();
long sum=1L;
FileReader fr=null;
try {
fr = new FileReader("13.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int ch = 0;
try {
for(int i =0;(ch = fr.read())!=-1;i++){
v.add(ch-'0');
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<v.size()-12;i++){
for(int j=i;j<i+13;j++){
sum =sum *(int)v.get(j);
}
//System.out.println(sum);
numList.add(sum);
sum=1;
}
Collections.sort(numList);//为了排序引入的函数
System.out.println(numList.get(numList.size()-1));
}
}