Advertisement
Dzaco

LAB3

Mar 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class Liczby {
  5.    
  6.     private int tab[] = new int[6];
  7.     private int top;
  8.    
  9.     public Liczby()
  10.     {
  11.         top = 0;
  12.     }
  13.    
  14.     private Boolean is_in_range( int x )
  15.     {
  16.         if( x > 0 && x < 50 )
  17.             return true;
  18.         else
  19.             System.out.println("Niepoprawny zakres");
  20.             return false;
  21.     }
  22.    
  23.     private Boolean is_unique( int x )
  24.     {
  25.         for( int i = 0; i < top; i++ )
  26.         {
  27.             if( x == tab[i] )
  28.             {
  29.                 System.out.println("Element juz istnieje");
  30.                 return false;
  31.             }
  32.         }
  33.        
  34.         return true;
  35.     }
  36.    
  37.     public Boolean is_value( int x )
  38.     {
  39.         for( int i : tab )
  40.         {
  41.             if(i == x)
  42.             {
  43.                 return true;
  44.             }
  45.         }
  46.        
  47.         return false;
  48.     }
  49.    
  50.     public void push( int x )
  51.     {
  52.         if( is_in_range(x) && is_unique(x) )
  53.         {
  54.             tab[top] = x;
  55.             top++;
  56.         }
  57.         else
  58.         {
  59.             System.out.println("Niepoprawna liczba");
  60.         }
  61.     }
  62.    
  63.     public int pop()
  64.     {
  65.         int x = tab[top];
  66.         top--;
  67.         return x;
  68.     }
  69.    
  70.     public int get_top()
  71.     {
  72.         return top;
  73.     }
  74.    
  75.     public void sort()
  76.     {
  77.         Arrays.sort(tab);
  78.     }
  79.  
  80.     public void display()
  81.     {
  82.         sort();
  83.        
  84.         System.out.print("[ ");
  85.                
  86.         for( int i = 0; i < get_top(); i++ )
  87.         {          
  88.             System.out.print(get_tab(i) + "\t");
  89.         }
  90.         System.out.print("]" );
  91.     }
  92.    
  93.     public int get_tab(int i)
  94.     {
  95.         return tab[i];
  96.     }
  97.    
  98.     public void rand()
  99.     {
  100.         Random R = new Random();
  101.         while( top < 6 )
  102.         {
  103.             int x = R.nextInt(49) + 1;
  104.             if( is_unique(x) )
  105.             {
  106.                 tab[top] = x;
  107.                 top++;
  108.             }
  109.         }
  110.     }
  111.    
  112.    
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////
  115.  
  116. import java.util.Scanner;
  117. import java.util.Random;
  118.  
  119. public class Lab03 {
  120.  
  121.     // Zadanie 1
  122.     public static void PrintFib( int n )
  123.     {
  124.         int Fn = 0 , Fn1 = 1 , pom = 0;
  125.         for(int i = 0; i <= n; i++)
  126.         {
  127.             System.out.println("F[" + i + "] = " + Fn);
  128.             pom = Fn + Fn1;
  129.             Fn = Fn1;
  130.             Fn1 = pom;
  131.         }
  132.     }
  133.    
  134.     public static int fib(int n)
  135.     {
  136.         if (n == 0)
  137.             return 0;
  138.         else if( n == 1)
  139.             return 1;
  140.         else
  141.             return fib(n-1)+fib(n-2);
  142.     }
  143.    
  144.    
  145.     //Zadanie 2
  146.         public static int ToInt( String str )
  147.         {
  148.             str = str.trim();
  149.             String x = "";
  150.            
  151.             for( int i = 0; i < str.length(); i++ )
  152.             {
  153.                 char pom = str.charAt(i);
  154.                 if( Character.isDigit(pom) )
  155.                 {
  156.                     x += pom;
  157.                 }
  158.             }
  159.            
  160.             if( x.isEmpty() )
  161.             {
  162.                 return 0;
  163.             }
  164.             else
  165.             {
  166.                 return Integer.parseInt(x);
  167.             }
  168.         }
  169.    
  170.     public static void main(String[] args) {
  171.         // TODO Auto-generated method stub
  172.  
  173. Scanner read = new Scanner( System.in );
  174.        
  175. /* Zadanie 1 - Fibbonacci
  176.         System.out.println("Ile wyrazów ciągu wypisać?");
  177.         System.out.print("n = ");
  178.         int n = read.nextInt();
  179.        
  180.        
  181.         System.out.println("Fibbonacci iteracyjnnie");
  182.         PrintFib(n);
  183.        
  184.         System.out.println("\nFibbonacci rekurencyjnie");
  185.         for(int i = 0; i < n + 1; i++)
  186.         {
  187.             System.out.println( "F[" + i + "] = " + fib(i) );
  188.         }*/
  189.  
  190. //Zadanie 2 - LOTTO
  191.  
  192.         Liczby L = new Liczby();
  193.  
  194.         System.out.println("Prosze podać 6 liczb");
  195.         for(; L.get_top() < 6;)
  196.         {
  197.             System.out.print(( L.get_top()+1 ) + ": ");
  198.             String s = read.next();
  199.             L.push( ToInt(s) );
  200.         }
  201.        
  202.        
  203.         System.out.println("Losowanie 6 liczb");
  204.         Liczby Los = new Liczby();
  205.        
  206.         Los.rand();
  207.         L.display();
  208.         System.out.println("<-- Twoje liczby");
  209.         Los.display();
  210.         System.out.println("<-- Wylosowane liczby");
  211.        
  212.        
  213.         Liczby Zgodne = new Liczby();
  214.                
  215.         for(int i = 0; i < 6; i++ )
  216.         {  
  217.             if( Los.is_value(L.get_tab(i)) )
  218.             {
  219.                 System.out.println(L.get_tab(i));
  220.                 int x = L.get_tab(i);
  221.                 Zgodne.push( x );
  222.                 System.out.println(Zgodne.get_tab( Zgodne.get_top()-1 ) + " , " + x );
  223.             }
  224.         }
  225.        
  226.         System.out.println("Trafiłeś " + Zgodne.get_top() + " razy");
  227.    
  228.  
  229.  
  230.  
  231.  
  232.  
  233.     }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement