Fórum chamada de métodos se comportando de forma esquisita [ java/ Eclipse/ recursividade ] #590198
01/01/2018
0
olá pessoa
sou iniciante nesta linguagem, enquanto estudava pelas internets da vida,
me deparei com o conceito de recursividade(pelo que eu entendi e a tal pilha me corrijam se eu estiver errado)
para essa matéria me propus a fazer uma mini calculadora que deixarei o código abaixo.
depois debugando o código vi algo estranho,
no método coloquei condicionais para verificar se há operações a se fazer ou se resta somente um numero(resultado)
a parte estranha:
depois de ser barrado pela condicional // ou seja, não há mais operações a se fazer, resta somente o resultado da operação
chegando ao return -> o método é chamado de novo
sendo que a re-chamada para o método fica dentro do bloco pertencente a condicional do qual já havia sido barrado
O QUE DEIXEI PASSAR?? ?????
código:
sou iniciante nesta linguagem, enquanto estudava pelas internets da vida,
me deparei com o conceito de recursividade(pelo que eu entendi e a tal pilha me corrijam se eu estiver errado)
para essa matéria me propus a fazer uma mini calculadora que deixarei o código abaixo.
depois debugando o código vi algo estranho,
no método coloquei condicionais para verificar se há operações a se fazer ou se resta somente um numero(resultado)
a parte estranha:
depois de ser barrado pela condicional // ou seja, não há mais operações a se fazer, resta somente o resultado da operação
chegando ao return -> o método é chamado de novo
sendo que a re-chamada para o método fica dentro do bloco pertencente a condicional do qual já havia sido barrado
O QUE DEIXEI PASSAR?? ?????
código:
public class Pilha2 {
// Atributos:
private String[] sentenca;
float n1;
float n2;
String resultado;
String novoArg;
//boolean confereOp;
// Métodos:
public String fila(String arg) {
while (arg.charAt(arg.length() - 1) == '*' || arg.charAt(arg.length() - 1) == '/'
|| arg.charAt(arg.length() - 1) == '+' || arg.charAt(arg.length() - 1) == '-') {
arg = arg.substring(0, arg.length() - 1);
}
while (arg.charAt(0) == '*' || arg.charAt(0) == '/' || arg.charAt(0) == '+') {
arg = arg.substring(1, arg.length());
}
for (int i = 0; i < arg.length(); i++) {
if (arg.charAt(i) == '*' || arg.charAt(i) == '/' || arg.charAt(i) == '+' || arg.charAt(i) == '-') {
if (arg.charAt(i + 1) == '*' || arg.charAt(i + 1) == '/' || arg.charAt(i + 1) == '+'
|| arg.charAt(i + 1) == '-') {
arg = "Sua Expressão não pode ser escrita desta forma '" + arg + "'";
return arg;
// throw new IllegalArgumentException("Sua expressão matematica nao pode ser
// escrita desta forma");
}
}
}
int nunOper = 0;
for (int i = 0; i < arg.length(); i++) {
if (arg.charAt(i) == '*' || arg.charAt(i) == '/' || arg.charAt(i) == '+' || arg.charAt(i) == '-') {
nunOper++;
}
}
this.sentenca = new String[nunOper * 2 + 1];
int indiceGravar = 0;
String numero = "";
for (int i = 0; i < arg.length(); i++) {
if (arg.charAt(i) == '*' || arg.charAt(i) == '/' || arg.charAt(i) == '+' || arg.charAt(i) == '-') {
// this.sentenca[indiceGravar] = numero;
numero = "";
indiceGravar++;
this.sentenca[indiceGravar] = Character.toString(arg.charAt(i));
indiceGravar++;
} else {
numero = numero + Character.toString(arg.charAt(i));
this.sentenca[indiceGravar] = numero;
}
}
return this.pilha();
}
// Metodo pilha
public String pilha() {
this.novoArg = "";
/*confereOp = false;
* for (String teste : sentenca) { if (teste.equals("*") || teste.equals("/")) {
* confereOp = true;
*
* } } if (confereOp == false && sentenca.length == 1) { novoArg = sentenca[0];
* return novoArg; }
*/
if (sentenca.length > 1) {
//TESTE PARA OPERADORES * e /
for (int i = 0; i < sentenca.length; i++) {
this.novoArg = "";
if (this.sentenca[i].equals("*")) {
this.n1 = Float.parseFloat(this.sentenca[i - 1]);
this.n2 = Float.parseFloat(this.sentenca[i + 1]);
this.resultado = Float.toString(this.n1 * this.n2);
// 2/22*5*51/65
int j = 0;
while (j < i - 1) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
this.novoArg = this.novoArg + this.resultado;
j = i + 2;
while (j > i + 1 && j < this.sentenca.length) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
this.fila(this.novoArg);
} else if (this.sentenca[i].equals("/")) {
this.n1 = Float.parseFloat(this.sentenca[i - 1]);
this.n2 = Float.parseFloat(this.sentenca[i + 1]);
this.resultado = Float.toString(this.n1 / this.n2);
// 2/22*5*51/65
int j = 0;
while (j < i - 1) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
this.novoArg = this.novoArg + this.resultado;
j = i + 2;
while (j > i + 1 && j < this.sentenca.length) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
this.fila(this.novoArg);
}
}
//TESTE PARA OPERADORES + e -
for (int i = 0; i < sentenca.length; i++) {
this.novoArg = "";
if (this.sentenca[i].equals("+")) {
//ADIÇÃO
this.n1 = Float.parseFloat(this.sentenca[i - 1]);
this.n2 = Float.parseFloat(this.sentenca[i + 1]);
this.resultado = Float.toString(this.n1 + this.n2);
//REINSCRIÇÃO DA EXPRESAO MATEMATICA ANTERIORES A CONTA
int j = 0;
while (j < i - 1) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
// ADIÇÃO A EXPRESSAO DA CONTA
this.novoArg = this.novoArg + this.resultado;
//ADIÇÃO A EXPRESSAO DOS ARGUMENTOS POTERIORES A CONTA
j = i + 2;
while (j > i + 1 && j < this.sentenca.length) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
//CHAMA O PRÓXIMO PROCESSO
this.fila(this.novoArg);
} else if (this.sentenca[i].equals("/")) {
//SUBTRAÇÃO
this.n1 = Float.parseFloat(this.sentenca[i - 1]);
this.n2 = Float.parseFloat(this.sentenca[i + 1]);
this.resultado = Float.toString(this.n1 - this.n2);
//ADIÇÃO A EXPRESSAO DOS ARGUMENTOS ANTERIORES A CONTA
int j = 0;
while (j < i - 1) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
//ADIÇÃO A EXPRESSAO DA CONTA
this.novoArg = this.novoArg + this.resultado;
//ADIÇÃO A EXPRESSAO DOS ARGUMENTOS POTERIORES A CONTA
j = i + 2;
while (j > i + 1 && j < this.sentenca.length) {
this.novoArg = this.novoArg + this.sentenca[j];
j++;
}
//CHAMA O NOVO PROCESSO
this.fila(this.novoArg);
}
}
}
return this.sentenca[0];
}
// Métodos Especiais:
public void setSentenca(String[] sentenca) {
this.sentenca = sentenca;
}
}
Henrique Dias
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)