Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.commo;
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class Main {
- public static void main(String args[]) {
- while (true) {
- ClosestPair matcher = new ClosestPair(); // Create Matcher Object
- Scanner scan = new Scanner(System.in); // Create a Scanner object
- Arrays e24 = new Arrays(); //create arrays object
- double ar1[] = e24.getAr1(); // Fill the Arrays
- double ar2[] = e24.getAr1();
- int m = ar1.length; // Set Vars to Calc
- int n = ar2.length;
- String Ohm = "Ω"; // Get Unicode Char for Ohms
- double x = 0; // Init desired value with value 0
- double result[] = new double[4]; // save result R's here
- String input = "NaN"; //initialize Scanner Variable with NaN
- System.out.println("Moin! Das ist ein Widerstandsrechner für Reihen- und Parallelschaltung. \nBitte einen Wert eingeben im Format XX.xx, Dekaden werden als K, M oder G angegeben. Es wird dann die \nKombination, die am nächsten dranliegt mit Abweichung in % ausgegeben.");
- input = scan.nextLine().toLowerCase(); //get Input String & make it Case Insensitive
- input = input.replace(",", "."); //Replaces , by . to ensure, its workin with , either
- try { //exchange Chars in Input String for Decades & Parse to Double
- if (!(input.contains("k") || input.contains("m") || input.contains("g"))) {
- x = Double.parseDouble(input);
- }
- if (input.contains("k")) {
- input = input.replace("k", " ");
- x = (Double.parseDouble(input)) * 1000;
- }
- if (input.contains("m")) {
- input = input.replace("m", " ");
- x = (Double.parseDouble(input)) * 1000000;
- }
- if (input.contains("g")) {
- input = input.replace("g", " ");
- x = (Double.parseDouble(input)) * 1000000000;
- }
- //Fill Result Array with Values from matcher
- result[0] = matcher.printClosestParallel(ar1, ar2, m, n, x)[0];
- result[1] = matcher.printClosestParallel(ar1, ar2, m, n, x)[1];
- result[2] = matcher.printClosestSerial(ar1, ar2, m, n, x)[0];
- result[3] = matcher.printClosestSerial(ar1, ar2, m, n, x)[1];
- String resultStrings[] = new String[4]; //Initialize a String Array for the Results
- for (int i = 0; i < 4; i++) { //Add Decade Signs and Parse to Output String
- resultStrings[i] = Integer.toString(makeMeIntBaby(result[i])); //Casts the Result / 1000*n to Int -> If This isn't Zero -> Put Decade Sign
- if (makeMeIntBaby((result[i]) / 1000) != 0) {
- resultStrings[i] = Double.toString((result[i]) / 1000) + "k";
- }
- if (makeMeIntBaby((result[i]) / 1000000) != 0) {
- resultStrings[i] = Double.toString((result[i]) / 1000000) + "M";
- }
- if (makeMeIntBaby((result[i]) / 1000000000) != 0) {
- resultStrings[i] = Double.toString((result[i]) / 1000000000) + "G";
- }
- }
- double[] AbweichungArr = Abweichung(x, result); //get Deviation from Function
- //Output Text with Values
- System.out.println("In Parallel ist die Lösung einmal " + resultStrings[0] + Ohm + " und " + resultStrings[1] + Ohm + ". Die Abweichung beträgt dabei " + roundAndFormat(AbweichungArr[1], 2) + "%.");
- System.out.println("In Reihe ist die Lösung einmal " + resultStrings[2] + Ohm + " und " + resultStrings[3] + Ohm + ". Die Abweichung beträgt dabei " + roundAndFormat(AbweichungArr[0], 2) + "%." + "\n" + "\n" + "\n");
- } catch (Exception e) {
- System.out.println("Ungültige Eingabe! Bitte erneut versuchen!" + "\n" + "\n" + "\n"); // Catch invalid Inputs
- }
- }
- }
- public static String roundAndFormat(final double value, final int frac) { //rounds the given double value to the n'th digit where frac = n
- final java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
- nf.setMaximumFractionDigits(frac);
- return nf.format(new BigDecimal(value));
- }
- public static double[] Abweichung (double x, double[] result) {
- double rser = (result[2] + result [3]); //Calculates R-Values from Result Array
- double rpar = ((result[0] * result[1]) / (result[0] + result[1]));
- double ser = ((rser - x) / x) * 100; //Calc Deviation
- double par = ((rpar - x) / x) * 100;
- return new double[] { //Returns deviation in percent
- ser, par
- };
- }
- public static int makeMeIntBaby(double toConvert) { //Just Builds an Int from a Double Value, is used to Determine if there's need to put a Decade Sign
- Integer intobject = new Integer((int) toConvert);
- return intobject.intValue();
- }
- }
- class ClosestPair { //Here it gets complicated, There happens the magic
- public double[] printClosestSerial(double ar1[], double ar2[], int m, int n, double x) { //Doing the Math for Serial Calculation here
- if (x == 0) { //if Input R = 0 Ohm, just terminate here
- return new double[] {
- 0,
- 0
- };
- } else {
- // Initialize the diff between pair sum and x.
- double diff = Double.MAX_VALUE;
- // res_l and res_r are result indexes from ar1[] and ar2[]
- // respectively
- int res_l = 0;
- int res_r = 0;
- for (int k = 0; k < 7; k++) {
- // Start from left side of ar1[] and right side of ar2[]
- int l = 0, r = n - 1;
- while (l < m && r >= 0) {
- // If this pair is closer to x than the previously
- // found closest, then update res_l, res_r and diff
- if (Math.abs(ar1[l] + ar2[r] - x) < diff) { //Calculates R here
- res_l = l;
- res_r = r;
- diff = Math.abs(ar1[l] + ar2[r] - x);
- }
- // If sum of this pair is more than x, move to smaller
- // side
- if (ar1[l] + ar2[r] > x)
- r--;
- else // move to the greater side
- l++;
- }
- }
- return new double[] {
- ar1[res_l], ar2[res_r] //return values here
- };
- }
- }
- public double[] printClosestParallel(double ar1[], double ar2[], int m, int n, double x) { //does the same for Parallel, same as above, just the formula for the R changes
- // Initialize the diff between pair sum and x.
- double diff = Double.MAX_VALUE;
- if (x == 0) {
- return new double[] {
- 0,
- 0
- };
- } else {
- // res_l and res_r are result indexes from ar1[] and ar2[]
- // respectively
- int res_l = 0;
- int res_r = 0;
- // Start from left side of ar1[] and right side of ar2[]
- int l = 0, r = n - 1;
- while (l < m && r >= 0) {
- // If this pair is closer to x than the previously
- // found closest, then update res_l, res_r and diff
- if (Math.abs(((ar1[l] * ar2[r]) / (ar1[l] + ar2[r])) - x) < diff) { //Calculates R Here
- res_l = l;
- res_r = r;
- diff = Math.abs(((ar1[l] * ar2[r]) / (ar1[l] + ar2[r])) - x);
- }
- // If sum of this pair is more than x, move to smaller
- // side
- if (((ar1[l] * ar2[r]) / (ar1[l] + ar2[r])) > x)
- r--;
- else // move to the greater side
- l++;
- }
- return new double[] {
- ar1[res_l], ar2[res_r]
- };
- }
- }
- }
- class Arrays { //Just Contains the Array of the e24 Row to store the Resistor Values
- private double ar1[] = new double[]{0, 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7,
- 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1,
- 10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27,
- 30, 33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91,
- 100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270,
- 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910,
- 1000, 1100, 1200, 1300, 1500, 1600, 1800, 2000, 2200, 2400, 2700,
- 3000, 3300, 3600, 3900, 4300, 4700, 5100, 5600, 6200, 6800, 7500, 8200, 9100,
- 10000, 11000, 12000, 13000, 15000, 16000, 18000, 20000, 22000, 24000, 27000,
- 30000, 33000, 36000, 39000, 43000, 47000, 51000, 56000, 62000, 68000, 75000, 82000, 91000,
- 100000, 110000, 120000, 130000, 150000, 160000, 180000, 200000, 220000, 240000, 270000,
- 300000, 330000, 360000, 390000, 430000, 470000, 510000, 560000, 620000, 680000, 750000, 820000, 910000,
- 1000000, 1100000, 1200000, 1300000, 1500000, 1600000, 1800000, 2000000, 2200000, 2400000, 2700000,
- 3000000, 3300000, 3600000, 3900000, 4300000, 4700000, 5100000, 5600000, 6200000, 6800000, 7500000, 8200000, 9100000,
- 10000000, 11000000, 12000000, 13000000, 15000000, 16000000, 18000000, 20000000, 22000000, 24000000, 27000000,
- 30000000, 33000000, 36000000, 39000000, 43000000, 47000000, 51000000, 56000000, 62000000, 68000000, 75000000, 82000000, 91000000,
- 100000000, 110000000, 120000000, 130000000, 150000000, 160000000, 180000000, 200000000, 220000000, 240000000, 270000000,
- 300000000, 330000000, 360000000, 390000000, 430000000, 470000000, 510000000, 560000000, 620000000, 680000000, 750000000, 820000000, 910000000,
- 1000000000, 1100000000, 1200000000, 1300000000, 1500000000, 1600000000, 1800000000, 2000000000, 2200000000.0, 2400000000.0, 2700000000.0,
- 3000000000.0, 3300000000.0, 3600000000.0, 3900000000.0, 4300000000.0, 4700000000.0, 5100000000.0, 5600000000.0, 6200000000.0, 6800000000.0, 7500000000.0, 8200000000.0, 9100000000.0,
- 10000000000.0, 11000000000.0, 1200000000.0, 13000000000.0, 15000000000.0, 16000000000.0, 18000000000.0, 20000000000.0, 22000000000.0, 24000000000.0, 27000000000.0,
- 30000000000.0, 33000000000.0, 36000000000.0, 39000000000.0, 43000000000.0, 47000000000.0, 51000000000.0, 56000000000.0, 62000000000.0, 68000000000.0, 75000000000.0, 82000000000.0, 91000000000.0
- };
- public double[] getAr1(){ //simple get method to fill the arrays in main with the values of the e24 array ar1.
- return ar1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement