Skip to content

Instantly share code, notes, and snippets.

@olegrewko
Created April 27, 2020 16:30
Show Gist options
  • Save olegrewko/e511dd6ff3847776d8fd0238484e1d70 to your computer and use it in GitHub Desktop.
Save olegrewko/e511dd6ff3847776d8fd0238484e1d70 to your computer and use it in GitHub Desktop.
Римско-Арабский калькулятор
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="13.0.2" project-jdk-type="JavaSDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/calcRA.iml" filepath="$PROJECT_DIR$/.idea/calcRA.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="030e8db0-ddc4-4b57-862f-f29393244e7e" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ProjectId" id="1b872PcpkrrsbwQSitQPf7I7RK3" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showExcludedFiles" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
</component>
<component name="RunManager">
<configuration default="true" type="Application" factoryName="Application">
<option name="ALTERNATIVE_JRE_PATH" value="13.0.2" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
<component name="ServiceViewManager">
<option name="viewStates">
<list>
<serviceView>
<treeState>
<expand />
<select />
</treeState>
</serviceView>
</list>
</option>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="030e8db0-ddc4-4b57-862f-f29393244e7e" name="Default Changelist" comment="" />
<created>1588001659802</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1588001659802</updated>
<workItem from="1588001664891" duration="218000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="WindowStateProjectService">
<state x="414" y="174" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1588001916988">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="414" y="174" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1588001916988" />
<state x="808" y="408" key="#com.intellij.ide.util.projectWizard.JdkChooserPanel.MyDialog" timestamp="1588001692755">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="808" y="408" key="#com.intellij.ide.util.projectWizard.JdkChooserPanel.MyDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1588001692755" />
<state x="655" y="343" key="com.intellij.ide.util.TipDialog" timestamp="1588001668905">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="655" y="343" key="com.intellij.ide.util.TipDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1588001668905" />
</component>
</project>
package Test.CalcRA;
/**
* Написать Римско-Арабский консольный калькулятор чтобы он сам распознавал нужные символы и выдавал ответ
* Вот это написал уже работает
* Помогите плиз отладить чтоб идеально работал
*/
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class RomanArabianCalc {
static Scanner scanner = new Scanner(System.in);
static int number1, number2, number10, number20;
static char operation;
static int result, result2;
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[] block01 = under_charString.split("[+-/*=]");
System.out.println("---Получили две строки в массиве---");
System.out.println(Arrays.toString(block01));
System.out.println(block01[0]);
System.out.println(block01[1]);
number1 = romanToNumber(block01[0]);
number2 = romanToNumber(block01[1]);
result = calculated(number1, number2, operation);
System.out.println("---Результат для римских цифр-----");
System.out.println(number1 + " " + operation + " " + number2 + " = " + result);
number10 = Integer.parseInt(block01[0]);
number20 = Integer.parseInt(block01[1]);
result2 = calculated(number10, number20, operation);
System.out.println("--Результат для арабских цифр------");
System.out.println(number10 + " " + operation + " " + number20 + " = " + result2);
}
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;
default:
throw new IllegalArgumentException("Не верный знак операции");
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment