Skip to content

Instantly share code, notes, and snippets.

@olegrewko
Created April 29, 2020 11:18
Show Gist options
  • Save olegrewko/39e2d506174bcb1f4f0e417825db6412 to your computer and use it in GitHub Desktop.
Save olegrewko/39e2d506174bcb1f4f0e417825db6412 to your computer and use it in GitHub Desktop.
RAconsoleCAend
package calcRA;
import java.util.InputMismatchException;
import java.util.Scanner;
public class RomanArabianCalc {
static Scanner scanner = new Scanner(System.in);
static int number1, number2;
static char operation;
static int result;
static String[] roman = {"X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I"};
public static void main (String[] args) {
System.out.println("Введите выражение [2+2=] или [V+V=] + Enter ");
// Считываем строку userInput которую ввёл пользователь
String userInput = scanner.nextLine();
// Создаём пустой символьный массив длиной 10 символов: under_char
char[] under_char = new char[10];
// Заполняем символьный массив символами строки которую ввел пользователь и по ходу ловим знак операции
for (int i = 0; i < userInput.length(); i++) {
under_char[i] = userInput.charAt(i);
if (under_char[i] == '+') {
operation = '+';
}
if (under_char[i] == '-') {
operation = '-';
}
if (under_char[i] == '*') {
operation = '*';
}
if (under_char[i] == '/') {
operation = '/';
}
}
// Конвертируем символьный массив в строковый массив чтобы методом split разделить на две подстроки
String under_charString = String.valueOf(under_char);
String[] blacks;
blacks = under_charString.split("[+-/*=]");
System.out.println("---Получили две строки из массиве---");
System.out.println(blacks[0]);
System.out.println(blacks[1]);
number1 = romanToNumber(blacks[0]);
number2 = romanToNumber(blacks[1]);
if (number1 < 0 && number2 < 0) {
result = 0;
} else {
result = calculated(number1, number2, operation);
System.out.println("---Результат для римских цифр----");
System.out.println(number1 + " " + operation + " " + number2 + " = " + result);
}
number1 = Integer.parseInt(blacks[0]);
number2 = Integer.parseInt(blacks[1]);
result = calculated(number1, number2, operation);
System.out.println("--Результат для арабских цифр----");
System.out.println(number1 + " " + operation + " " + number2 + " = " + result);
}
private static int romanToNumber (String roman) {
try {
if (roman.equals("I")) {
return 1;
} else if (roman.equals("II")) {
return 2;
} else if (roman.equals("III")) {
return 3;
} else if (roman.equals("IV")) {
return 4;
} else if (roman.equals("V")) {
return 5;
} else if (roman.equals("VI")) {
return 6;
} else if (roman.equals("VII")) {
return 7;
} else if (roman.equals("VIII")) {
return 8;
} else if (roman.equals("IX")) {
return 9;
} else if (roman.equals("X")) {
return 10;
}
} catch (InputMismatchException e) {
throw new InputMismatchException("Неверный формат данных");
}
return -1;
}
public static int calculated (int num1, int num2, char op) {
int result = 0;
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
try {
result = num1 / num2;
} catch (ArithmeticException | InputMismatchException e) {
System.out.println("Exception : " + e);
System.out.println("Only integer non-zero parameters allowed");
break;
}
break;
default:
throw new IllegalArgumentException("Не верный знак операции");
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment