2.27每日总结
今天对软件工程有了更深的认识,熟悉了java中文件的相关操作,完成了建民老师布置的读取文件最长英语单词链任务
项目结构
//shouwei.java 定义了一个结构体和相关文件读取方法
package piao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class shouwei {
public char start; //单词首字母
public char end; //单词尾字母
public int pos; //单词出现位置
public int sum; //单词是连续的第几个
public String word; //单词
public shouwei() {
}
@Override
public String toString() {
return "shouwei [start=" + start + ", end=" + end + ", pos=" + pos + ", sum=" + sum + ", word=" + word
+ "]";
}
public ArrayList<shouwei> symb(String s) { //将文件的单词分离出 删除符号等影响操作 返回一个list结构体
ArrayList<shouwei> kk=new ArrayList<shouwei>();
File file=new File(s);
StringBuffer end=new StringBuffer();
if(!file.exists()) {
System.out.print("该文件不存在");
System.exit(0);
}
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
String text=null;
while((text=reader.readLine())!=null)
{
end.append(text);
}
} catch (IOException e) {
System.out.println("程序出现问题");
}
StringBuffer result=new StringBuffer();
for(int i=0;i<end.length();i++) {
if(end.charAt(i)>='a'&&end.charAt(i)<='z')
result.append(end.charAt(i));
else if(end.charAt(i)>='A'&&end.charAt(i)<='Z')
result.append(end.charAt(i));
else if(end.charAt(i)==' ')
result.append(end.charAt(i));
else
result.append(" ");
}
int pos1=0;
char start=' ';
char ende;
String word="";
for(int i=1;i<result.length()-1;i++) {
if(result.charAt(i)==' ')
continue;
if(result.charAt(i-1)==' ')
{
word+=result.charAt(i);
start=result.charAt(i);
continue;
}
if(result.charAt(i+1)==' ')
{
shouwei e =new shouwei();
word+=result.charAt(i);
ende=result.charAt(i);
pos1++;
e.start=start;
e.end=ende;
e.pos=pos1;
e.sum=0;
e.word=word;
kk.add(e);
word="";
start=' ';
ende=' ';
continue;
}
if((result.charAt(i)>='a'&&result.charAt(i)<='z')||(result.charAt(i)>='A'&&result.charAt(i)<='Z')) {
word+=result.charAt(i);
}
}
if(kk.size()==0) {
System.out.println("只有一个单词!");
System.exit(0);
}
return kk;
}
public int da(ArrayList<shouwei> kk) { //返回最大单词链长度
int max=0;
int text=1;
for(int i=0;i<kk.size()-1;i++) {
if(kk.get(i).end==kk.get(i+1).start) {
text++;
kk.get(i).sum=text;
}
else text=1;
max=(max>text)?max:text;
}
return max;
}
public String pos(ArrayList<shouwei> kk) { //返回最大的单词链
int pos=0;
int max=0;
String s="";
for(int i=0;i<kk.size();i++) {
if(kk.get(i).sum>max) {
max=kk.get(i).sum;
pos=i;
}
}
while(max>0) {
s+=kk.get(pos-max+2).word+" ";
max--;
}
return s;
}
public int position(ArrayList<shouwei> kk) {
int pos=0;
int max=0;
String s="";
for(int i=0;i<kk.size();i++) {
if(kk.get(i).sum>max) {
max=kk.get(i).sum;
pos=i;
}
}
return pos+1;
}
}
//Piao.java 主函数
package piao;
import java.util.ArrayList;
import java.util.Scanner;
public class Piao {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入文件名:");
String text=sc.next();
ArrayList<shouwei> kk=new shouwei().symb(text);
System.out.println("最大长度是:"+new shouwei().da(kk));
System.out.println("最大单词链为:"+new shouwei().pos(kk));
}
}