Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

dmitryCalcTest1

import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {

        calcVer1("5+14");
    }

    private static void calcVer1(String input) throws Exception {
 

        Float number1 = 0F;
        String action = null;
        Float number2 = 0F;

        int dexaPart1 = 0;
        int dexaPart2 = 0;
        boolean isDotExsist = false;

        for (char symbol : input.toCharArray()) {

            if (symbol == '.') {
                isDotExsist = true;
            } else if (symbol == '/'
                    || symbol == '+'
                    || symbol == '-'
                    || symbol == '*') {
                action = String.valueOf(symbol);
                isDotExsist = false;
            } else if (Character.isDigit(symbol)) {
                Float digit = Float.valueOf(String.valueOf(symbol));
                if (action == null) {
                    if (isDotExsist) {
                        dexaPart1 = (int) (dexaPart1 * 10 + digit);

                    } else {
                        number1 = number1 * 10 + digit;
                    }
                } else {
                    if (isDotExsist) {
                        dexaPart2 = (int) (dexaPart2 * 10 + digit);

                    } else {
                        number2 = number2 * 10 + digit;
                    }
                }
            } else {
                throw new Exception("Не получилось");
            }
        }

        number1 = number1 + (float) (dexaPart1 / (Math.pow(10, String.valueOf(dexaPart1).length())));
        number2 = number2 + (float) (dexaPart2 / (Math.pow(10, String.valueOf(dexaPart2).length())));


        float result;
        switch (action) {
            case "+":
                result = number1 + number2;
                break;
            case "-":
                result = number1 - number2;
                break;
            case "*":
                result = number1 * number2;
                break;
            case "/":
                result = number1 / number2;
                break;
            default:
                System.out.println("Я пока не умею такую команду: " + action);
                return;
        }


        System.out.println(number1 + action + number2 + "=" + result);
    }


    private static void calcVer2() throws Exception {
        Scanner consoleReader = new Scanner(System.in);
        String input = consoleReader.next();

        Float number1 = 0F;
        String action = null;
        Float number2 = 0F;

        String buffer = "";
        for (char symbol : input.toCharArray()) {
            if (Character.isDigit(symbol) || symbol == '.') {
                buffer = buffer + symbol;
            } else if (symbol == '/'
                    || symbol == '+'
                    || symbol == '-'
                    || symbol == '*') {
                action = String.valueOf(symbol);
                number1 = Float.valueOf(buffer);
                buffer = "";
            } else {
                throw new Exception("Не получилось");
            }
        }

        number2 = Float.valueOf(buffer);

        float result;
        switch (action) {
            case "+":
                result = number1 + number2;
                break;
            case "-":
                result = number1 - number2;
                break;
            case "*":
                result = number1 * number2;
                break;
            case "/":
                result = number1 / number2;
                break;
            default:
                System.out.println("Я пока не умею такую команду: " + action);
                return;
        }


        System.out.println(number1 + action + number2 + "=" + result);
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.