📄 circularshifter.java
字号:
import java.io.IOException;
import java.io.CharArrayWriter;
import java.util.StringTokenizer;
public class CircularShifter extends Filter
{
public CircularShifter(Pipe input, Pipe output)
{
super(input, output);
}
/* Produces all circular shifts of lines from the input pipe. */
protected void transform(){
try
{
CharArrayWriter writer = new CharArrayWriter();
int c = input_.read();
while(c != -1)
{
if(((char) c) == '\n') //if meet the symbol of end of line
{
String line = writer.toString();
StringTokenizer tokenizer = new StringTokenizer(line); //将String line拆开成单个的词
String[] words = new String[tokenizer.countTokens()]; //create an array of String
int i = 0;
while(tokenizer.hasMoreTokens()) //将String line拆开成单个的词
words[i++] = tokenizer.nextToken(); // load all words into words[]
for(i = 0; i < words.length; i++)
{
String shift = "";
// 我们要将所有的数组words中的词words[0], words[1],...,words[words.length-1]
// 以及他们所有的shifts “连接”到一个大的字符串shift中,并且带有相应的空格
// when i=0, put all words[0],...,words[words.length-1] into shift
// when i=1, concate words[1],...,words[words.length-1], words[0] at the end of shift
// when i=2, concate words[2],...,words[0], words[1] at the end of shift
// when i=3, concate words[3],...,words[0], words[1], words[2]at the end of shift
for(int j = i; j < (words.length + i); j++) //start shifting
{
shift += words[j % words.length];
if(j < (words.length + i - 1))
shift += " ";
}
shift += '\n'; // 添加一个行截止符
char[] chars = shift.toCharArray(); // 将大字符串shift改成char[]型,叫做chars
for(int j = 0; j < chars.length; j++) // 将chars的内容按照字符写入写入到输出数据流
output_.write(chars[j]);
} //end of 1st "for" loop, 完成了将完整的一行字以及以及他们所有的shifts “连接”
//到一个大的字符传shift中,并且带有相应的空格
writer.reset();
}
else //对应于if(((char) c) == '\n')
writer.write(c); // what to write??
c = input_.read();
}
output_.closeWriter();
}
catch(IOException exc)
{
exc.printStackTrace();
System.err.println("KWIC Error: Could not make circular shifts.");
System.exit(1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -