Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication3;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- /**
- *
- * @author User
- */
- public class JavaApplication3 {
- /**
- * @param args the command line arguments
- */
- public static double [][]C; //матрица емкостей
- public static double [][]G; //матрица проводимостей
- public static double []X; //вектор неизвестных
- public static double []U; //правая часть ограничений
- public static int uzl;
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner in = new Scanner(System.in);
- System.out.println("Введите количество узлов");
- System.out.println("Введите количество элементов");
- int elements = in.nextInt(); //получаем число элементов
- System.out.println("Введите число индуктивных элмементов");
- int induct = in.nextInt();
- uzl += induct; //получаем оптимальные размерности
- C = new double[uzl][uzl]; //матрица емкостей
- G = new double[uzl][uzl]; //матрица проводимостей
- X = new double[uzl]; //матрица неизвестных
- U = new double[uzl]; //матрица правых частей
- try
- {
- for (int i = 0; i < elements; i++) //считывание элементов
- {
- System.out.println("Enter " + (i + 1) + " element");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "Cp1251"));
- String q = br.readLine();
- analys_elements(q);
- }
- System.out.println("C = ");
- for(int i = 0; i < uzl; i++)
- {
- for(int j = 0; j < uzl; j++)
- {
- System.out.print(C[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println("G = ");
- for(int i = 0; i < uzl; i++)
- {
- for(int j = 0; j < uzl; j++)
- {
- System.out.print(G[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println("X = ");
- for(int i = 0; i < uzl; i++)
- {
- System.out.println(X[i]);
- }
- System.out.println("U = ");
- for(int i = 0; i < uzl; i++)
- {
- System.out.println(U[i]);
- }
- }
- catch (Exception e)
- {
- System.out.println(e);
- e.printStackTrace();
- }
- }
- public static void analys_elements(String input) throws Exception
- {
- int count_pointers = 0; //число пробелов в строке
- for (char element : input.toCharArray()){ //подсчет числа пробелов в строке
- if (element == ' ')
- {
- count_pointers++;
- }
- }
- int i, j; //строки и столбцы
- int position_type = input.indexOf(" "); //ищем символ пробела после типа
- String type = input.substring(0, position_type); //получаем тип элемента
- String sub1 = input.substring(position_type + 1, input.length()); //получаем строку для дальнейшего парсинга
- int position_value = sub1.indexOf(" "); //ищем значение элемента элемента
- String str_position = sub1.substring(0, position_value); //получаем значение
- double value = Double.parseDouble(str_position); //парсим значение
- String sub2 = sub1.substring(position_value + 1, sub1.length()); //получаем стркоу с узлами
- if (count_pointers == 2)
- {
- i = Integer.parseInt(sub2); //парсим значение индекса
- j = i;
- }
- else
- {
- int node1_position = sub2.indexOf(" "); //ищем первый индекс
- String str_node1_value = sub2.substring(0,node1_position ); //получаем значение
- i = Integer.parseInt(str_node1_value); //парсим значение индекса
- //String sub3 = sub2.substring(node1_position + 1, sub2.length());
- int node2_position = sub2.indexOf(" "); //ищем первый индекс
- String sub3 = sub2.substring(node2_position + 1, sub2.length()); //получаем стркоу с узлами
- j = Integer.parseInt(sub3);
- }
- switch (type)
- {
- case "U":
- U[i-1] = value;
- break;
- case "C":
- if (count_pointers == 2 )
- {
- C[i-1][j-1] = value;
- }
- else
- {
- C[i-1][i-1] = value;
- C[j-1][j-1] = value;
- C[i-1][j-1] = -1 * value;
- C[j-1][i-1] = -1 * value;
- }
- break;
- case "Y":
- if (count_pointers == 2 )
- {
- G[i-1][j-1] = value;
- }
- else
- {
- G[i-1][i-1] = value;
- G[j-1][j-1] = value;
- G[i-1][j-1] = -1 * value;
- G[j-1][i-1] = -1 * value;
- }
- break;
- case "Yb":
- if (count_pointers == 2 )
- {
- G[i-1][j-1] = value;
- }
- else
- {
- G[i-1][i-1] = value;
- G[j-1][j-1] = value;
- G[i-1][j-1] = -1 * value;
- G[j-1][i-1] = -1 * value;
- }
- U[i-1] *= value;
- break;
- case "L":
- C[uzl-1][uzl-1] = value;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement