Advertisement
Guest User

zad 3 i 4

a guest
Nov 25th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.36 KB | None | 0 0
  1. import scala.io._
  2.  
  3.  
  4.   type Set = Int => Boolean
  5.   def contains(s:Set,elem:Int):Boolean = s(elem)
  6.   //A: Jednoelementowy zbior:
  7.   def singletonSet(elem:Int):Set = x => x==elem //porownuje x do elem
  8.   //B: polaczenie obu zbiorow
  9.   def union(s:Set,t:Set):Set = x => s(x)||t(x)
  10.   //C: iloczyn zbiorow (xEA ixEB)
  11.   def intersect(s:Set,t:Set):Set = x => s(x)&&t(x)
  12.   //D: roznica zbiorow
  13.   def diff(s:Set,t:Set):Set = x => s(x) && (!t(x))
  14.   //E: elem, spelniaja predykat
  15.   def filter(s:Set,p:Int=>Boolean):Set = x => s(x)&&p(x)
  16.   def predEven (x:Int) = x%2 == 0
  17.   val s1 = singletonSet(1)          //s1 = Set(1)
  18.   val s2 = singletonSet(2)          //s2 = Set(2)
  19.   val s3 = singletonSet(3)          //s3 = Set(3)
  20.   val s5 = singletonSet(5)          //s5 = Set(5)
  21.   val s6 = singletonSet(6)          //s5 = Set(6)
  22.   val united123 = union(s1,union(s2,s3))
  23.   val united156 = union(s1,union(s5,s6))
  24.   val united12356 = union(united123,united156)
  25.   val intersected1 = intersect(s1,united123)
  26.   val diff23 = diff(united123,united156)
  27.   val evenFiltered26 = filter (united12356,predEven)
  28.  
  29.   //pomocnicza funkcja zamieniajaca set na liste (od 0 do max)
  30.   def toList(s:Set,max:Int):List[Int] = {
  31.     var xs:List[Int] = List()
  32.     for( i <- 0 to max){
  33.       //if element i is in set s add it to the list
  34.       if(s(i))
  35.         xs= i::xs
  36.     }
  37.     xs.reverse
  38.   }
  39.  
  40. //lista 6 zad 3
  41.   def print_set (s:Set)={
  42.   //drukuje zbior, arg: set i funkcja zmieniajaca go na liste
  43.  
  44.     val xs=toList(s,100)
  45.     println("Elementy zbioru: ")
  46.     println()
  47.  
  48.     for(i <-0 to (xs.length-1)){
  49.       print(xs(i)+", ")
  50.     }
  51.     println()
  52.   }
  53. //lista 6 zad 4
  54.   def input_one(s:Set):Set={//metoda dodajaca jeden element do istniejacego juz zbioru
  55.      println("Podaj liczbe ktora chcesz dodac do zbioru: ")
  56.     union(s,singletonSet(StdIn.readInt()))
  57.   }
  58.  
  59.   def input_multiple (s:Set):Set = {//dodajaca wiele elementow do istniejacego juz zbioru
  60.  
  61.     var newSet: Set =input_one(s)
  62.  
  63.     println("Kolejny element? true/false")
  64.  
  65.     while (StdIn.readBoolean()) {
  66.       newSet =input_one(newSet)
  67.  
  68.       println("Kolejny element? true/false")
  69.     }
  70.  
  71.     newSet
  72.   }
  73.  
  74.   //tworzenie nowego zbioru
  75.   def create_set:Set = {
  76.     println("Podaj pierwszy element nowego zbioru: ")
  77.     var newSet: Set =singletonSet(StdIn.readInt())
  78.  
  79.     println("Czy chcesz dodac wiecej elementow? true/false")
  80.     if(StdIn.readBoolean()){
  81.       newSet=input_multiple(newSet)
  82.     }
  83.     newSet
  84.   }
  85.  
  86.   def working_on_sets = {
  87.     println("Zbior A: ")
  88.     var setA=create_set
  89.     println("Zbior B: ")
  90.     var setB=create_set
  91.  
  92.     print_set(setA)
  93.     print_set(setB)
  94.  
  95.     //wybor funkcji
  96.     print_menu
  97.     var answer= StdIn.readInt()
  98.     answer match {
  99.       case 1 => print_set(union(setA,setB))
  100.       case 2 => print_set(intersect(setA,setB))
  101.       case 3 => print_set(diff(setA,setB))
  102.       case 4 => {
  103.         println("Zbior A: ")
  104.         setA=create_set
  105.         println("Zbior B: ")
  106.         setB=create_set
  107.  
  108.         print_set(setA)
  109.         print_set(setB)
  110.       }
  111.       case 5 => {
  112.         println("Parzyste z setu A")
  113.         print_set(  filter (setA,predEven))
  114.         println("Parzyste z setu B")
  115.         print_set (filter(setB,predEven))
  116.       }
  117.     }
  118.  
  119.     println("Cos jeszcze? true/false")
  120.     while(StdIn.readBoolean()) {
  121.  
  122.       print_menu
  123.       answer= StdIn.readInt()
  124.  
  125.       answer match {
  126.         case 1 => print_set(union(setA,setB))
  127.         case 2 => print_set(intersect(setA,setB))
  128.         case 3 => print_set(diff(setA,setB))
  129.         case 4 => {
  130.           println("Zbior A: ")
  131.           setA=create_set
  132.           println("Zbior B: ")
  133.           setB=create_set
  134.  
  135.           print_set(setA)
  136.           print_set(setB)
  137.         }
  138.         case 5 => {
  139.           println("Parzyste z setu A")
  140.           print_set(  filter (setA,predEven))
  141.           println("Parzyste z setu B")
  142.           print_set (filter(setB,predEven))
  143.         }
  144.       }
  145.  
  146.       println("Cos jeszcze? true/false")
  147.  
  148.     }
  149.  
  150.   }
  151.  
  152.   def print_menu ={
  153.     println("1. Suma zbiorow\n" +
  154.       "2. Iloczyn\n" +
  155.       "3. Roznica\n" +
  156.       "4. Nadpisz obydwa zbiory\n" +
  157.       "5. Filtrowanie parzystosci")
  158.  
  159.   }
  160.  
  161.  
  162.   def main(args: Array[String] ): Unit = {
  163.     println("Hello World")
  164.     //zad 3
  165.     print_set(united12356)
  166.  
  167.     //zad 4 b
  168.  
  169.     working_on_sets
  170.  
  171.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement