Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. package com.commo;
  2.  
  3.  
  4. import java.math.BigDecimal;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String args[]) {
  10. while (true) {
  11. ClosestPair matcher = new ClosestPair(); // Create Matcher Object
  12. Scanner scan = new Scanner(System.in); // Create a Scanner object
  13. Arrays e24 = new Arrays(); //create arrays object
  14. double ar1[] = e24.getAr1(); // Fill the Arrays
  15. double ar2[] = e24.getAr1();
  16. int m = ar1.length; // Set Vars to Calc
  17. int n = ar2.length;
  18. String Ohm = "Ω"; // Get Unicode Char for Ohms
  19. double x = 0; // Init desired value with value 0
  20. double result[] = new double[4]; // save result R's here
  21. String input = "NaN"; //initialize Scanner Variable with NaN
  22.  
  23. 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.");
  24.  
  25. input = scan.nextLine().toLowerCase(); //get Input String & make it Case Insensitive
  26. input = input.replace(",", "."); //Replaces , by . to ensure, its workin with , either
  27.  
  28. try { //exchange Chars in Input String for Decades & Parse to Double
  29. if (!(input.contains("k") || input.contains("m") || input.contains("g"))) {
  30. x = Double.parseDouble(input);
  31. }
  32. if (input.contains("k")) {
  33. input = input.replace("k", " ");
  34. x = (Double.parseDouble(input)) * 1000;
  35. }
  36. if (input.contains("m")) {
  37. input = input.replace("m", " ");
  38. x = (Double.parseDouble(input)) * 1000000;
  39. }
  40. if (input.contains("g")) {
  41. input = input.replace("g", " ");
  42. x = (Double.parseDouble(input)) * 1000000000;
  43. }
  44.  
  45. //Fill Result Array with Values from matcher
  46. result[0] = matcher.printClosestParallel(ar1, ar2, m, n, x)[0];
  47. result[1] = matcher.printClosestParallel(ar1, ar2, m, n, x)[1];
  48. result[2] = matcher.printClosestSerial(ar1, ar2, m, n, x)[0];
  49. result[3] = matcher.printClosestSerial(ar1, ar2, m, n, x)[1];
  50. String resultStrings[] = new String[4]; //Initialize a String Array for the Results
  51.  
  52. for (int i = 0; i < 4; i++) { //Add Decade Signs and Parse to Output String
  53. resultStrings[i] = Integer.toString(makeMeIntBaby(result[i])); //Casts the Result / 1000*n to Int -> If This isn't Zero -> Put Decade Sign
  54. if (makeMeIntBaby((result[i]) / 1000) != 0) {
  55. resultStrings[i] = Double.toString((result[i]) / 1000) + "k";
  56. }
  57. if (makeMeIntBaby((result[i]) / 1000000) != 0) {
  58. resultStrings[i] = Double.toString((result[i]) / 1000000) + "M";
  59. }
  60. if (makeMeIntBaby((result[i]) / 1000000000) != 0) {
  61. resultStrings[i] = Double.toString((result[i]) / 1000000000) + "G";
  62. }
  63. }
  64.  
  65.  
  66. double[] AbweichungArr = Abweichung(x, result); //get Deviation from Function
  67.  
  68. //Output Text with Values
  69. 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) + "%.");
  70. 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");
  71.  
  72. } catch (Exception e) {
  73. System.out.println("Ungültige Eingabe! Bitte erneut versuchen!" + "\n" + "\n" + "\n"); // Catch invalid Inputs
  74. }
  75. }
  76. }
  77.  
  78. public static String roundAndFormat(final double value, final int frac) { //rounds the given double value to the n'th digit where frac = n
  79. final java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
  80. nf.setMaximumFractionDigits(frac);
  81. return nf.format(new BigDecimal(value));
  82. }
  83.  
  84.  
  85. public static double[] Abweichung (double x, double[] result) {
  86. double rser = (result[2] + result [3]); //Calculates R-Values from Result Array
  87. double rpar = ((result[0] * result[1]) / (result[0] + result[1]));
  88. double ser = ((rser - x) / x) * 100; //Calc Deviation
  89. double par = ((rpar - x) / x) * 100;
  90.  
  91. return new double[] { //Returns deviation in percent
  92. ser, par
  93. };
  94. }
  95.  
  96.  
  97. 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
  98. Integer intobject = new Integer((int) toConvert);
  99. return intobject.intValue();
  100. }
  101.  
  102. }
  103.  
  104. class ClosestPair { //Here it gets complicated, There happens the magic
  105. public double[] printClosestSerial(double ar1[], double ar2[], int m, int n, double x) { //Doing the Math for Serial Calculation here
  106.  
  107. if (x == 0) { //if Input R = 0 Ohm, just terminate here
  108. return new double[] {
  109. 0,
  110. 0
  111. };
  112. } else {
  113. // Initialize the diff between pair sum and x.
  114. double diff = Double.MAX_VALUE;
  115.  
  116. // res_l and res_r are result indexes from ar1[] and ar2[]
  117. // respectively
  118. int res_l = 0;
  119. int res_r = 0;
  120.  
  121.  
  122. for (int k = 0; k < 7; k++) {
  123.  
  124. // Start from left side of ar1[] and right side of ar2[]
  125. int l = 0, r = n - 1;
  126. while (l < m && r >= 0) {
  127. // If this pair is closer to x than the previously
  128. // found closest, then update res_l, res_r and diff
  129. if (Math.abs(ar1[l] + ar2[r] - x) < diff) { //Calculates R here
  130. res_l = l;
  131. res_r = r;
  132. diff = Math.abs(ar1[l] + ar2[r] - x);
  133. }
  134.  
  135. // If sum of this pair is more than x, move to smaller
  136. // side
  137. if (ar1[l] + ar2[r] > x)
  138. r--;
  139. else // move to the greater side
  140. l++;
  141. }
  142. }
  143.  
  144. return new double[] {
  145. ar1[res_l], ar2[res_r] //return values here
  146. };
  147. }
  148. }
  149.  
  150. 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
  151. // Initialize the diff between pair sum and x.
  152. double diff = Double.MAX_VALUE;
  153.  
  154. if (x == 0) {
  155. return new double[] {
  156. 0,
  157. 0
  158. };
  159. } else {
  160.  
  161. // res_l and res_r are result indexes from ar1[] and ar2[]
  162. // respectively
  163. int res_l = 0;
  164. int res_r = 0;
  165.  
  166. // Start from left side of ar1[] and right side of ar2[]
  167. int l = 0, r = n - 1;
  168. while (l < m && r >= 0) {
  169. // If this pair is closer to x than the previously
  170. // found closest, then update res_l, res_r and diff
  171. if (Math.abs(((ar1[l] * ar2[r]) / (ar1[l] + ar2[r])) - x) < diff) { //Calculates R Here
  172. res_l = l;
  173. res_r = r;
  174. diff = Math.abs(((ar1[l] * ar2[r]) / (ar1[l] + ar2[r])) - x);
  175. }
  176.  
  177. // If sum of this pair is more than x, move to smaller
  178. // side
  179. if (((ar1[l] * ar2[r]) / (ar1[l] + ar2[r])) > x)
  180. r--;
  181. else // move to the greater side
  182. l++;
  183. }
  184. return new double[] {
  185. ar1[res_l], ar2[res_r]
  186. };
  187. }
  188. }
  189.  
  190.  
  191.  
  192. }
  193.  
  194.  
  195. class Arrays { //Just Contains the Array of the e24 Row to store the Resistor Values
  196. 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,
  197. 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,
  198. 10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27,
  199. 30, 33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91,
  200. 100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270,
  201. 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910,
  202. 1000, 1100, 1200, 1300, 1500, 1600, 1800, 2000, 2200, 2400, 2700,
  203. 3000, 3300, 3600, 3900, 4300, 4700, 5100, 5600, 6200, 6800, 7500, 8200, 9100,
  204. 10000, 11000, 12000, 13000, 15000, 16000, 18000, 20000, 22000, 24000, 27000,
  205. 30000, 33000, 36000, 39000, 43000, 47000, 51000, 56000, 62000, 68000, 75000, 82000, 91000,
  206. 100000, 110000, 120000, 130000, 150000, 160000, 180000, 200000, 220000, 240000, 270000,
  207. 300000, 330000, 360000, 390000, 430000, 470000, 510000, 560000, 620000, 680000, 750000, 820000, 910000,
  208. 1000000, 1100000, 1200000, 1300000, 1500000, 1600000, 1800000, 2000000, 2200000, 2400000, 2700000,
  209. 3000000, 3300000, 3600000, 3900000, 4300000, 4700000, 5100000, 5600000, 6200000, 6800000, 7500000, 8200000, 9100000,
  210. 10000000, 11000000, 12000000, 13000000, 15000000, 16000000, 18000000, 20000000, 22000000, 24000000, 27000000,
  211. 30000000, 33000000, 36000000, 39000000, 43000000, 47000000, 51000000, 56000000, 62000000, 68000000, 75000000, 82000000, 91000000,
  212. 100000000, 110000000, 120000000, 130000000, 150000000, 160000000, 180000000, 200000000, 220000000, 240000000, 270000000,
  213. 300000000, 330000000, 360000000, 390000000, 430000000, 470000000, 510000000, 560000000, 620000000, 680000000, 750000000, 820000000, 910000000,
  214. 1000000000, 1100000000, 1200000000, 1300000000, 1500000000, 1600000000, 1800000000, 2000000000, 2200000000.0, 2400000000.0, 2700000000.0,
  215. 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,
  216. 10000000000.0, 11000000000.0, 1200000000.0, 13000000000.0, 15000000000.0, 16000000000.0, 18000000000.0, 20000000000.0, 22000000000.0, 24000000000.0, 27000000000.0,
  217. 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
  218. };
  219. public double[] getAr1(){ //simple get method to fill the arrays in main with the values of the e24 array ar1.
  220. return ar1;
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement