Friends, I have a small problem in 'converting' an algorithm.
In the algorithm in .net
I have the following conditional structure:
if (')' == caracter){
String item = pilha.Pop().ToString();
while (!item.Equals("(")){
resultado += item;
item = pilha.Pop().ToString();
}
}
Throwing everything to javaScript
would look like this:
if (')' == caracter){
var item = pilha.pop();
while (item != '('){
resultado += item; //Corrigido
item = pilha.pop();
}
}
In my understanding while (!item.Equals("(")){
would be the equivalent of while (item != '('){
So in this condition it is entering infinite looping, ie the algorithm is not running the way it should ..... Someone could give me a help about the Equals
function and the !
operator on .net
?
Was my interpretation "very" erroneous?
------- Interpretation of the whole code:
.NET
private String funcao(String expressao){
String resultado = null;
Stack pilha = new Stack();
char caracter;
int prioridade = 0;
for(int i = 0; i < expressao.Length; i++){
caracter = expressao[i];
if(IsOperando(caracter)){
resultado += caracter;
}else if (IsOperador(caracter)){
prioridade = ObterPrioridade(caracter);
while((pilha.Count != 0) && (ObterPrioridade(Convert.ToChar(pilha.Peek())) >= prioridade)){
resultado += pilha.Pop().ToString();
}
pilha.Push(caracter);
}else if ('(' == caracter){
pilha.Push(caracter);
}else if (')' ==caracter){
String item = pilha.Pop().ToString();
while (!item.Equals("(")){
resultado += item;
item = pilha.Pop().ToString();
}
}
}
while(pilha.Count != 0){
resultado += pilha.Pop().ToString();
}
return resultado;
}
Interpreted for JS
:
function funcao(){
var arrayDeCaracteres = "((A + B) * C – (D – E)) ^ (F – G)";
var pilha = new Array;
var prioridade = 0;
var caracter = ""; var resultado = "";
for(i = 0; i < arrayDeCaracteres.length; i++){
caracter = arrayDeCaracteres[i];
if(operando(caracter)){
resultado += caracter;
}else if (operador(caracter)){
prioridade = obterPrioridade(caracter);
aux = pilha.pop();
aux2 = obterPrioridade(aux);
pilha.push(aux);
while((pilha.length > 1) && (obterPrioridade(aux2 >= prioridade))){
resultado += pilha.pop();
}
//Insere o objeto no topo da pilha
pilha.push(caracter);
}else if ('(' == caracter){
//Insere o objeto no topo da pilha
pilha.push(caracter);
}else if (')' == caracter){
var item = pilha.pop();
while (item != '('){
resultado += item;
//Recupera e remove o objeto do topo da pilha
item = pilha.pop();
}
}
}
while(pilha.length > 1){
resultado += pilha.pop();
}
}
Functions ObterPrioridade
, IsOperando
and IsOperador
in .NET
:
private int ObterPrioridade(char caracter){
int retorno= 0;
String pri2 = "+-";
String pri3 = "*/";
if('(' == caracter){
retorno = 1;
}else if(pri2.IndexOf(caracter) >= 0) {
retorno = 2;
}else if(pri3.IndexOf(caracter) >= 0){
retorno = 3;
}else if ('^' == caracter){
retorno = 4;
}
return retorno;
}
private bool IsOperando(char caracter){
String letras = "ABCDEFGHIJKLMNOPQRSTUVXZ";
return (letras.IndexOf(caracter) >= 0);
}
private bool IsOperador(char caracter){
String operadores = "+-*/^";
return (operadores.IndexOf(caracter) >= 0);
}
Functions obterPrioridade
, operando
and operador
in JS
:
function operando(caracter){
var regex = /^[a-zA-Z]+$/;
if(regex.test(caracter)){
return true;
}else{
return false;
}
}
function operador(caracter){
var regex = /^[*+-–/]+$/gi;
if(regex.test(caracter)){
return true;
}else{
return false;
}
}
function obterPrioridade(caracter){
var retorno= 0;
var regex1 = /^[+-–]+$/gi;
var regex2 = /^[*/]+$/gi;
if('(' == caracter){
retorno = 1;
}else if(regex1.test(caracter)){
retorno = 2;
}else if(regex2.test(caracter)){
retorno = 3;
}else if ('^' == caracter){
retorno = 4;
}
return retorno;
}
EXPLANATION OF ALGORITHM:
The algorithm predicts the conversion of a mathematical expression InFixa in PostFixa ....
What is a InFixa expression ?
Natural expressions, as they are usually written, type: ( a + b ) * ( c - d )
What is a PostFixa expression ?
Expressions where operators are positioned after operations so that parentheses are not needed, type a b + c d - *
It has operators priority rule and everything, it's very complicated, but the algorithm is somewhat simple