Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Vector;
- class Fraction{
- private Integer numerator=1;
- private Integer denominator =1;
- public Fraction(){
- }
- public Fraction(Integer num,Integer denom) throws Exception{
- this.numerator=num;
- this.denominator=denom;
- if(denom==0) throw new Exception("Denominator = 0");
- }
- public static Fraction sum(Fraction left,Fraction right){
- Integer resultNumerator =left.numerator*right.denominator+right.numerator*left.denominator;
- Integer resultDenominator = left.denominator*right.denominator;
- try {
- return new Fraction(resultNumerator,resultDenominator);
- } catch (Exception e) {
- return null;
- }
- }
- public void integer(){
- if (this.numerator % this.denominator == 0){
- this.numerator/=this.denominator;
- this.denominator=1;
- }
- }
- public void Multiply(Fraction fraction){
- Integer resultNumerator=this.numerator*fraction.numerator;
- Integer resultDenominator=this.denominator*fraction.denominator;
- this.numerator=resultNumerator;
- this.denominator=resultDenominator;
- this.socrat();
- }
- public void Division(Fraction fraction){
- Integer resultNumerator=this.numerator*fraction.denominator;
- Integer resultDenominator=this.denominator*fraction.numerator;
- this.numerator=resultNumerator;
- this.denominator=resultDenominator;
- this.socrat();
- }
- public void socrat (){
- int c=1;
- int a=this.numerator;
- int b=this.denominator;
- if (b>a){
- c=b;
- b=a;
- a=c;
- }
- while(a!=0&&b!=0){
- c=a-b;
- a=b;
- b=c;
- if (b>a) {
- c = b;
- b = a;
- a = c;
- }
- }
- if (a>b){
- this.numerator/=a;
- this.denominator/=a;
- }else{
- this.numerator/=b;
- this.denominator/=b;
- }
- }
- public void sumTo(Fraction fraction){
- Integer resultNumerator=this.numerator*fraction.denominator+fraction.numerator*this.denominator;
- Integer resultDenominator = this.denominator*fraction.denominator;
- this.numerator=resultNumerator;
- this.denominator=resultDenominator;
- }
- public void pow(double Power){
- Integer num=this.numerator;
- Integer denom=this.denominator;
- for (int i = 1; i < Power; i++) {
- this.Multiply(this);
- }
- }
- public void swap(Fraction fraction){
- Integer pern=this.numerator;
- Integer perd=this.denominator;
- this.numerator=fraction.numerator;
- this.denominator=fraction.denominator;
- fraction.denominator=perd;
- fraction.numerator=pern;
- }
- @Override
- public String toString() {
- try {
- Fraction d2 = new Fraction(numerator,denominator);
- if (denominator==1){
- return String.valueOf(numerator);
- }else{
- return numerator+" / "+denominator;
- }
- } catch (Exception e) {
- return e.getMessage();
- }
- }
- }
- public class Main {
- public static Scanner in = new Scanner(System.in);
- public static void main(String[] args) {
- System.out.println("Рус/Eng");
- String l=in.next();
- Fraction d1 = null;
- Fraction d2=null;
- Integer denom=0;
- Integer num;
- System.out.println(l.equals("Eng")?"Please,input numerator of the first fraction":"Пожалуйста,введите числитель первой дроби");
- num = in.nextInt();
- System.out.println(l.equals("Eng")?"Please,input denominator of the first fraction":"Пожалуйста,введите знаменатель первой дроби");
- denom = in.nextInt();
- try {
- d1=new Fraction(num,denom);
- d1.socrat();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- while (denom==0) {
- try {
- d1=new Fraction(num,denom);
- d1.socrat();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- System.out.println(l.equals("Eng")?"Please,input correct denominator of the first fraction":"Пожалуйста,введите корректный знаменатель первой дроби");
- denom = in.nextInt();
- }
- System.out.println(l.equals("Eng")?"Please,input numerator of the second fraction":"Пожалуйста,введите числитель второй дроби");
- num=in.nextInt();
- System.out.println(l.equals("Eng")?"Please,input denominator of the second fraction":"Пожалуйста,введите знаменатель второй дроби");
- denom=in.nextInt();
- try {
- d2=new Fraction(num,denom);
- d2.socrat();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- while (denom==0) {
- try {
- d2=new Fraction(num,denom);
- d2.socrat();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- System.out.println(l.equals("Eng")?"Please,input correct denominator of the second fraction":"Пожалуйста,введите корректный знаменатель второй дроби");
- denom = in.nextInt();
- }
- System.out.println(l.equals("Eng")?"Choose the function you want to complete":"Выберете функцию");
- boolean flag=true;
- while(flag){
- String s=in.nextLine();
- switch(s){
- case ("Swap"):{
- d1.swap(d2);
- System.out.println(d1+" "+d2);
- flag=false;
- break;
- }
- case ("Поменять местами"):{
- d1.swap(d2);
- System.out.println(d1+" "+d2);
- flag=false;
- break;
- }
- case("Сумма"):{
- d1.sumTo(d2);
- d1.socrat();
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Sum"): {
- d1.sumTo(d2);
- d1.socrat();
- System.out.println(d1);
- flag = false;
- break;
- }
- case ("Деление"):{
- d1.Division(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Division"):{
- d1.Division(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Pow 1"):{
- System.out.print("Power - ");
- Integer power=in.nextInt();
- d1.pow(power);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Степень 1 дробь"):{
- System.out.print("Степень - ");
- Integer power=in.nextInt();
- d1.pow(power);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Pow 2"):{
- System.out.print("Power - ");
- Integer power=in.nextInt();
- d2.pow(power);
- System.out.println(d2);
- flag=false;
- break;
- }
- case ("Степень 2 дробь"):{
- System.out.print("Степень - ");
- Integer power=in.nextInt();
- d2.pow(power);
- System.out.println(d2);
- flag=false;
- break;
- }
- case("Multiply"):{
- d1.Multiply(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Умножение"):{
- d1.Multiply(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- default: {
- System.out.println(l.equals("Eng")?"Functions:Sum,Multiply,Division,Pow 1,Pow 2,Swap":"Функции:Сумма,Умножение,Деление,Степень 1 дробь,Степень 2 дробь,Поменять местами");
- break;
- }
- }
- }
- boolean cont=true;
- while(cont){
- System.out.println(l.equals("Eng")?"Do you want to continue?('1'-Yes or '2'-No)":"Хотите ли продолжить?('1'- Да или '2'- Нет)");
- Integer danet=in.nextInt();
- if (danet==2)
- break;
- flag=true;
- System.out.println(l.equals("Eng")?"Choose the function you want to complete":"Выберете функцию");
- while(flag){
- String s=in.nextLine();
- switch(s){
- case("Сумма"):
- case ("Sum"):{
- d1.sumTo(d2);
- d1.socrat();
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Поменять местами"):{
- d1.swap(d2);
- System.out.println(d1+" "+d2);
- flag=false;
- break;
- }
- case ("Swap"):{
- d1.swap(d2);
- System.out.println(d1+" "+d2);
- flag=false;
- break;
- }
- case ("Деление"):{
- d1.Division(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Division"):{
- d1.Division(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Pow 1"):{
- System.out.print("Power - ");
- Integer power=in.nextInt();
- d1.pow(power);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Степень 1 дробь"):{
- System.out.print("Степень - ");
- Integer power=in.nextInt();
- d1.pow(power);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Pow 2"):{
- System.out.print("Power - ");
- Integer power=in.nextInt();
- d2.pow(power);
- System.out.println(d2);
- flag=false;
- break;
- }
- case ("Степень 2 дробь"):{
- System.out.print("Степень - ");
- Integer power=in.nextInt();
- d2.pow(power);
- System.out.println(d2);
- flag=false;
- break;
- }
- case("Multiply"):{
- d1.Multiply(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Умножение"):{
- d1.Multiply(d2);
- System.out.println(d1);
- flag=false;
- break;
- }
- case ("Создать 1"):{
- System.out.println(l.equals("Eng")?"Please,input numerator of the first fraction":"Пожалуйста,введите числитель первой дроби");
- num = in.nextInt();
- System.out.println(l.equals("Eng")?"Please,input denominator of the first fraction":"Пожалуйста,введите знаменатель первой дроби");
- denom = in.nextInt();
- try {
- d1=new Fraction(num,denom);
- d1.socrat();
- System.out.println(d1);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- while (denom==0) {
- try {
- d1=new Fraction(num,denom);
- d1.socrat();
- System.out.println(d1);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- System.out.println(l.equals("Eng")?"Please,input correct denominator of the first fraction":"Пожалуйста,введите корректный знаменатель первой дроби");
- denom = in.nextInt();
- }
- flag=false;
- break;
- }
- case ("Create1"):{
- System.out.println(l.equals("Eng")?"Please,input numerator of the first fraction":"Пожалуйста,введите числитель первой дроби");
- num = in.nextInt();
- System.out.println(l.equals("Eng")?"Please,input denominator of the first fraction":"Пожалуйста,введите знаменатель первой дроби");
- denom = in.nextInt();
- try {
- d1=new Fraction(num,denom);
- d1.socrat();
- System.out.println(d1);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- while (denom==0) {
- try {
- d1=new Fraction(num,denom);
- d1.socrat();
- System.out.println(d1);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- System.out.println(l.equals("Eng")?"Please,input correct denominator of the first fraction":"Пожалуйста,введите корректный знаменатель первой дроби");
- denom = in.nextInt();
- }
- flag=false;
- break;
- }
- case ("Создать 2"):{
- System.out.println(l.equals("Eng")?"Please,input numerator of the second fraction":"Пожалуйста,введите числитель второй дроби");
- num=in.nextInt();
- System.out.println(l.equals("Eng")?"Please,input denominator of the second fraction":"Пожалуйста,введите знаменатель второй дроби");
- denom=in.nextInt();
- try {
- d2=new Fraction(num,denom);
- d2.socrat();
- System.out.println(d2);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- while (denom==0) {
- try {
- d2=new Fraction(num,denom);
- d2.socrat();
- System.out.println(d2);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- System.out.println(l.equals("Eng")?"Please,input correct denominator of the second fraction":"Пожалуйста,введите корректный знаменатель второй дроби");
- denom = in.nextInt();
- }
- flag=false;
- break;
- }
- case ("Create2"):{
- System.out.println(l.equals("Eng")?"Please,input numerator of the second fraction":"Пожалуйста,введите числитель второй дроби");
- num=in.nextInt();
- System.out.println(l.equals("Eng")?"Please,input denominator of the second fraction":"Пожалуйста,введите знаменатель второй дроби");
- denom=in.nextInt();
- try {
- d2=new Fraction(num,denom);
- d2.socrat();
- System.out.println(d2);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- while (denom==0) {
- try {
- d2=new Fraction(num,denom);
- d2.socrat();
- System.out.println(d2);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- System.out.println(l.equals("Eng")?"Please,input correct denominator of the second fraction":"Пожалуйста,введите корректный знаменатель второй дроби");
- denom = in.nextInt();
- }
- flag=false;
- break;
- }
- default :{
- System.out.println(l.equals("Eng")?"Functions:Sum,Create1,Create2,Multiply,Division,Pow 1,Pow 2,Swap":"Функции:Поменять местами,Сумма,Создать 1,Создать 2,Умножение,Деление,Степень 1 дробь,Степень 2 дробь");
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement