public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		String line;
		int choice = -1;
		int p;
		int n;
		int k;

		while (choice != 2) {
			System.out.println("Meniu:");
			System.out.println("1. Svorio skirstinio skaiciavimas");
			System.out.println("2. Baigti darba");
			System.out.println();

			try {
				line = scanner.nextLine();
				choice = Integer.parseInt(line);

				switch (choice) {
				case 1:
					int newChoice = -1;
					System.out.println("Iveskite p:");
					line = scanner.nextLine();
					p = Integer.parseInt(line);

					System.out.println("Iveskite n:");
					line = scanner.nextLine();
					n = Integer.parseInt(line);

					System.out.println("Iveskite k:");
					line = scanner.nextLine();
					k = Integer.parseInt(line);
					System.out
							.println("Sugeneruojam atsitiktine generuojancia matrica:");
					int[][] genMatrix = MatrixOperations.generateRandomeMatrix(
							k, n, p);
					MatrixOperations.printMatrix(genMatrix);

					// Suskaiciuojame zodziu kieki
					int wordCount = Math.pow(p, k);
					while (newChoice != 3) {
						System.out
								.println("Pasirinkite svoriu skirstinio skaiciavimo buda:\n"
										+ "1. Tiesioginio perrinkimo budas\n"
										+ "2. Apskaiciuoti dualaus kodo svoriu skirstini tiesioginio perrinkimo budu ir panaudoti MacWilliams tapatybe\n"
										+ "3. Baigti skaiciavima");
						line = scanner.nextLine();
						newChoice = Integer.parseInt(line);
						System.out.println("Galima skaiciuoti tiesinio kodo C["
								+ n + "," + k + "] virs F" + p
								+ " svoriu skirstini.");

						switch (newChoice) {
						case 1:

							// Generuojame visus galimus pranesimus
							int[][] messages = getAllMessages(wordCount, k, p);

							// Randame visus kodo zodzius
							int[][] words = MatrixOperations.multiply(p,
									messages, genMatrix);
							System.out.println("Visi kodo zodziai: ");
							MatrixOperations.printMatrix(words);

							// Randame svorių skirstinį
							int[] weights = getWeights(words);
							String sWeights = "{";
							for (int i = 0; i < weights.length; i++) {
								if (i == weights.length - 1) {
									sWeights += weights[i] + "}";
								} else {
									sWeights += weights[i] + ", ";
								}
							}
							System.out.println("Kodo svoriu skirstinys: ");
							System.out.println(sWeights);
							System.out.println();
							break;
						case 2:
							System.out
									.println("Pradedamas darbas su C dualiu kodu C'");
							System.out
									.println("C' kodo generuojanti (C kontroline) matrica:");
							int[][] contMatrix = MatrixOperations
									.getControlingMatrix(p, genMatrix);
							MatrixOperations.printMatrix(contMatrix);

							// Generuojame visus galimus pranesimus dualiam
							// kodui
							wordCount = Math.pow(p, n - k);
							int[][] messagesDual = getAllMessages(wordCount, n
									- k, p);

							// Randame visus dualaus kodo zodzius
							int[][] wordsDual = MatrixOperations.multiply(p,
									messagesDual, contMatrix);
							System.out.println("Visi dualaus kodo zodziai: ");
							MatrixOperations.printMatrix(wordsDual);

							// Randame dualaus kodo svorių skirstinį
							weights = getWeights(wordsDual);
							sWeights = "{";
							for (int i = 0; i < weights.length; i++) {
								if (i == weights.length - 1) {
									sWeights += weights[i] + "}";
								} else {
									sWeights += weights[i] + ", ";
								}
							}
							System.out
									.println("Dualaus kodo svoriu skirstinys: ");
							System.out.println(sWeights);

							// Skaiciuojam C' svoriu skirstinio binoma
							List<BinomMember> weightFunctionDual = new ArrayList<BinomMember>();
							for (int i = 0; i < weights.length; i++) {
								weightFunctionDual.add(new BinomMember(1, n
										- weights[i], weights[i]));
							}
							System.out
									.println("C' kodo svoriu skirstinio binomas:");
							System.out.println(weightFunctionDual.toString());

							// Pritaikome MacWilliams tapatybe, gauname C svoriu
							// skirstinio binoma
							System.out
									.println("Pritaikome MacWilliams tapatybe, gauname kodo C svoriu skirstinio binoma:");
							ArrayList<BinomMember> binomMacWilliams = new ArrayList<BinomMember>();
							for (int i = 0; i < weightFunctionDual.size(); i++) {
								
								// Skaiciuojame atsikurs binomus
								ArrayList<BinomMember> binom1 = new ArrayList<BinomMember>();
								ArrayList<BinomMember> binom2 = new ArrayList<BinomMember>();
								ArrayList<BinomMember> multiplication = new ArrayList<BinomMember>();
								ArrayList<BinomMember> simplified = new ArrayList<BinomMember>();
								if (weightFunctionDual.get(i).getxDeg() != 0) {
									binom1.addAll(BinomOperations
											.binomToPower(weightFunctionDual
													.get(i).getxDeg(), p));
								}
								if (weightFunctionDual.get(i).getyDeg() != 0) {
									binom2.addAll(BinomOperations
											.negativeBinomToPower(weightFunctionDual
													.get(i).getyDeg()));
								}
								// Skaiciuojame binomu sandauga
								if (binom1.isEmpty()) {
									simplified = binom2;
								} else if (binom2.isEmpty()) {
									simplified = binom1;
								} else {
									multiplication.addAll(BinomOperations
											.multiplyBinoms(binom1, binom2));
									// Suprastinam gauta binoma
									simplified = BinomOperations
											.simplifyBinom(multiplication);
								}

								binomMacWilliams.addAll(simplified);

							}
							// Suprastinam galutini binoma
							binomMacWilliams = BinomOperations
									.simplifyBinom(binomMacWilliams);
							// Padalinam is C' zodziu kiekio
							binomMacWilliams = BinomOperations.divide(
									binomMacWilliams, wordCount);
							System.out.println(binomMacWilliams.toString());
							sWeights = "{";
							for (BinomMember b : binomMacWilliams) {

								for (int i = 0; i < b.getCons(); i++) {

									sWeights += b.getyDeg() + ", ";
								}

							}
							sWeights = sWeights.substring(0,
									sWeights.length() - 2);
							sWeights += "}";

							System.out.println("C kodo svoriu skirstinys: ");

							System.out.println(sWeights);
							break;
						case 3:
							System.out.println("Skaiciavimas baigtas");
							break;
						default:
							System.out.println("Tikriausiai ivedimo klaida");
						}

					}
					scanner.close();
					break;
				case 2:
					System.out.println("Programa baigta");
					scanner.close();
					break;
				default:
					System.out.println("Tikriausiai ivedimo klaida");
					scanner.close();
				}
			} catch (Exception e) {
				System.out.println("Ivyko klaida" + e.getMessage());
				e.printStackTrace();
			}
		}

	}