Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package javaapplication3;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.Scanner;
  10. /**
  11. *
  12. * @author User
  13. */
  14. public class JavaApplication3 {
  15.  
  16. /**
  17. * @param args the command line arguments
  18. */
  19.  
  20. public static double [][]C; //матрица емкостей
  21. public static double [][]G; //матрица проводимостей
  22.  
  23.  
  24. public static double []X; //вектор неизвестных
  25. public static double []U; //правая часть ограничений
  26. public static int uzl;
  27.  
  28. public static void main(String[] args) {
  29. // TODO code application logic here
  30.  
  31. Scanner in = new Scanner(System.in);
  32.  
  33. System.out.println("Введите количество узлов");
  34.  
  35.  
  36. System.out.println("Введите количество элементов");
  37. int elements = in.nextInt(); //получаем число элементов
  38.  
  39. System.out.println("Введите число индуктивных элмементов");
  40. int induct = in.nextInt();
  41.  
  42. uzl += induct; //получаем оптимальные размерности
  43.  
  44. C = new double[uzl][uzl]; //матрица емкостей
  45. G = new double[uzl][uzl]; //матрица проводимостей
  46.  
  47.  
  48. X = new double[uzl]; //матрица неизвестных
  49. U = new double[uzl]; //матрица правых частей
  50.  
  51. try
  52. {
  53. for (int i = 0; i < elements; i++) //считывание элементов
  54. {
  55. System.out.println("Enter " + (i + 1) + " element");
  56. BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "Cp1251"));
  57. String q = br.readLine();
  58. analys_elements(q);
  59. }
  60.  
  61. System.out.println("C = ");
  62. for(int i = 0; i < uzl; i++)
  63. {
  64. for(int j = 0; j < uzl; j++)
  65. {
  66. System.out.print(C[i][j] + " ");
  67. }
  68. System.out.println();
  69. }
  70.  
  71. System.out.println("G = ");
  72. for(int i = 0; i < uzl; i++)
  73. {
  74. for(int j = 0; j < uzl; j++)
  75. {
  76. System.out.print(G[i][j] + " ");
  77. }
  78. System.out.println();
  79. }
  80.  
  81.  
  82.  
  83. System.out.println("X = ");
  84. for(int i = 0; i < uzl; i++)
  85. {
  86. System.out.println(X[i]);
  87. }
  88.  
  89. System.out.println("U = ");
  90. for(int i = 0; i < uzl; i++)
  91. {
  92. System.out.println(U[i]);
  93.  
  94. }
  95.  
  96.  
  97.  
  98. }
  99. catch (Exception e)
  100. {
  101. System.out.println(e);
  102. e.printStackTrace();
  103. }
  104. }
  105. public static void analys_elements(String input) throws Exception
  106. {
  107.  
  108.  
  109.  
  110. int count_pointers = 0; //число пробелов в строке
  111.  
  112. for (char element : input.toCharArray()){ //подсчет числа пробелов в строке
  113. if (element == ' ')
  114. {
  115. count_pointers++;
  116. }
  117. }
  118.  
  119. int i, j; //строки и столбцы
  120.  
  121.  
  122. int position_type = input.indexOf(" "); //ищем символ пробела после типа
  123.  
  124. String type = input.substring(0, position_type); //получаем тип элемента
  125.  
  126. String sub1 = input.substring(position_type + 1, input.length()); //получаем строку для дальнейшего парсинга
  127.  
  128.  
  129. int position_value = sub1.indexOf(" "); //ищем значение элемента элемента
  130. String str_position = sub1.substring(0, position_value); //получаем значение
  131. double value = Double.parseDouble(str_position); //парсим значение
  132.  
  133. String sub2 = sub1.substring(position_value + 1, sub1.length()); //получаем стркоу с узлами
  134.  
  135.  
  136. if (count_pointers == 2)
  137. {
  138. i = Integer.parseInt(sub2); //парсим значение индекса
  139.  
  140. j = i;
  141. }
  142. else
  143. {
  144. int node1_position = sub2.indexOf(" "); //ищем первый индекс
  145. String str_node1_value = sub2.substring(0,node1_position ); //получаем значение
  146. i = Integer.parseInt(str_node1_value); //парсим значение индекса
  147.  
  148. //String sub3 = sub2.substring(node1_position + 1, sub2.length());
  149.  
  150. int node2_position = sub2.indexOf(" "); //ищем первый индекс
  151. String sub3 = sub2.substring(node2_position + 1, sub2.length()); //получаем стркоу с узлами
  152. j = Integer.parseInt(sub3);
  153. }
  154.  
  155.  
  156. switch (type)
  157. {
  158. case "U":
  159. U[i-1] = value;
  160. break;
  161. case "C":
  162. if (count_pointers == 2 )
  163. {
  164. C[i-1][j-1] = value;
  165. }
  166. else
  167. {
  168. C[i-1][i-1] = value;
  169. C[j-1][j-1] = value;
  170. C[i-1][j-1] = -1 * value;
  171. C[j-1][i-1] = -1 * value;
  172. }
  173. break;
  174. case "Y":
  175. if (count_pointers == 2 )
  176. {
  177. G[i-1][j-1] = value;
  178. }
  179. else
  180. {
  181. G[i-1][i-1] = value;
  182. G[j-1][j-1] = value;
  183. G[i-1][j-1] = -1 * value;
  184. G[j-1][i-1] = -1 * value;
  185. }
  186. break;
  187. case "Yb":
  188. if (count_pointers == 2 )
  189. {
  190. G[i-1][j-1] = value;
  191. }
  192. else
  193. {
  194. G[i-1][i-1] = value;
  195. G[j-1][j-1] = value;
  196. G[i-1][j-1] = -1 * value;
  197. G[j-1][i-1] = -1 * value;
  198. }
  199. U[i-1] *= value;
  200. break;
  201. case "L":
  202. C[uzl-1][uzl-1] = value;
  203.  
  204. break;
  205. }
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement