/ / pourquoi ne s'est-il pas divisé? - java

pourquoi ne s'est-il pas fendu? - java

je ne comprends pas pourquoi il ne divise pas lechaîne? mon tableau de chaîne exp ne contient rien lorsque je débogue, c'est le fractionnement incorrect? ce que j'essaie de faire est de diviser une expression très simple comme 1 + 2 + 3 puis d'analyser les valeurs, en faisant une calculatrice.

MODIFIER salut, pourquoi je me divise sur chaque personnage estparce que je fais une calculatrice et que j'ai lu quelque chose sur la conversion d'infix en postfix, j'ai donc besoin de diviser la chaîne, puis de parcourir chaque chaîne et de faire la vérification comme indiqué ci-dessous, mais quand je débogue, elle montre l'exp [] est vide

For each token in turn in the input infix expression:

* If the token is an operand, append it to the postfix output.
* If the token is an operator A then:
o While there is an operator B of higher or equal precidence than A at the top of the stack, pop B off the stack and append it to the output.
o Push A onto the stack.
* If the token is an opening bracket, then push it onto the stack.
* If the token is a closing bracket:
o Pop operators off the stack and append them to the output, until the operator at the top of the stack is a opening bracket.
o Pop the opening bracket off the stack.

When all the tokens have been read:

* While there are still operator tokens in the stack:
o Pop the operator on the top of the stack, and append it to the output.


// the main class
public class Main {


public static void main(String[] args) {
calcExpChecker calc = new calcExpChecker("1+2+3+4");
calc.legitExp();
calc.displayPostfix();
}

}
//the class
package javaapplication4;
import java.util.*;


public class calcExpChecker {


private String originalExp; // the orginal display passed
private boolean isItLegitExp; // the whole expression is it legit
private boolean isItBlank; // is the display blank?
private StringBuilder expression = new StringBuilder(50);
private Stack stack = new Stack();//stack for making a postfix string

calcExpChecker(String original)
{
originalExp = original;
}

//check for blank expression
public void isitBlank()
{
if(originalExp.equals(""))
{
isItBlank = true;
}
else
{
isItBlank = false;
}

}

//check for extra operators
public void legitExp()
{
String[] exp = originalExp.split(".");
for(int i = 0 ; i < exp.length ; i++)
{
if(exp[i].matches("[0-9]"))
{
expression.append(exp[i]);
}
else if(exp[i].matches("[+]"))
{
if(stack.empty())
{
stack.push(exp[i]);
}
else
{
while(stack.peek().equals("+"))
{
expression.append(stack.pop());
}
stack.push(exp[i]);
}
}
if (!stack.empty())
{
expression.append(stack.pop());
}
}

}

public void displayPostfix()
{
System.out.print(expression.toString());
}
}

Réponses:

3 pour la réponse № 1

Si vous faites de chaque personnage un délimiteur, qu'y a-t-il entre eux? Rien

par exemple., 1 + 2 + 3 + 4 1 est-il un délimiteur? oui, ok, saisissez tout entre lui et le prochain délimiteur. Prochain délimiteur? +. Rien capturé. Prochain délimiteur? 2. etc etc


2 pour la réponse № 2

Vous voulez partager sur chaque personnage, alors utilisez plutôt string.split("").

for (String part : string.split("")) {
// ...
}

Ou mieux, il suffit d'itérer sur chaque personnage retourné par string.toCharArray().

for (char c : string.toCharArray()) {
// ...
}

Avec les caractères, vous pouvez utiliser un switch déclaration ce qui est mieux qu'un grand if/else bloc.


0 pour la réponse № 3

pourquoi avez-vous besoin de diviser chaque personnage et de ne pas y aller pour chaque dans la chaîne. De cette façon, vous n'avez pas non plus à référencer comme exp [i].

Quoi qu'il en soit, vous pouvez diviser en utilisant "" au lieu de "."


-1 pour la réponse № 4

Confession:

D'accord, je suppose que ma réponse est mauvaise car il y a de subtiles différences entre Java et C # avec ce genre de choses. Peut-être que ça aidera quelqu'un avec le même problème mais en C #!

Btw, en C #, si vous passez dans un RegEx "." vous n'obtenez pas un tableau vide, au lieu de cela, vous obtenez un tableau de blancs (""), un pour chaque limite de caractère dans la chaîne.

modifier

Vous pouvez passer une expression régulière dans le split() fonction:

string expressions = "10+20*4/2";

/* this will separate the string into an array of numbers and the operators;
the numbers will be together rather than split into individual characters
as "" or "." would do;
this should make processing the expression easier
gives you: {"10", "+", "20", "*", "4", "/", "2"} */
foreach (string exp in expressions.split(@"(u002A)|(u002B)|(u002D)|(u002F)"))
{
//process each number or operator from the array in this loop
}

Original

String[] exp = originalExp.split(".");

Vous devez obtenir au moins une chaîne de la valeur de retour de split() (la chaîne non divisée d'origine). Si le tableau de chaînes est vide, la chaîne d'origine était probablement vide.