Advertisement
Guest User

Untitled

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