-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39b50fd
commit 81e91e8
Showing
14 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>TrabalhoPilhaOriginal</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding//src/com/senac/algoritmos/AvaliadorRPN.java=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
* Especifica��o do Trabalho sobre Pilhas | ||
|
||
Implementar um sistema onde o usu�rio insere express�es aritm�ticas (ex. 20 * (5 + 4)) | ||
pelo teclado e o sistema responde com o valor correspondente (ex. "Resultado = 180"). | ||
|
||
O programa termina quando o usu�rio escreve a palavra "fim". | ||
|
||
* Classes auxiliares | ||
|
||
Para esse sistema, � preciso utilizar uma classe de Pilha para o algoritmo de | ||
c�lculo de express�o aritm�tica em formato p�s-fixo (RPN - Nota��o Polonesa Reversa), | ||
e uma Pilha para o algoritmo de invers�o de nota��o de express�o matem�tica. | ||
|
||
S�o fornecidas todas as classes necess�rias para a avalia��o da express�o RPN, e | ||
devem ser constru�das as classes para a invers�o de nota��o. | ||
|
||
Caso deseje, n�o � necess�rio utilizar as classes em anexo, e pode criar as suas | ||
pr�prias classes. | ||
|
||
* Algoritmo para invers�o de nota��o infixa para p�s-fixa | ||
|
||
1. Atribuir prioridades aos operadores que ser�o utilizados: | ||
+,-: 1 | ||
*,/: 2 | ||
(: 3 | ||
2. Enquanto existir um elemento (operador ou operando) na entrada: | ||
2.1. Ler o pr�ximo elemento da entrada. | ||
2.2. Se for um operando, envia para a sa�da. | ||
2.3. Se for um operador: | ||
2.3.1. Se for o operador ")", desempilhar at� que o | ||
operador "(" seja o operador "desempilhado"; | ||
a cada operador desempilhado, enviar para a | ||
sa�da (com exce��o do "("). | ||
2.3.2. Sen�o, se o operador tiver prioridade MAIOR | ||
que o operador do topo da pilha, ou se a pilha | ||
estiver vazia, ou o operador do topo da pilha | ||
for "(", empilhar o operador. | ||
2.3.3. Sen�o, dempilhar (enviando para a sa�da) os | ||
operadores da pilha at� que o operador tenha | ||
prioridade MAIOR que o operador do topo, ou | ||
que o operador do topo da pilha seja "(", | ||
ou que a pilha esteja vazia. Empilhar o operador. | ||
3. Desempilhar os operadores da pilha enviando para a sa�da at� | ||
que a pilha fique vazia. Nenhum desse operadores pode ser o "(". | ||
|
||
NOTA: Lembre-se de reportar ao usu�rio as situa��es onde a express�o n�o foi | ||
corretamente digitada! | ||
|
||
* Data da Entrega do Trabalho: 20/10/2013 | ||
|
||
* Forma de entrega do trabalho | ||
|
||
O sistema deve ser entregue utilizando o Github, no reposit�rio que j� est� sendo | ||
utilizado para a disciplina. | ||
|
||
Neste reposit�rio, deve existir um diret�rio com o nome "Planilha", onde ser� | ||
armazenada as classes do sistema. Devem ser utilizados "pacotes" para separar | ||
testes unit�rios, classes do sistema, classes de estruturas de dados e algoritmos. | ||
|
||
A sugest�o para a estrutura de diret�rios � a seguinte: | ||
|
||
Planilha/ | ||
com/ | ||
senac/ | ||
estruturas/ | ||
test/ | ||
algoritmos/ | ||
test/ | ||
|
||
Quando o trabalho estiver pronto, marcar com a "tag" ALG2_T2_X, substituindo | ||
"X" pelo n�mero da "release", que ser� um n�mero inteiro, come�ando em "1", | ||
por exemplo, ALG2_T2_1, ALG2_T2_2, e assim sucessivamente. | ||
|
||
Ser� avaliada a �ltima "tag" entregue. | ||
|
||
O trabalho ser� considerado "entregue" com o envio de um email para o endere�o: | ||
|
||
[email protected] | ||
|
||
O email a ser enviado deve ter a seguinte estrutura: | ||
|
||
Subject: [<turma>] Entrega do trabalho de Pilhas | ||
|
||
Prof. | ||
|
||
O trabalho de pilhas pode ser encontrado no reposit�rio: | ||
|
||
<link do reposit�rio> | ||
|
||
Sob a tag <nome_da_tag>. | ||
|
||
<nome_do_aluno> | ||
|
||
Substitua os itens entre "<>" pelo valor apropriado. A "turma" � o | ||
nome que estamos chamando as turmas, e � semelhante ao nome do reposit�rio, | ||
sendo, por exemplo, Alg2_6T a turma de "Algoritmos 2, 6a. a Tarde". | ||
|
||
* Como o trabalho ser� avaliado | ||
|
||
- Corre��o da implementa��o | ||
- Organiza��o do c�digo | ||
- Ader�ncia a essa especifica��o | ||
|
||
Boa sorte, | ||
|
||
Rafael |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>TrabalhoPilha</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.senac; | ||
|
||
import java.util.Scanner; | ||
|
||
import com.senac.algoritmos.AvaliadorRPN; | ||
|
||
import static java.lang.System.*; | ||
|
||
public class Planilha { | ||
public static void main(String[] args) throws Exception { | ||
|
||
Scanner entradaTeclado = new Scanner(System.in); | ||
String expressao = ""; | ||
|
||
out.println("Insira uma expressão ou digite a palavra 'fim' para finalizar com programa"); | ||
|
||
while (entradaTeclado.hasNext()) { | ||
expressao = entradaTeclado.nextLine(); | ||
if (expressao.equals("fim")){ | ||
out.println("Programa finalizado !"); | ||
System.exit(0); | ||
} | ||
else{ | ||
AvaliadorRPN.InversorPosFixo(expressao); | ||
} | ||
} | ||
|
||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
TrabalhoPilha/src/com/senac/algoritmos/AvaliadorRPN.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.senac.algoritmos; | ||
|
||
import java.util.Scanner; | ||
|
||
import com.senac.estruturas.PilhaCheia; | ||
import com.senac.estruturas.PilhaOperador; | ||
import com.senac.estruturas.PilhaOperando; | ||
import com.senac.estruturas.PilhaVazia; | ||
|
||
public class AvaliadorRPN { | ||
public static double avalia(String expressao) throws PilhaCheia, | ||
PilhaVazia, InvalidOperator { | ||
PilhaOperando p = new PilhaOperando(50); | ||
|
||
Scanner sc = new Scanner(expressao); | ||
|
||
while (sc.hasNext()) { | ||
if (sc.hasNextInt()) { | ||
p.push(sc.nextInt()); | ||
} else { | ||
String op = sc.next(); | ||
double rhs = p.pop(); | ||
double lhs = p.pop(); | ||
p.push(ExecOperador(op.charAt(0), lhs, rhs)); | ||
} | ||
} | ||
|
||
return p.pop(); | ||
} | ||
|
||
public static void InversorPosFixo(String expressao) throws PilhaCheia, | ||
PilhaVazia, InvalidOperator { | ||
Scanner sc = new Scanner(expressao); | ||
|
||
PilhaOperador p = new PilhaOperador(50); | ||
String saida = ""; | ||
|
||
while (sc.hasNext()) { | ||
|
||
if (sc.hasNextInt()) { | ||
saida += " " + sc.next(); | ||
} | ||
|
||
else { | ||
String operadorLido = sc.next(); | ||
|
||
if (FechaParentes(operadorLido)) { | ||
do { | ||
saida += " " + p.pop(); | ||
|
||
} while (!AbreParenteses(p.peek())); | ||
p.pop(); | ||
} | ||
|
||
else { | ||
if (p.isEmpty()) { | ||
|
||
p.push(operadorLido); | ||
|
||
} else if (Prioridade(operadorLido) > Prioridade(p.peek())) | ||
p.push(operadorLido); | ||
|
||
else { | ||
while (!p.isEmpty() | ||
&& Prioridade(operadorLido) < Prioridade(p | ||
.peek()) && !AbreParenteses(p.peek())) { | ||
if (AbreParenteses(p.peek())) | ||
p.pop(); | ||
else | ||
saida += " " + p.pop(); | ||
} | ||
p.push(operadorLido); | ||
} | ||
} | ||
} | ||
} | ||
|
||
while (!p.isEmpty()) { | ||
if (AbreParenteses(p.peek())) | ||
p.pop(); | ||
else | ||
saida += " " + p.pop(); | ||
} | ||
|
||
System.out.println("Notação Polonesa Reversa: " + saida); | ||
System.out.println(AvaliadorRPN.avalia(saida)); | ||
} | ||
|
||
public static boolean AbreParenteses(String op) { | ||
return op.equals("("); | ||
} | ||
|
||
public static boolean FechaParentes(String op) { | ||
return op.equals(")"); | ||
|
||
} | ||
|
||
public static int Prioridade(String operador) throws InvalidOperator { | ||
int precedencia = 0; | ||
switch (operador.charAt(0)) { | ||
case '(': | ||
precedencia = 3; | ||
break; | ||
|
||
case '*': | ||
precedencia = 2; | ||
break; | ||
|
||
case '/': | ||
precedencia = 2; | ||
break; | ||
|
||
case '+': | ||
precedencia = 1; | ||
break; | ||
|
||
case '-': | ||
precedencia = 1; | ||
break; | ||
|
||
default: | ||
throw new InvalidOperator(operador.charAt(0)); | ||
} | ||
|
||
return precedencia; | ||
} | ||
|
||
private static double ExecOperador(char op, double lhs, double rhs) | ||
throws InvalidOperator { | ||
switch (op) { | ||
case '+': | ||
return lhs + rhs; | ||
case '-': | ||
return lhs - rhs; | ||
case '*': | ||
return lhs * rhs; | ||
case '/': | ||
return lhs / rhs; | ||
default: | ||
throw new InvalidOperator(op); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.senac.algoritmos; | ||
|
||
public class InvalidOperator extends Exception { | ||
public InvalidOperator(char op) | ||
{ | ||
super(String.format("Operador invalido: %c",op)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.senac.estruturas; | ||
|
||
public class PilhaCheia extends Exception { | ||
public PilhaCheia() | ||
{ | ||
super("Tentativa de inclus‹o em uma pilha cheia."); | ||
} | ||
} |
Oops, something went wrong.