Advertisement
Vladislav8653

laba_2_2_java

Nov 8th, 2022 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class functions {
  3.     private static final Scanner scanner = new Scanner(System.in);
  4.     public static void main (String[] args) {
  5.         int size1 = enterArraySize();
  6.         int[] arr1 = inputArray(size1);
  7.         float number1 = arrayToInt(size1, arr1);
  8.         int size2 = enterArraySize();
  9.         int[] arr2 = inputArray(size2);
  10.         float number2 = arrayToInt(size2, arr2);
  11.         divide(number1, number2);
  12.     }
  13.  
  14.     public static int inputData (int min, int max) {
  15.         int n = 0;
  16.         boolean isIncorrect;
  17.         do {
  18.             isIncorrect = false;
  19.             try {
  20.                 n = Integer.parseInt(scanner.nextLine());
  21.             } catch (Exception e) {
  22.                 isIncorrect = true;
  23.                 System.out.println("Please, enter a positive integer number:");
  24.             }
  25.             if (!isIncorrect && (n < min || n > max))  {
  26.                 isIncorrect = true;
  27.                 System.out.println("Please, enter a positive number:");
  28.             }
  29.         } while (isIncorrect);
  30.         return n;
  31.     }
  32.  
  33.     public static int enterArraySize() {
  34.         System.out.println("Input number of digits: ");
  35.         final int min = 1;
  36.         final int max = 10;
  37.         return inputData(min,max);
  38.     }
  39.  
  40.     public static int[] inputArray(int n) {
  41.         final int min = 1;
  42.         final int max = 9;
  43.         int[] arr = new int[n];
  44.         System.out.println("Enter the numbers one by one: ");
  45.         for (int i = 0; i < n; i++)
  46.             arr[i] = inputData(min, max);
  47.         return arr;
  48.     }
  49.  
  50.     public static int arrayToInt(int n, int[] arr) {
  51.         int c = 0;
  52.         for (int i = 0; i < n; i++)
  53.             c = c * 10 + arr[i];
  54.         return c;
  55.     }
  56.  
  57.     public static void divide(float number1, float number2) {
  58.         float number = number1 / number2;
  59.         int k = 1;
  60.         int numberForLoopVirgin = (int) number;
  61.         if (numberForLoopVirgin == 0)
  62.             k = 0;
  63.         else {
  64.             while ((numberForLoopVirgin /= 10) > 0)
  65.                 k++;
  66.         }
  67.         System.out.println("How many decimal places do you want to output? (<= 6)");
  68.         int decimalPlaces;
  69.         final int min = 0;
  70.         final int max = 7;
  71.         decimalPlaces = inputData(min, max);
  72.         for (int i = 0; i < decimalPlaces; i++)
  73.             number = number * 10;
  74.         int n = decimalPlaces + k;
  75.         int[] arr = new int[n];
  76.         int numberInt = (int) number;
  77.         for (int i = 0; i < n; i++) {
  78.             arr[i] = numberInt % 10;
  79.             numberInt /= 10;
  80.         }
  81.         System.out.println("Result is: ");
  82.         if (k == 0)
  83.             System.out.println("0");
  84.         for (int i = n - 1; i > decimalPlaces - 1; i--)
  85.             System.out.println(arr[i]);
  86.         if (decimalPlaces > 0) {
  87.             System.out.println(",");
  88.             for (int i = decimalPlaces - 1; i > -1; i--)
  89.                 System.out.println(arr[i]);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement