Advertisement
Sunni

Fractions

Nov 10th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 19.52 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. import java.util.Vector;
  4.  
  5. class Fraction{
  6.     private Integer numerator=1;
  7.     private Integer denominator =1;
  8.     public Fraction(){
  9.  
  10.     }
  11.     public Fraction(Integer num,Integer denom) throws Exception{
  12.         this.numerator=num;
  13.         this.denominator=denom;
  14.         if(denom==0) throw new Exception("Denominator = 0");
  15.     }
  16.     public  static Fraction sum(Fraction left,Fraction right){
  17.         Integer resultNumerator =left.numerator*right.denominator+right.numerator*left.denominator;
  18.         Integer resultDenominator = left.denominator*right.denominator;
  19.         try {
  20.             return new Fraction(resultNumerator,resultDenominator);
  21.         } catch (Exception e) {
  22.             return null;
  23.         }
  24.     }
  25.     public void integer(){
  26.         if (this.numerator % this.denominator == 0){
  27.             this.numerator/=this.denominator;
  28.             this.denominator=1;
  29.         }
  30.     }
  31.     public void Multiply(Fraction fraction){
  32.         Integer resultNumerator=this.numerator*fraction.numerator;
  33.         Integer resultDenominator=this.denominator*fraction.denominator;
  34.         this.numerator=resultNumerator;
  35.         this.denominator=resultDenominator;
  36.         this.socrat();
  37.     }
  38.     public void Division(Fraction fraction){
  39.         Integer resultNumerator=this.numerator*fraction.denominator;
  40.         Integer resultDenominator=this.denominator*fraction.numerator;
  41.         this.numerator=resultNumerator;
  42.         this.denominator=resultDenominator;
  43.         this.socrat();
  44.     }
  45.     public void socrat (){
  46.         int c=1;
  47.         int a=this.numerator;
  48.         int b=this.denominator;
  49.         if (b>a){
  50.             c=b;
  51.             b=a;
  52.             a=c;
  53.         }
  54.         while(a!=0&&b!=0){
  55.             c=a-b;
  56.             a=b;
  57.             b=c;
  58.             if (b>a) {
  59.                 c = b;
  60.                 b = a;
  61.                 a = c;
  62.             }
  63.         }
  64.         if (a>b){
  65.             this.numerator/=a;
  66.             this.denominator/=a;
  67.         }else{
  68.             this.numerator/=b;
  69.             this.denominator/=b;
  70.         }
  71.     }
  72.     public  void sumTo(Fraction fraction){
  73.         Integer resultNumerator=this.numerator*fraction.denominator+fraction.numerator*this.denominator;
  74.         Integer resultDenominator = this.denominator*fraction.denominator;
  75.         this.numerator=resultNumerator;
  76.         this.denominator=resultDenominator;
  77.     }
  78.     public void pow(double Power){
  79.         Integer num=this.numerator;
  80.         Integer denom=this.denominator;
  81.         for (int i = 1; i < Power; i++) {
  82.             this.Multiply(this);
  83.         }
  84.     }
  85.     public void swap(Fraction fraction){
  86.         Integer pern=this.numerator;
  87.         Integer perd=this.denominator;
  88.         this.numerator=fraction.numerator;
  89.         this.denominator=fraction.denominator;
  90.         fraction.denominator=perd;
  91.         fraction.numerator=pern;
  92.  
  93.     }
  94.     @Override
  95.     public String toString() {
  96.         try {
  97.             Fraction d2 = new Fraction(numerator,denominator);
  98.             if (denominator==1){
  99.                 return String.valueOf(numerator);
  100.             }else{
  101.                 return numerator+" / "+denominator;
  102.             }
  103.         } catch (Exception e) {
  104.             return e.getMessage();
  105.         }
  106.     }
  107. }
  108. public class Main {
  109.     public static Scanner in = new Scanner(System.in);
  110.     public static void main(String[] args) {
  111.         System.out.println("Рус/Eng");
  112.         String l=in.next();
  113.         Fraction d1 = null;
  114.         Fraction d2=null;
  115.         Integer denom=0;
  116.         Integer num;
  117.         System.out.println(l.equals("Eng")?"Please,input numerator of the first fraction":"Пожалуйста,введите числитель первой дроби");
  118.         num = in.nextInt();
  119.         System.out.println(l.equals("Eng")?"Please,input denominator of the first fraction":"Пожалуйста,введите знаменатель первой дроби");
  120.         denom = in.nextInt();
  121.         try {
  122.             d1=new Fraction(num,denom);
  123.             d1.socrat();
  124.         } catch (Exception e) {
  125.             System.out.println(e.getMessage());
  126.         }
  127.         while (denom==0) {
  128.             try {
  129.                 d1=new Fraction(num,denom);
  130.                 d1.socrat();
  131.             } catch (Exception e) {
  132.                 System.out.println(e.getMessage());
  133.             }
  134.             System.out.println(l.equals("Eng")?"Please,input correct denominator of the first fraction":"Пожалуйста,введите корректный знаменатель первой дроби");
  135.             denom = in.nextInt();
  136.         }
  137.         System.out.println(l.equals("Eng")?"Please,input numerator of the second fraction":"Пожалуйста,введите числитель второй дроби");
  138.         num=in.nextInt();
  139.         System.out.println(l.equals("Eng")?"Please,input denominator of the second fraction":"Пожалуйста,введите знаменатель второй дроби");
  140.         denom=in.nextInt();
  141.         try {
  142.             d2=new Fraction(num,denom);
  143.             d2.socrat();
  144.         } catch (Exception e) {
  145.             System.out.println(e.getMessage());
  146.         }
  147.         while (denom==0) {
  148.             try {
  149.                 d2=new Fraction(num,denom);
  150.                 d2.socrat();
  151.             } catch (Exception e) {
  152.                 System.out.println(e.getMessage());
  153.             }
  154.             System.out.println(l.equals("Eng")?"Please,input correct denominator of the second fraction":"Пожалуйста,введите корректный знаменатель второй дроби");
  155.             denom = in.nextInt();
  156.         }
  157.         System.out.println(l.equals("Eng")?"Choose the function you want to complete":"Выберете функцию");
  158.         boolean flag=true;
  159.         while(flag){
  160.             String s=in.nextLine();
  161.             switch(s){
  162.                 case ("Swap"):{
  163.                     d1.swap(d2);
  164.                     System.out.println(d1+" "+d2);
  165.                     flag=false;
  166.                     break;
  167.                 }
  168.                 case ("Поменять местами"):{
  169.                     d1.swap(d2);
  170.                     System.out.println(d1+" "+d2);
  171.                     flag=false;
  172.                     break;
  173.                 }
  174.                 case("Сумма"):{
  175.                     d1.sumTo(d2);
  176.                     d1.socrat();
  177.                     System.out.println(d1);
  178.                     flag=false;
  179.                     break;
  180.                 }
  181.                 case ("Sum"): {
  182.                     d1.sumTo(d2);
  183.                     d1.socrat();
  184.                     System.out.println(d1);
  185.                     flag = false;
  186.                     break;
  187.                 }
  188.                 case ("Деление"):{
  189.                     d1.Division(d2);
  190.                     System.out.println(d1);
  191.                     flag=false;
  192.                     break;
  193.                 }
  194.                 case ("Division"):{
  195.                     d1.Division(d2);
  196.                     System.out.println(d1);
  197.                     flag=false;
  198.                     break;
  199.                 }
  200.                 case ("Pow 1"):{
  201.                     System.out.print("Power - ");
  202.                     Integer power=in.nextInt();
  203.                     d1.pow(power);
  204.                     System.out.println(d1);
  205.                     flag=false;
  206.                     break;
  207.                 }
  208.                 case ("Степень 1 дробь"):{
  209.                     System.out.print("Степень - ");
  210.                     Integer power=in.nextInt();
  211.                     d1.pow(power);
  212.                     System.out.println(d1);
  213.                     flag=false;
  214.                     break;
  215.                 }
  216.                 case ("Pow 2"):{
  217.                     System.out.print("Power - ");
  218.                     Integer power=in.nextInt();
  219.                     d2.pow(power);
  220.                     System.out.println(d2);
  221.                     flag=false;
  222.                     break;
  223.                 }
  224.                 case ("Степень 2 дробь"):{
  225.                     System.out.print("Степень - ");
  226.                     Integer power=in.nextInt();
  227.                     d2.pow(power);
  228.                     System.out.println(d2);
  229.                     flag=false;
  230.                     break;
  231.                 }
  232.                 case("Multiply"):{
  233.                     d1.Multiply(d2);
  234.                     System.out.println(d1);
  235.                     flag=false;
  236.                     break;
  237.                 }
  238.                 case ("Умножение"):{
  239.                     d1.Multiply(d2);
  240.                     System.out.println(d1);
  241.                     flag=false;
  242.                     break;
  243.                 }
  244.                 default: {
  245.                     System.out.println(l.equals("Eng")?"Functions:Sum,Multiply,Division,Pow 1,Pow 2,Swap":"Функции:Сумма,Умножение,Деление,Степень 1 дробь,Степень 2 дробь,Поменять местами");
  246.                     break;
  247.                 }
  248.             }
  249.         }
  250.         boolean cont=true;
  251.         while(cont){
  252.             System.out.println(l.equals("Eng")?"Do you want to continue?('1'-Yes or '2'-No)":"Хотите ли продолжить?('1'- Да или '2'- Нет)");
  253.             Integer danet=in.nextInt();
  254.             if (danet==2)
  255.                 break;
  256.             flag=true;
  257.             System.out.println(l.equals("Eng")?"Choose the function you want to complete":"Выберете функцию");
  258.             while(flag){
  259.                 String s=in.nextLine();
  260.                 switch(s){
  261.                     case("Сумма"):
  262.                     case ("Sum"):{
  263.                         d1.sumTo(d2);
  264.                         d1.socrat();
  265.                         System.out.println(d1);
  266.                         flag=false;
  267.                         break;
  268.                     }
  269.                     case ("Поменять местами"):{
  270.                         d1.swap(d2);
  271.                         System.out.println(d1+" "+d2);
  272.                         flag=false;
  273.                         break;
  274.                     }
  275.                     case ("Swap"):{
  276.                         d1.swap(d2);
  277.                         System.out.println(d1+" "+d2);
  278.                         flag=false;
  279.                         break;
  280.                     }
  281.                     case ("Деление"):{
  282.                         d1.Division(d2);
  283.                         System.out.println(d1);
  284.                         flag=false;
  285.                         break;
  286.                     }
  287.                     case ("Division"):{
  288.                         d1.Division(d2);
  289.                         System.out.println(d1);
  290.                         flag=false;
  291.                         break;
  292.                     }
  293.                     case ("Pow 1"):{
  294.                         System.out.print("Power - ");
  295.                         Integer power=in.nextInt();
  296.                         d1.pow(power);
  297.                         System.out.println(d1);
  298.                         flag=false;
  299.                         break;
  300.                     }
  301.                     case ("Степень 1 дробь"):{
  302.                         System.out.print("Степень - ");
  303.                         Integer power=in.nextInt();
  304.                         d1.pow(power);
  305.                         System.out.println(d1);
  306.                         flag=false;
  307.                         break;
  308.                     }
  309.                     case ("Pow 2"):{
  310.                         System.out.print("Power - ");
  311.                         Integer power=in.nextInt();
  312.                         d2.pow(power);
  313.                         System.out.println(d2);
  314.                         flag=false;
  315.                         break;
  316.                     }
  317.                     case ("Степень 2 дробь"):{
  318.                         System.out.print("Степень - ");
  319.                         Integer power=in.nextInt();
  320.                         d2.pow(power);
  321.                         System.out.println(d2);
  322.                         flag=false;
  323.                         break;
  324.                     }
  325.                     case("Multiply"):{
  326.                         d1.Multiply(d2);
  327.                         System.out.println(d1);
  328.                         flag=false;
  329.                         break;
  330.                     }
  331.                     case ("Умножение"):{
  332.                         d1.Multiply(d2);
  333.                         System.out.println(d1);
  334.                         flag=false;
  335.                         break;
  336.                     }
  337.                     case ("Создать 1"):{
  338.                         System.out.println(l.equals("Eng")?"Please,input numerator of the first fraction":"Пожалуйста,введите числитель первой дроби");
  339.                         num = in.nextInt();
  340.                         System.out.println(l.equals("Eng")?"Please,input denominator of the first fraction":"Пожалуйста,введите знаменатель первой дроби");
  341.                         denom = in.nextInt();
  342.                         try {
  343.                             d1=new Fraction(num,denom);
  344.                             d1.socrat();
  345.                             System.out.println(d1);
  346.                         } catch (Exception e) {
  347.                             System.out.println(e.getMessage());
  348.                         }
  349.                         while (denom==0) {
  350.                             try {
  351.                                 d1=new Fraction(num,denom);
  352.                                 d1.socrat();
  353.                                 System.out.println(d1);
  354.                             } catch (Exception e) {
  355.                                 System.out.println(e.getMessage());
  356.                             }
  357.                             System.out.println(l.equals("Eng")?"Please,input correct denominator of the first fraction":"Пожалуйста,введите корректный знаменатель первой дроби");
  358.                             denom = in.nextInt();
  359.                         }
  360.                         flag=false;
  361.                         break;
  362.                     }
  363.                     case ("Create1"):{
  364.                         System.out.println(l.equals("Eng")?"Please,input numerator of the first fraction":"Пожалуйста,введите числитель первой дроби");
  365.                         num = in.nextInt();
  366.                         System.out.println(l.equals("Eng")?"Please,input denominator of the first fraction":"Пожалуйста,введите знаменатель первой дроби");
  367.                         denom = in.nextInt();
  368.                         try {
  369.                             d1=new Fraction(num,denom);
  370.                             d1.socrat();
  371.                             System.out.println(d1);
  372.                         } catch (Exception e) {
  373.                             System.out.println(e.getMessage());
  374.                         }
  375.                         while (denom==0) {
  376.                             try {
  377.                                 d1=new Fraction(num,denom);
  378.                                 d1.socrat();
  379.                                 System.out.println(d1);
  380.                             } catch (Exception e) {
  381.                                 System.out.println(e.getMessage());
  382.                             }
  383.                             System.out.println(l.equals("Eng")?"Please,input correct denominator of the first fraction":"Пожалуйста,введите корректный знаменатель первой дроби");
  384.                             denom = in.nextInt();
  385.                         }
  386.                         flag=false;
  387.                         break;
  388.                     }
  389.                     case ("Создать 2"):{
  390.                         System.out.println(l.equals("Eng")?"Please,input numerator of the second fraction":"Пожалуйста,введите числитель второй дроби");
  391.                         num=in.nextInt();
  392.                         System.out.println(l.equals("Eng")?"Please,input denominator of the second fraction":"Пожалуйста,введите знаменатель второй дроби");
  393.                         denom=in.nextInt();
  394.                         try {
  395.                             d2=new Fraction(num,denom);
  396.                             d2.socrat();
  397.                             System.out.println(d2);
  398.                         } catch (Exception e) {
  399.                             System.out.println(e.getMessage());
  400.                         }
  401.                         while (denom==0) {
  402.                             try {
  403.                                 d2=new Fraction(num,denom);
  404.                                 d2.socrat();
  405.                                 System.out.println(d2);
  406.                             } catch (Exception e) {
  407.                                 System.out.println(e.getMessage());
  408.                             }
  409.                               System.out.println(l.equals("Eng")?"Please,input correct denominator of the second fraction":"Пожалуйста,введите корректный знаменатель второй дроби");
  410.                               denom = in.nextInt();
  411.                             }
  412.                           flag=false;
  413.                           break;
  414.                         }
  415.                         case ("Create2"):{
  416.                         System.out.println(l.equals("Eng")?"Please,input numerator of the second fraction":"Пожалуйста,введите числитель второй дроби");
  417.                         num=in.nextInt();
  418.                         System.out.println(l.equals("Eng")?"Please,input denominator of the second fraction":"Пожалуйста,введите знаменатель второй дроби");
  419.                         denom=in.nextInt();
  420.                         try {
  421.                             d2=new Fraction(num,denom);
  422.                             d2.socrat();
  423.                             System.out.println(d2);
  424.                         } catch (Exception e) {
  425.                             System.out.println(e.getMessage());
  426.                         }
  427.                         while (denom==0) {
  428.                             try {
  429.                                 d2=new Fraction(num,denom);
  430.                                 d2.socrat();
  431.                                 System.out.println(d2);
  432.                             } catch (Exception e) {
  433.                                 System.out.println(e.getMessage());
  434.                             }
  435.                             System.out.println(l.equals("Eng")?"Please,input correct denominator of the second fraction":"Пожалуйста,введите корректный знаменатель второй дроби");
  436.                             denom = in.nextInt();
  437.                         }
  438.                         flag=false;
  439.                         break;
  440.                     }
  441.                     default :{
  442.                         System.out.println(l.equals("Eng")?"Functions:Sum,Create1,Create2,Multiply,Division,Pow 1,Pow 2,Swap":"Функции:Поменять местами,Сумма,Создать 1,Создать 2,Умножение,Деление,Степень 1 дробь,Степень 2 дробь");
  443.                         break;
  444.                     }
  445.                 }
  446.             }
  447.         }
  448.     }
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement