import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ввод чисел и операторов
System.out.print("Введите первое число: ");
double num1 = scanner.nextDouble();
System.out.print("Введите первый оператор (+, -, *, /): ");
char operator1 = scanner.next().charAt(0);
System.out.print("Введите второе число: ");
double num2 = scanner.nextDouble();
System.out.print("Введите второй оператор (+, -, *, /): ");
char operator2 = scanner.next().charAt(0);
System.out.print("Введите третье число: ");
double num3 = scanner.nextDouble();
// Выполнение первой операции
double result1 = calculate(num1, num2, operator1);
// Выполнение второй операции с результатом первой
double finalResult = calculate(result1, num3, operator2);
// Вывод результата
System.out.println("Результат: " + finalResult);
}
public static double calculate(double num1, double num2, char operator) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 != 0) {
return num1 / num2;
} else {
System.out.println("Ошибка: Деление на ноль.");
return 0;
}
default:
System.out.println("Ошибка: Неверный оператор.");
return 0;
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.BiFunction;
public class FunctionalCalculatorEmulator {
// Метод для инициализации операций калькулятора
private static Map> initializeOperations() {
Map> operations = new HashMap<>();
operations.put("+", (a, b) -> a + b);
operations.put("-", (a, b) -> a - b);
operations.put("*", (a, b) -> a * b);
operations.put("/", (a, b) -> b != 0 ? a / b : null); // Обработка деления на ноль
return operations;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double result = 0.0;
boolean continueCalculation = true;
// Инициализация операций
Map> operations = initializeOperations();
System.out.println("Welcome to the Functional Calculator Emulator!");
System.out.println("You can perform operations: +, -, *, /");
System.out.println("Type 'exit' to stop the program.");
while (continueCalculation) {
System.out.println("Current result: " + result);
System.out.print("Enter operation (+, -, *, /) or 'exit' to quit: ");
String operation = scanner.next();
if (operation.equalsIgnoreCase("exit")) {
continueCalculation = false;
System.out.println("Exiting the calculator. Final result: " + result);
break;
}
if (!operations.containsKey(operation)) {
System.out.println("Invalid operation. Please enter +, -, *, or /.");
continue;
}
System.out.print("Enter the number: ");
if (scanner.hasNextDouble()) {
double number = scanner.nextDouble();
BiFunction func = operations.get(operation);
Double newResult = func.apply(result, number);
if (newResult == null) {
System.out.println("Error: Division by zero is not allowed.");
} else {
result = newResult;
}
} else {
System.out.println("Invalid input. Please enter a valid number.");
scanner.next(); // clear the invalid input
}
}
scanner.close();
}
}