Calculatrice Java avec plusieurs opérations


J'ai ce qui suit jusqu'à présent qui fonctionne bien. Je suis sûr qu'il existe probablement un moyen plus facile de le faire, mais ce que je devrai changer/modifier, c'est la méthode supérieure de Matheq. La méthode mathématique effectue les opérations mathématiques simples.

Il fonctionne avec n'importe quelle opération avec+, -, * et /.

Mon problème est de résoudre une équation plus grande telle que 10 - 10 / 5 + 3. Cela résout cependant 10 / 5 + 65 * 2 correctement. La raison en est que chaque partie, nombres et opérations, sont divisés en une chaîne tableau. Une fois chaque opération terminée, les nombres et l'opération sont remplacés par le résultat de cette équation. Peut sembler plus déroutant, mais je ne pouvais pas penser à une meilleure façon. La raison pour laquelle il ne peut pas résoudre l'autre équation est à cause de la façon dont j'ai mappé les chaînes dans un tableau de chaînes.

Exemple de tableaux de chaînes Ex. avec 10 - 10 / 5 + 3 Chaîne = { 10, -, 10, /, 5, +, 3 } après les opérations fait d'abord la division puis de gauche à droite la soustraction puis l'addition Chaîne = { 8, 8, 2, 2, 2, 5, 5 }

Voici mon code et quelqu'un s'il vous plait m'aider:

RÉVISÉ révisé, maintenant il fonctionne avec ci-dessus mais a encore quelques problèmes avec de LONGUES équations. Un court exemple est qu'il résout 2 * 2 * 2 * 2 divisé par 5 très bien mais si le changer ainsi 10 - 2 * 2 * 2 * 2 divisé par 5 Je me trompe de réponse.

 public class Matheq {

String fnum = null;
String lnum = null;
String total = null;

public String Matheq(String mathoperation) {

    String mathoperation= "6 * 3 - 4 * 2";

    mathoperation = mathoperation.replaceAll(",", "");
    mathoperation = mathoperation.replaceAll("plus", "+");
    mathoperation = mathoperation.replaceAll("minus", "-");
    mathoperation = mathoperation.replaceAll("times", "*");
    mathoperation = mathoperation.replaceAll("divided by", "dividedby");
    mathoperation = mathoperation.replaceAll("percent of", "percentof");
    String[] splitstr = mathoperation.split(" ");
    while(splitstr.length>1){
    for(int i=0; i<splitstr.length; i++) {
        System.out.println("Get value: " + splitstr[i]);
        if(splitstr[i].indexOf("percentof") >= 0) {
        String buildit = splitstr[i-1] + " percent of " + splitstr[i+1];
        String done = math(buildit);
        System.out.println("Percentage operation: " + splitstr[i-1] + " percent of " + splitstr[i+1] + "=" + done);
        splitstr[i] = done;
        splitstr[i-1] = "";
        splitstr[i+1] = "";
        ArrayList<String> list = new ArrayList<String>();
        for(String s : splitstr){
            if(!s.equals("")){
                list.add(s);
            }
        }
        splitstr = list.toArray(new String[list.size()]);
        }
    }
    for(int i=0; i<splitstr.length; i++) {
        System.out.println("Get value: " + splitstr[i]);
        if(splitstr[i].indexOf("dividedby") >= 0) {
            String buildit = splitstr[i-1] + " divided by " + splitstr[i+1];
            String done = math(buildit);
            System.out.println("Division operation: " + splitstr[i-1] + " divided by " + splitstr[i+1] + "=" + done);
            splitstr[i] = done;
            splitstr[i-1] = "";
            splitstr[i+1] = "";
            ArrayList<String> list = new ArrayList<String>();
            for(String s : splitstr){
                if(!s.equals("")){
                    list.add(s);
                }
            }
            splitstr = list.toArray(new String[list.size()]);
        }
    }
    for(int i=0; i<splitstr.length; i++) {
        System.out.println("Get value: " + splitstr[i]);
        if(splitstr[i].indexOf("*") >= 0) {
            String buildit = splitstr[i-1] + " * " + splitstr[i+1];
            String done = math(buildit);
            System.out.println("Multiplication operation: "+ splitstr[i-1] + " * " + splitstr[i+1] + "=" + done);
            splitstr[i] = done;
            splitstr[i-1] = "";
            splitstr[i+1] = "";
            ArrayList<String> list = new ArrayList<String>();
            for(String s : splitstr){
                if(!s.equals("")){
                    list.add(s);
                }
            }
            splitstr = list.toArray(new String[list.size()]);
        }
    }
    for(int i=0; i<splitstr.length; i++) {
            System.out.println("Get value: " + splitstr[i]);
        if(splitstr[i].indexOf("+") >= 0) {
            String buildit = splitstr[i-1] + " + " + splitstr[i+1];
            String done = math(buildit);
            System.out.println("Addition operation: " + splitstr[i-1] + " + " + splitstr[i+1] + "=" + done);
            splitstr[i] = done;
            splitstr[i-1] = "";
            splitstr[i+1] = "";
            ArrayList<String> list = new ArrayList<String>();
            for(String s : splitstr){
                if(!s.equals("")){
                    list.add(s);
                }
            }
            splitstr = list.toArray(new String[list.size()]);
        }
    }
        for(int i=0; i<splitstr.length; i++) {
            System.out.println("Get value: " + splitstr[i]);
            if(splitstr[i].indexOf("-") >= 0) {
            String buildit = splitstr[i-1] + " - " + splitstr[i+1];
            String done = math(buildit);
            System.out.println("Subtraction operation: " + splitstr[i-1] + " - " + splitstr[i+1] + "=" + done);
            splitstr[i] = done;
            splitstr[i-1] = "";
            splitstr[i+1] = "";
            ArrayList<String> list = new ArrayList<String>();
            for(String s : splitstr){
                if(!s.equals("")){
                    list.add(s);
                }
            }
            splitstr = list.toArray(new String[list.size()]);
        }   
    }
    for(int i=0; i<splitstr.length; i++) {  
        System.out.println("Final operation: " + total + " " + splitstr[i]);
    }
    }
    return total;

}

private String math(String mathoperation) {
    // TODO Auto-generated method stub
    if(mathoperation.contains("percent of")){
        mathoperation = mathoperation.replaceAll("percent of", "%");
        int str = mathoperation.indexOf("%");
        System.out.println(str);
        fnum = mathoperation.substring(0, str-1);
        fnum = fnum.replaceAll(" ", "");
        fnum = "." + fnum;
        System.out.println(fnum);
        double intfnum = Double.parseDouble(fnum);
        System.out.println(intfnum);
        int lastind = mathoperation.length();
        System.out.println(lastind);
        lnum = mathoperation.substring(str+1, lastind);
        lnum = lnum.replaceAll(" ", "");
        System.out.println(lnum);
        double intlnum = Double.parseDouble(lnum);
        System.out.println(intlnum);
        double tot = intlnum * intfnum;
        System.out.println(tot);
        total = Double.toString(tot);
        if(total.length() == 3){
            total = total + "0";
        }
        if(total.length() > 5){
            total = total.substring(0, 4);
        }
        total = total.replace("0.", "");
        System.out.println("Total:" + total);
        } else 
    if(mathoperation.contains("-")){
        int str = mathoperation.indexOf("-");
        System.out.println(str);
        fnum = mathoperation.substring(0, str-1);
        fnum = fnum.replaceAll(" ", "");
        System.out.println(fnum);
        double intfnum = Double.parseDouble(fnum);
        System.out.println(intfnum);
        int lastind = mathoperation.length();
        System.out.println(lastind);
        lnum = mathoperation.substring(str+1, lastind);
        lnum = lnum.replaceAll(" ", "");
        System.out.println(lnum);
        double intlnum = Double.parseDouble(lnum);
        System.out.println(intlnum);
        double tot = intfnum - intlnum; 
        System.out.println(tot);
        total = Double.toString(tot);
        System.out.println(total);
        } else 
    if(mathoperation.contains("+")){
        int str = mathoperation.indexOf("+");
        System.out.println(str);
        fnum = mathoperation.substring(0, str-1);
        fnum = fnum.replaceAll(" ", "");
        System.out.println(fnum);
        double intfnum = Double.parseDouble(fnum);
        System.out.println(intfnum);
        int lastind = mathoperation.length();
        System.out.println(lastind);
        lnum = mathoperation.substring(str+1, lastind);
        lnum = lnum.replaceAll(" ", "");
        System.out.println(lnum);
        double intlnum = Double.parseDouble(lnum);
        System.out.println(intlnum);
        double tot = intfnum + intlnum; 
        System.out.println(tot);
        total = Double.toString(tot);
        System.out.println(total);
        } else 
    if(mathoperation.contains("*")){
    int str = mathoperation.indexOf("*");
    System.out.println(str);
    fnum = mathoperation.substring(0, str-1);
    fnum = fnum.replaceAll(" ", "");
    System.out.println(fnum);
    double intfnum = Double.parseDouble(fnum);
    System.out.println(intfnum);
    int lastind = mathoperation.length();
    System.out.println(lastind);
    lnum = mathoperation.substring(str+1, lastind);
    lnum = lnum.replaceAll(" ", "");
    System.out.println(lnum);
    double intlnum = Double.parseDouble(lnum);
    System.out.println(intlnum);
    double tot = intfnum * intlnum; 
    System.out.println(tot);
    total = Double.toString(tot);
    System.out.println(total);
    } else
    if(mathoperation.contains("divided by")){
    mathoperation = mathoperation.replaceAll("divided by", "/");
    int str = mathoperation.indexOf("/");
    System.out.println(str);
    fnum = mathoperation.substring(0, str-1);
    fnum = fnum.replaceAll(" ", "");
    System.out.println(fnum);
    double intfnum = Double.parseDouble(fnum);
    System.out.println(intfnum);
    int lastind = mathoperation.length();
    System.out.println(lastind);
    lnum = mathoperation.substring(str+1, lastind);
    lnum = lnum.replaceAll(" ", "");
    System.out.println(lnum);
    double intlnum = Double.parseDouble(lnum);
    System.out.println(intlnum);
    double tot = intfnum / intlnum; 
    System.out.println(tot);
    total = Double.toString(tot);
    System.out.println(total);
    } else {
        total = null;
    }
    return total;
}

}
Author: itgeek25, 2012-09-16

3 answers

Un tableau est la mauvaise structure pour représenter l'équation analysée. Vous devez avoir une structure qui peut représenter la priorité de l'opérateur. Le mécanisme typique pour gérer ce type de problème est un arbre de syntaxe abstraite . Pour votre 10 - 10 / 5 + 3 exemple, vous voudriez probablement construire un arbre qui ressemble à ceci:

 <result>
  /   \
 '-' '+'
 / \ / \
10 '/'  3
   / \
  10  5

Avec ce type de structure avec les opérateurs de priorité élevée plus bas dans l'arbre, vous pouvez effectuer une évaluation ascendante pour obtenir le résultat correct.

 9
Author: Michael Barker, 2012-09-16 16:19:45

Tout ce que vous avez à faire est de convertir l'entrée de préfixe au format postfix. Ensuite, vous pouvez facilement faire la magie en utilisant une pile. J'ai utilisé le modèle de commande pour écrire une solution complète.

Méthode pour convertir infix en postfix

    public static List<String> infixToPostfixConvert(String input) {

    int priority = 0;
    String postfixBuffer = "";
    Stack<Character> stack = new Stack<Character>();
    List<String> postfixArray = new ArrayList<String>();

    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {

            if (postfixBuffer.length() > 0) {
                postfixArray.add(postfixBuffer);
            }
            postfixBuffer = "";
            // check the precedence
            if (stack.size() <= 0)
                stack.push(ch);
            else {
                Character chTop = (Character) stack.peek();
                if (chTop == '*' || chTop == '/')
                    priority = 1;
                else
                    priority = 0;
                if (priority == 1) {
                    if (ch == '+' || ch == '-') {
                        postfixArray.add(String.valueOf(stack.pop()));
                        i--;
                    } else { // Same
                        postfixArray.add(String.valueOf(stack.pop()));
                        i--;
                    }
                } else {
                    if (ch == '+' || ch == '-') {
                        postfixArray.add(String.valueOf(stack.pop()));
                        stack.push(ch);
                    } else
                        stack.push(ch);
                }
            }
        } else {
            postfixBuffer += ch;
        }
    }
    postfixArray.add(postfixBuffer);
    int len = stack.size();
    for (int j = 0; j < len; j++)
        postfixArray.add(stack.pop().toString());

    return postfixArray;
}

Ensuite, j'ai une autre méthode qui utilise l'instance de la calculatrice et passe la chaîne postfix.

    public void calculate(Calculator cal, List<String> postFix) {

    Stack<BigDecimal> stack = new Stack<BigDecimal>();

    for ( int i = 0; i < postFix.size(); i++ ) {

        String next = postFix.get(i);

        if (next.equals("+") || next.equals("-") || next.equals("*")
                || next.equals("/")) {
            ArithmaticCalculatorCommand cmd = new ArithmaticCalculatorCommand(
                    next.charAt(0), stack.pop(), stack.pop(), cal);
            Invoker invoker = new Invoker();
            invoker.compute(cmd);
            stack.push(cal.getCurrent());
        } else if ( false ){

        }
        else
        {
            stack.push(new BigDecimal(next.trim()));
        }
    }
}

Mise en œuvre complète

Interface de commande

package org.sanjaya;
public interface Command {

    public void calculate();
}

Implémentation de la commande

package org.sanjaya.impl;

import java.math.BigDecimal;

import org.sanjaya.Command;

public class ArithmaticCalculatorCommand implements Command {

    private char operator;
    private BigDecimal leftOperand;
    private BigDecimal rightOperand;
    private Calculator calculator;

    public ArithmaticCalculatorCommand( char operator, BigDecimal leftOperand, BigDecimal rightOperand, Calculator calculator ) {
        this.leftOperand = leftOperand;
        this.rightOperand = rightOperand;
        this.operator = operator;
        this.calculator = calculator;
    }

    /*
     * This method invoke the three argument operation method that is only used for arithmetic calculations.
     * @param operator
     * @param leftOperand
     * @param rightOperand   * 
     * @see org.sanjaya.Command#calculate()
     */
    public void calculate() {
        calculator.operation( operator, leftOperand, rightOperand );        
    }   
}

Classe Calculatrice( Récepteur)

package org.sanjaya.impl;

import java.math.BigDecimal;

public class Calculator {

    private static Calculator calculator;   
    private BigDecimal current = new BigDecimal( 0 );

    private Calculator()
    {

    }

    public static Calculator getInstance()
    {
        if ( calculator == null )
        {
            calculator = new Calculator();
        }
        return calculator;
    }

    /*
     * This method calculate current value for any number of calculation operations.
     * Currently following operations are supported
     * +,-,*,/
     * 
     * @param operator
     * @param leftOperand
     * @param rightOperand
     *  
     */
    public void operation( char operator, BigDecimal leftOperand, BigDecimal rightOperand )
    {       
        switch ( operator )
        {
        case '+':
            current = leftOperand.add( rightOperand );
            break;
        case '-':
            current = rightOperand.subtract( leftOperand );
            break;
        case '/':
            current = rightOperand.divide(leftOperand);
            break;
        case '*':
            current = leftOperand.multiply( rightOperand );
            break;
        default:
            break;
        }       
    }

    public BigDecimal getCurrent() {
        return current;
    }

    public void setCurrent(BigDecimal current) {
        this.current = current;
    }

}

Classe d'appel

package org.sanjaya.impl;

import java.math.BigDecimal;

import org.sanjaya.Command;
public class Invoker {

    public void compute( Command command )
    {
        command.calculate();
    }   
}

Classe client

package org.sanjaya.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.logging.Logger;
public class CalculatorParser {

    public static Logger logger = Logger.getLogger( "Calculator_Logger" );

    public static void main( String a [] )
    {
        new CalculatorParser().start();
    }

    /*
     * This is the starting point of the program. It receives input from the command line 
     * and process them further and sends to calculate function. At the end this method 
     * displays the calculated result.
     */
    public void start()
    {
        Scanner scanner = new Scanner( System.in );
        logger.info("\n\t Please input expression to calculate::");

        String line = scanner.nextLine();
        List<String> postfixString = CalculatorParser.infixToPostfixConvert( line );
        Calculator calculator = Calculator.getInstance();
        calculator.setCurrent( new BigDecimal( 0 ) );

        calculate( calculator, postfixString );

        logger.info("Result is " + calculator.getCurrent() );
    }

    /*
     * This method keeps a stack to process postfix version of the input and execute the right command implementation.
     * Currently this method supports for arithmetic command calculations only.
     * @param Cal
     * @param postFix
     */
    public void calculate(Calculator cal, List<String> postFix) {

        Stack<BigDecimal> stack = new Stack<BigDecimal>();

        for ( int i = 0; i < postFix.size(); i++ ) {

            String next = postFix.get(i);

            if (next.equals("+") || next.equals("-") || next.equals("*")
                    || next.equals("/")) {
                ArithmaticCalculatorCommand cmd = new ArithmaticCalculatorCommand(
                        next.charAt(0), stack.pop(), stack.pop(), cal);
                Invoker invoker = new Invoker();
                invoker.compute(cmd);
                stack.push(cal.getCurrent());
            } else if ( false ){

            }
            else
            {
                stack.push(new BigDecimal(next.trim()));
            }
        }
    }

    /*
     * This method convert the infix into postfix in order to proceed in the calculation.
     * @param input 
     */
    public static List<String> infixToPostfixConvert(String input) {

        int priority = 0;
        String postfixBuffer = "";
        Stack<Character> stack = new Stack<Character>();
        List<String> postfixArray = new ArrayList<String>();

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {

                if (postfixBuffer.length() > 0) {
                    postfixArray.add(postfixBuffer);
                }
                postfixBuffer = "";
                // check the precedence
                if (stack.size() <= 0)
                    stack.push(ch);
                else {
                    Character chTop = (Character) stack.peek();
                    if (chTop == '*' || chTop == '/')
                        priority = 1;
                    else
                        priority = 0;
                    if (priority == 1) {
                        if (ch == '+' || ch == '-') {
                            postfixArray.add(String.valueOf(stack.pop()));
                            i--;
                        } else { // Same
                            postfixArray.add(String.valueOf(stack.pop()));
                            i--;
                        }
                    } else {
                        if (ch == '+' || ch == '-') {
                            postfixArray.add(String.valueOf(stack.pop()));
                            stack.push(ch);
                        } else
                            stack.push(ch);
                    }
                }
            } else {
                postfixBuffer += ch;
            }
        }
        postfixArray.add(postfixBuffer);
        int len = stack.size();
        for (int j = 0; j < len; j++)
            postfixArray.add(stack.pop().toString());

        return postfixArray;
    }
}
 4
Author: Sanjaya Athukorala, 2013-05-10 11:29:12

Je n'ai pas fait fonctionner le point, c'est pour un autre jour.

Dans le main ou launch laissez-les boutons envoyer leur texte à notre buttonHandler.

buttons.get(i).setOnAction(ev -> buttonHandler(text))

Dans la méthode:

  • si ce texte est un nombre, ajoutez-le simplement.
  • s'il s'agit d'un opérateur, ajoutez-le à la pile.
  • s'il y avait un * ou / sur la pile, effectuez le calcul et ajoutez le nouveau numéro à la pile.
  • À la fin, il n'y a que + et - sur la pile
  • Calculer le résultat.

Le code:

private void buttonHandler(String buttonValue) {
    String newInput = tfInput.getText();

    switch (buttonValue) {
        default:
            // If a previous calculation was made, reset input.
            if (lastOperator.equals("=")) {
                lInput.setText("");
                tfInput.setText(buttonValue);

            // else append the new input.
            } else {
                tfInput.appendText(buttonValue);
            }
            break;
        case " ":
            break;
        case ".":
            if(!input.contains(".")) {
                tfInput.appendText(buttonValue);
            }
            break;
        case "DEL":
            try {
                tfInput.setText("" + newInput.substring(0, tfInput.getText().length() - 1));
            } catch (Exception ex) { }
            break;

        // Operators
        case "x":
        case "/":
        case "+":
        case "-":
        case "=":

            if (newInput.length() > 0) {
                lInput.setText(lInput.getText() + newInput + " " + buttonValue + " ");

                try {
                    // If the previous operator has priority, make a calculation and push to stack
                    if (!input.empty()) {
                        if (input.peek().equals("x")) {
                            input.pop();
                            newInput = new BigDecimal(input.pop()).multiply(new BigDecimal(newInput)).toString();
                        } else if (input.peek().equals("/")) {
                            input.pop();
                            newInput = new BigDecimal(input.pop()).divide(new BigDecimal(newInput), 3, RoundingMode.HALF_UP).stripTrailingZeros().toString();
                            System.out.println(newInput);
                        }
                    }
                } catch (Exception ex) {
                    System.out.println("something is this part went wrong");
                }

                // If the "=" wasn't pressed, add input to the stack and set the textfield empty.
                if (!buttonValue.equals("=")) {
                    // Push to stack and empty input.
                    input.push(newInput);
                    input.push(buttonValue);
                    tfInput.setText("");

                // The button is "=". Prepare for final calculation
                } else {
                    if(!input.empty()) {
                        try {
                            // Reverse items.
                            Stack<String> stack = new Stack<>();
                            stack.push(newInput);
                            while (!input.empty()) {
                                stack.push(input.pop());
                            }

                            // Perform final calculation.
                            while (!stack.empty()) {
                                String firstNumber = stack.pop();
                                if (stack.peek().equals("+")) {
                                    stack.pop();
                                    newInput = new BigDecimal(firstNumber).add(new BigDecimal(stack.pop())).toString();
                                } else if (stack.peek().equals("-")) {
                                    stack.pop();
                                    newInput = new BigDecimal(firstNumber).subtract(new BigDecimal(stack.pop())).toString();
                                }
                                if (!stack.empty()) {
                                    stack.push(newInput);
                                } else {
                                    tfInput.setText(newInput);
                                }
                            }

                        } catch (Exception ex) {
                            System.out.println("something in the second part went wrong");
                        }
                    } else {
                        tfInput.setText(newInput);
                    }
                }
            }
    }
    lastOperator = buttonValue;
}

Si vous voulez calculer une chaîne, mettez quelque chose comme ça dans le mehtod' launch':

 String test = "10-10/5+3=";
    for (int i = 0; i < test.length(); i++) {
        buttonHandler(test.charAt(i) + "");
    }
 0
Author: Halfacht, 2018-03-27 13:18:50