https://gist.github.com/gcrfelix/65d5a028c655f1b86f41
http://www.1point3acres.com/bbs/thread-119657-1-1.html
Given a function that reads one line a time from a file and returns the line to you. The line contains comment sign as of "/* */" or even nested signs. Note the sign doesn't have to be paired in one single line. Write a function to append all these lines and return them as one single string without comments.
解法:
1. flag 记录/*有没有开始, 如果没开始,但找到了/*,说明这行有comment, 把comment前的东西print出来
2. 如果这行还出现了*/, 说明comment的结尾也找到了,把结尾后面的代码print出来
3. 如果flag = true,没出现结尾,说明这行完全是comment(multiple lines), 跳过
4. 如果flag = false, 没出现开头,说明这行完全是代码,print出来。
public class remove_comments {
public void remove() {
boolean flag = false;
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (flag == false && s.indexOf("/*") != -1) {
flag = true;
System.out.print(s.substring(0, s.indexOf("/*")));
}
if (flag == true && s.indexOf("*/") != -1) {
flag = false;
System.out.print(s.substring(s.indexOf("*/") + 2));
}
if (flag == true) {
continue;
}
if (flag == false && s.indexOf("*/") == -1) {
System.out.print(s);
}
}
sc.close();
}
}
问了一道怎么把钱的数字表达转换成string表达. CC150的原题
http://www.1point3acres.com/bbs/thread-119657-1-1.html
Given a function that reads one line a time from a file and returns the line to you. The line contains comment sign as of "/* */" or even nested signs. Note the sign doesn't have to be paired in one single line. Write a function to append all these lines and return them as one single string without comments.
解法:
1. flag 记录/*有没有开始, 如果没开始,但找到了/*,说明这行有comment, 把comment前的东西print出来
2. 如果这行还出现了*/, 说明comment的结尾也找到了,把结尾后面的代码print出来
3. 如果flag = true,没出现结尾,说明这行完全是comment(multiple lines), 跳过
4. 如果flag = false, 没出现开头,说明这行完全是代码,print出来。
public class remove_comments {
public void remove() {
boolean flag = false;
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String s = sc.nextLine();
if (flag == false && s.indexOf("/*") != -1) {
flag = true;
System.out.print(s.substring(0, s.indexOf("/*")));
}
if (flag == true && s.indexOf("*/") != -1) {
flag = false;
System.out.print(s.substring(s.indexOf("*/") + 2));
}
if (flag == true) {
continue;
}
if (flag == false && s.indexOf("*/") == -1) {
System.out.print(s);
}
}
sc.close();
}
}
问了一道怎么把钱的数字表达转换成string表达. CC150的原题