题目3:文本文件单词的检索与计数(代码实现)

题目1:学生成绩档案管理系统(代码实现)

一、信息类(pojo包)

1.单词类(Word.java)

public class Word {
    private ArrayList<String> list = new ArrayList<>();

    public Word() {
    }

    public Word(ArrayList<String> list) {
        this.list = list;
    }

    public ArrayList<String> getList() {
        return list;
    }

    public void setList(ArrayList<String> list) {
        this.list = list;
    }
}

二、数据类和文档交互类

1.文档交互类(Reader.java)

public class Reader {
    private BufferedReader br;
    private File file = new File("E:\\work\\idea project\\Data_Sructures\\Project_3\\src\\word.txt");

    public Reader() {
    }

    public ArrayList<String> reader() {
        ArrayList<String> list = new ArrayList<>();
        //创建字符缓冲输入流
        try {
            br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file)));
            String line = null;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}

2.数据库类(Database.java)

public class Database {
    private Reader reader = new Reader();
    private Word word = new Word();
    private ArrayList<Word> list = new ArrayList<>();

    public Database() {
        ArrayList<String> list1 = reader.reader();
        ArrayList<String> list2 = new ArrayList<>();
        for (String s : list1) {
            String student[] = s.split(" ");
            for (int i = 0; i < student.length; i++) {
                list2.add(student[i]);
            }
            ArrayList<String> list3 = new ArrayList<>();
            for (String s1 : list2) {
                list3.add(s1);
            }
            list2.clear();
            list.add(new Word(list3));
        }
    }

    public ArrayList<Word> getList() {
        return list;
    }
}

三、 数据访问对象类(dao包)

1.单词数据访问对象类(WordDao.java)

public class WordDao {
    private Database database;

    public WordDao(Database database) {
        this.database = database;
    }

    public Database getDatabase() {
        return database;
    }
}

四、服务端类(Service包)

1.系统服务端类(WordService.java)

public class WordService {
    private Scanner key = new Scanner(System.in);
    private WordDao wordDao;
    private KMP_Algorithm kmp_algorithm = new KMP_Algorithm();
    private BF_Algorithm bf_algorithm = new BF_Algorithm();

    public WordService(Database database) {
        wordDao = new WordDao(database);
    }

    /**
     * 主菜单
     */
    public void start() {
        System.out.println("请输入您的查找方式:");
        System.out.println("1.单个单词查找");
        System.out.println("2.字符串子串查询");
        System.out.println("3.退出");
        String chooice = key.next();
        switch (chooice) {
            case "1":
                single();
                break;
            case "2":
                substring();
                break;
            case "3":
                System.out.println("谢谢使用");
                break;
            default:
                System.out.println("输入错误,请重新输入");
                start();
        }
    }

    /**
     * 单个单词查找
     */
    public void single() {
        int array[] = new int[2];
        ArrayList<int[]> list = new ArrayList<>();
        System.out.println("请输入您要查找的单词:");
        String s = key.next();
        for (Word word : wordDao.getDatabase().getList()) {
            for (int i = 0; i < word.getList().size(); i++) {
                if (word.getList().get(i).equals(s)) {
                    array = new int[]{wordDao.getDatabase().getList().indexOf(word) + 1, i + 1};
                    list.add(array);
                }
            }
        }
        System.out.println("共找到单词" + s + " " + list.size() + "次");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("第" + (i + 1) + "次:第" + list.get(i)[0] + "行第" + list.get(i)[1] + "个单词");
        }
        System.out.println("输入任意字符回到主菜单");
        String s1 = key.next();
        System.out.println("返回主菜单");
        start();
    }

    /**
     * 字符串子串查询
     */
    public void substring() {
        String[] array = new String[3];
        ArrayList<String[]> list = new ArrayList<>();
        System.out.println("请输入你要查找的字符串:");
        String s = key.next();
        System.out.println("请输入你要使用的算法:");
        System.out.println("1.朴素模式匹配算法");
        System.out.println("2.KMP算法");
        String s1 =key.next();
        for (Word word : wordDao.getDatabase().getList()) {
            for (int i = 0; i < word.getList().size(); i++) {
                int n;
                if(s1.equals("1")){
                    n = bf_algorithm.bf(s, word.getList().get(i));
                }else{
                    n=kmp_algorithm.kmpMatch(s,word.getList().get(i));
                }
                if (n != -1) {
                    array = new String[]{word.getList().get(i),
                            String.valueOf(wordDao.getDatabase().getList().indexOf(word) + 1),
                            String.valueOf(i + 1)};
                    list.add(array);
                }
            }
        }
        System.out.println("共找到字符串" + s + "的子串" + list.size() + "次");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("第" + (i + 1) + "个字串" + list.get(i)[0] + ":第" + list.get(i)[1] + "行第" + list.get(i)[2] + "个单词");
        }
        System.out.println("输入任意字符回到主菜单");
        String s2 = key.next();
        System.out.println("返回主菜单");
        start();
    }
}

五、单词匹配算法类(algorithm包)

1.朴素模式匹配算法类(BF_Algorithm.java)

同上

六、主类(main包)

1.主类(Main.java)

public class Main {
    public static void main(String[] args) {
        Database database = new Database();
        WordService wordServer = new WordService(database);
        wordServer.start();
    }
}

八、储存信息的文件(word.txt)

towards the end of the twentieth century a celebrated Romanian-French
philosopher was invited to speak in Zurich he was introduced with
rhetorical pomp and flattering comparisons to the likes of
Kierkegaard and Schopenhauer
the speaker smiled and immediately confounded his German interpreter
by beginning his presentation with the words Mais je ne suis
deconneur but just a joker
a few of his critics might agree but they would be wrong for Emil
Cioran is very much worthy of inclusion in the line of the great
French and European moral philosophers and writers of maxims
stretching back to Montaigne Chamfort Pascal and La Rochefoucauld
cioran was born in Rasinari Romania in April 1911 his father was a
Greek Orthodox priest
both facts were to be key in his later work the writer’s Romanian
origins are often taken as the source of a brooding Romantic
fatalistic temperament while his father ecclesiastical calling finds
echoes in his son unswerving preoccupation with themes of religion
sainthood and the dangers and joys of atheism
in 1934 at the age of only 23 he published his first book in Romanian
on the Heights of Despair the book contains in embryo much of the
lucidly bleak nihilistic thinking that he’d develop throughout his
life
he explained that writing had been an alternative to shooting himself
this is an author to read at moments of despair and melancholy
he does depress us merely makes us feel less alone with our sorrows
here is a peak inside his work it is not worth the bother of killing
yourself since you always kill yourself too late only optimists
commit suicide optimists who no longer succeed at being optimists the
others having no reason to live why would they have any to die
write books only if you are going to say in them the things you would
never dare confide to anyone and what do you do from morning to night
endure myself
1937 proved a pivotal year for Cioran he moved to Paris and soon
became a French citizen he never returned to the country of his birth
it was a move that provided him with another reason to be melancholic
exile
and yet he asserted that anyone who does feel themselves to be an
exile has no imagination only the village idiot thinks they belong he
noted cioran sat out the Second World War in Paris
in its aftermath he approached the famous publishing house Gallimard
with his first work in French, A Short History of Decay, published in
1949 writing in French was he said like writing a love letter with a
dictionary
the book became a bestseller the first in a series of devastatingly
wicked and dark texts composed mainly of aphorisms and tart short
essays each title comes as a provocation or a punch Syllogisms of
Bitterness The Temptation to Exist and his masterpiece The Trouble
with Being Born
each book deals relentlessly with themes of illness death and suicide
it was a rather touching irony that the author lived to the ripe old
age of 84 by the time Cioran died in 1995 he had become a cult in
France attracting the sort of faddish attention he witheringly
denounced in his work every life he maintained is utterly peculiar
and wholly unimportant
in the age of Walt Disney this kind of darkness matters cioran
writing belongs in the line of those great aloof European
miserabilists including La Rochefoucauld Chamfort Leopardi Nietzsche
and Beckett like them he saw civilization as an absurd distraction
from the ultimate meaninglessness of existence
only an idiot could think there is a point to any of this he insisted
but he always kept his wit and good cheer one of his greatest sources
of sorrow was that he could sleep very well and was often up
he walk around the streets of Paris until dawn he noted sardonically
in The Trouble with Being Born what is that one crucifixion compared
to the daily kind any insomniac suffers
cioran was obsessed by suicide. But better to live he wrote and yet
think and act as though one were already dead After all he said why
deprive oneself of the consoling idea of no longer being around
continuing to Live is possible only because of the deficiencies of
our imagination and our memories he wrote am simply an accident why
take it all so seriously
but suicide was a constant relief as an idea we dread the future only
when we are not sure we can kill ourselves when we want to having
always lived in fear of being surprised by the worst
have tried in every circumstance to get a head start flinging myself
into misfortune long before it occurred our age is notably optimistic
and it makes us suffer hugely from being so
we are all in private a lot sadder than we are allowed to admit
writers like Cioran provide an occasion for the sadness inside all of
us to be communally expressed and thereby a little but only a little
diminished and softened in his beautiful book the Trouble with Being
Born cioran wrote can be friends with people only when they are at
their lowest point and have neither the desire nor the strength to
restore their habitual sentimental illusions It is precisely in that
kind of mood into which all thoughtful people must sink on a fairly
regular basis that we can be blessed to find the dark and consoling
works of Emil Cioran waiting for us we love bringing you these films
if you want to help us to keep bringing you thoughtful content

八、README文件(README.txt)

数据结构实践
题目3:文本文件单词的检索与计数
编程语言:Java
编译环境:JDK1.8
开发工具:IntelliJ IDEA

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值