Commit | Line | Data |
---|---|---|
84aff5c8 H |
1 | import java.io.*; |
2 | import java.util.*; | |
3 | import java.lang.Math; | |
4 | ||
5 | /** Pisemka - program "Analyze" | |
6 | * Copylefted by: Harvie 2oo9 ( http://blog.harvie.cz/ ) | |
7 | * @author Thomas Harvie Mudrunka (mudruto1) | |
8 | * @version 1.0 | |
9 | */ | |
10 | /* Priklad: | |
11 | Zadavej posloupnost ukoncenou zapornym cislem: | |
12 | 1 | |
13 | 2 | |
14 | 3 | |
15 | 4 | |
16 | 5 | |
17 | -1 | |
18 | Byla zadana nasledujici rada: | |
19 | 1.0; 2.0; 3.0; 4.0; 5.0; | |
20 | minimum = 1.0 | |
21 | maximum = 5.0 | |
22 | Cisla delitelna 3.0: | |
23 | 3.0; | |
24 | prumer = 3,00 | |
25 | Prvocisla: | |
26 | 2.0; 3.0; 5.0; | |
27 | */ | |
28 | class analyzator { | |
29 | private double[] seq = new double[101]; | |
30 | private Scanner sc = new Scanner(System.in); | |
31 | ||
32 | public boolean readSeq() { | |
33 | System.out.println("Zadavej posloupnost ukoncenou zapornym cislem:"); | |
34 | int i;; | |
35 | for(i=0;i<seq.length;i++) { | |
36 | try { | |
37 | seq[i] = sc.nextDouble(); | |
38 | } catch(Exception e) { | |
39 | return false; | |
40 | } | |
41 | if(seq[i] < 0) break; | |
42 | } | |
43 | System.out.println("Byla zadana nasledujici rada:"); | |
44 | for(i=0;i<seq.length;i++) { | |
45 | if(seq[i] < 0) break; | |
46 | System.out.print(seq[i]+"; "); | |
47 | } | |
48 | System.out.println(""); | |
49 | return true; | |
50 | } | |
51 | ||
52 | public void vypisMinimax() { | |
53 | double min = seq[0], max = seq[0]; | |
54 | int i; | |
55 | for(i=1;i<seq.length;i++) { | |
56 | if(seq[i] < 0) break; | |
57 | if(seq[i] < min) min = seq[i]; | |
58 | if(seq[i] > max) max = seq[i]; | |
59 | } | |
60 | System.out.println("minimum = "+min); | |
61 | System.out.println("maximum = "+max); | |
62 | } | |
63 | ||
64 | public void vypisDelitelne(double a) { | |
65 | System.out.println("Cisla delitelna "+a+":"); | |
66 | int i; | |
67 | for(i=0;i<seq.length;i++) { | |
68 | if(seq[i] < 0) break; | |
69 | if((seq[i] % a) == 0) System.out.print(seq[i]+"; "); | |
70 | } | |
71 | System.out.println(""); | |
72 | } | |
73 | ||
74 | public boolean jePrvocislo(double a) { | |
75 | if(a <= 1) return false; | |
76 | if(a == 2) return true; | |
77 | if(a % 2 == 0) return false; | |
78 | int i; | |
79 | for(i = 3; i <= Math.sqrt(a); i += 2){ | |
80 | if(a % i == 0) return false; | |
81 | } | |
82 | return true; | |
83 | } | |
84 | ||
85 | public void vypisPrvocisla() { | |
86 | System.out.println("Prvocisla:"); | |
87 | int i; | |
88 | for(i=0;i<seq.length;i++) { | |
89 | if(seq[i] < 0) break; | |
90 | if(jePrvocislo(seq[i])) System.out.print(seq[i]+"; "); | |
91 | } | |
92 | System.out.println(""); | |
93 | } | |
94 | ||
95 | public void vypisPrumer() { | |
96 | int i, max; | |
97 | double avg = 0; | |
98 | for(max=0;max<seq.length;max++) if(seq[max] < 0) break; | |
99 | for(i=0;i<max;i++) avg += (seq[i]/max); | |
100 | System.out.printf("prumer = %.2f\n",avg); | |
101 | } | |
102 | } | |
103 | ||
104 | public class Analyze { | |
105 | public static void main(String[] argv) { | |
106 | //System.out.println("\n"); | |
107 | analyzator a = new analyzator(); | |
108 | if(!a.readSeq()) { | |
109 | System.err.println("Neco je spatne."); | |
110 | System.exit(1); | |
111 | } | |
112 | a.vypisMinimax(); | |
113 | a.vypisDelitelne(3); | |
114 | a.vypisPrumer(); | |
115 | a.vypisPrvocisla(); | |
116 | } | |
117 | } | |
118 | ||
119 |