Zmiana inputu
This commit is contained in:
parent
9d1452fa16
commit
42bc4ae122
145
MainApp.java
145
MainApp.java
@ -1,7 +1,11 @@
|
||||
//package com.madrakrystian.algebra;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
|
||||
public class MainApp {
|
||||
|
||||
@ -24,41 +28,114 @@ public class MainApp {
|
||||
// Output: [[3,1,0,5,0,1,4,5,5], [5,2,1], DivisionError]
|
||||
|
||||
|
||||
// Input w programie dla przykladu 1
|
||||
// Podaj n:
|
||||
// 2
|
||||
// Pierwszy wielomian:
|
||||
// Podaj liczbe wyrazow w wielomianie:
|
||||
// 5
|
||||
// Podaj kolejne wyrazy wielomianu po przycisku enter
|
||||
// 1
|
||||
// 1
|
||||
// 1
|
||||
// 0
|
||||
// 1
|
||||
// Drugi wielomian:
|
||||
// Podaj liczbe wyrazow w wielomianie:
|
||||
// 3
|
||||
// Podaj kolejne wyrazy wielomianu po przycisku enter
|
||||
// 0
|
||||
// 1
|
||||
// 1
|
||||
|
||||
|
||||
|
||||
// czytanie inputu z System.in
|
||||
static Scanner reader = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// wielomiany
|
||||
// liczniki indeksow wielomianow
|
||||
int counterFirst = 0, counterSecond = 0;
|
||||
|
||||
// wielomiany + pomocniczme stosy
|
||||
int polyOne[];
|
||||
Stack<Integer> p1 = new Stack<Integer>();
|
||||
int polyTwo[];
|
||||
Stack<Integer> p2 = new Stack<Integer>();
|
||||
|
||||
// wspolczynnik n
|
||||
int n=1;
|
||||
|
||||
//input
|
||||
|
||||
String str;
|
||||
String input;
|
||||
|
||||
char temp;
|
||||
|
||||
//
|
||||
// poprawiony bardzo nieelegancki nowy input
|
||||
//
|
||||
|
||||
input = reader.nextLine();
|
||||
|
||||
BufferedReader read = new BufferedReader(
|
||||
new StringReader(input));
|
||||
|
||||
// od poczatku pierwszego wielomianu '['
|
||||
int i = 3;
|
||||
try {
|
||||
if ((str = read.readLine()) != null) {
|
||||
|
||||
if (str.length() > 0) {
|
||||
n = Character.getNumericValue(str.charAt(0));
|
||||
}
|
||||
temp = str.charAt(i);
|
||||
while(temp != ']') {
|
||||
|
||||
if(temp != ',') {
|
||||
p1.push(Character.getNumericValue(temp));
|
||||
counterFirst++;
|
||||
}
|
||||
i++;
|
||||
temp = str.charAt(i);
|
||||
}
|
||||
|
||||
// przejscie do drugiej tablicy (zakladamy ze wielomiany oddzielone sa spacja)
|
||||
i += 3;
|
||||
temp = str.charAt(i);
|
||||
|
||||
while(temp != ']') {
|
||||
|
||||
if(temp != ',') {
|
||||
p2.push(Character.getNumericValue(temp));
|
||||
counterSecond++;
|
||||
}
|
||||
i++;
|
||||
temp = str.charAt(i);
|
||||
}
|
||||
|
||||
// inicjalizacja tablic
|
||||
polyOne = new int[counterFirst];
|
||||
polyTwo = new int[counterSecond];
|
||||
|
||||
// wypelnianie tablic
|
||||
for(int j=polyOne.length-1; j>=0; j--) {
|
||||
|
||||
polyOne[j] = p1.pop();
|
||||
}
|
||||
|
||||
for(int j=polyTwo.length-1; j>=0; j--) {
|
||||
polyTwo[j] = p2.pop();
|
||||
}
|
||||
|
||||
|
||||
// wyswietl wielomiany
|
||||
System.out.println("f:" + Arrays.toString(polyOne));
|
||||
System.out.println("g:" + Arrays.toString(polyTwo));
|
||||
|
||||
// 1.
|
||||
System.out.println("1. iloczyn f*g: ");
|
||||
int res[] = modTheTab(polynomialsMultiplication(polyOne, polyTwo),n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
// 2.
|
||||
System.out.println("2. klasa reszty: ");
|
||||
res = polynomialsDivide(polyOne,polyTwo,n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
// 3.
|
||||
System.out.println("3. nwd: ");
|
||||
res = nwd(polyOne,polyTwo,n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//input(old)
|
||||
/*
|
||||
do{
|
||||
System.out.println("Podaj n: ");
|
||||
// wpisz liczbe
|
||||
@ -72,25 +149,8 @@ public class MainApp {
|
||||
|
||||
System.out.println("Drugi wielomian: ");
|
||||
polyTwo = fillPolynomial();
|
||||
*/
|
||||
|
||||
// wyswietl wielomiany
|
||||
System.out.println("f:" + Arrays.toString(polyOne));
|
||||
System.out.println("g:" + Arrays.toString(polyTwo));
|
||||
|
||||
// 1.
|
||||
System.out.println("1. iloczyn f*g: ");
|
||||
int res[] = modTheTab(polynomialsMultiplication(polyOne, polyTwo),n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
// 2.
|
||||
System.out.println("2. klasa reszty: ");
|
||||
res = polynomialsDivide(polyOne,polyTwo,n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
// 3.
|
||||
System.out.println("3. nwd: ");
|
||||
res = nwd(polyOne,polyTwo,n);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
reader.close();
|
||||
}
|
||||
@ -119,6 +179,7 @@ public class MainApp {
|
||||
return tab;
|
||||
}
|
||||
|
||||
|
||||
// dzielenie modulo tablicy
|
||||
public static int[] modTheTab(int tab[], int mod) {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user