-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestePalavra.java
More file actions
124 lines (107 loc) · 4.12 KB
/
Copy pathTestePalavra.java
File metadata and controls
124 lines (107 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.InputMismatchException;
public class TestePalavra {
// ANDRESSA MARQUES FERNANDES - 31786812 - TURMA 03H
static final Scanner input = new Scanner(System.in);
static int opcao = 0;
static ABB abb = new ABB();
static int totalOcorrencias = 0;
public static void main(String[] args) {
do {
do {
try {
System.out.println("\nMenu principal \n" +
"1- Carregar o texto \n" +
"2- Contador de palavras \n" +
"3- Busca por palavra \n" +
"4- Exibição do texto \n" +
"5- Encerrar"
);
System.out.print("Sua opção: ");
opcao = input.nextInt();
break;
}
catch (InputMismatchException e) {
System.out.println("Opcão inválida!");
}
input.nextLine();
} while(opcao <= 0);
switch (opcao) {
case 1:
carregaArquivo(abb);
break;
case 2:
if (abb.getRoot() != null) {
System.out.println("\nTotal de palavras: " + contaPalavras(abb.getRoot()));
} else {
System.out.println("\nCarregue o texto primeiro!");
}
break;
case 3:
if (abb.getRoot() != null) {
buscaPalavra(abb);
} else {
System.out.println("\nCarregue o texto primeiro!");
}
break;
case 4:
if (abb.getRoot() != null) {
exibeTexto(abb);
} else {
System.out.println("\nCarregue o texto primeiro!");
}
break;
case 5:
System.out.println("\nEncerrando...");
break;
default:
System.out.println("\nOpção inválida!");
}
} while (opcao != 5);
}
public static void carregaArquivo(ABB abb) {
try {
FileReader file = new FileReader("C:\\Users\\Andressa\\Desktop\\information systems\\estrutura_dados\\Projeto2\\src\\construcao_chico_buarque.txt");
Scanner reader = new Scanner(file);
String st;
while (reader.hasNext()) {
st = reader.next();
Palavra palavra = new Palavra(st);
Node node = new Node(palavra);
if (abb.busca(node) != null) {
abb.busca(node).setOcorrencias();
} else {
abb.insere(node);
}
}
System.out.println("\nTexto carregado.");
reader.close();
} catch (FileNotFoundException e) {
System.out.println("\nArquivo não encontrado.");
}
}
public static void buscaPalavra(ABB abb) {
System.out.print("Busque uma palavra: ");
String palavra = input.next();
Node node = new Node(new Palavra(palavra));
if (abb.busca(node) == null) {
System.out.println("\nNão há nenhuma ocorrência desta palavra.");
} else {
System.out.println("\nQuantidade de ocorrências: " + abb.busca(node).getOcorrencias());
}
}
public static void exibeTexto(ABB abb) {
abb.executaInOrdem(abb.getRoot());
}
public static int contaPalavras(Node no) {
if (no == null) {
return 0;
}
contaPalavras(no.getLeft());
contaPalavras(no.getRight());
totalOcorrencias += no.getOcorrencias();
return totalOcorrencias;
}
}