Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.io._
- type Set = Int => Boolean
- def contains(s:Set,elem:Int):Boolean = s(elem)
- //A: Jednoelementowy zbior:
- def singletonSet(elem:Int):Set = x => x==elem //porownuje x do elem
- //B: polaczenie obu zbiorow
- def union(s:Set,t:Set):Set = x => s(x)||t(x)
- //C: iloczyn zbiorow (xEA ixEB)
- def intersect(s:Set,t:Set):Set = x => s(x)&&t(x)
- //D: roznica zbiorow
- def diff(s:Set,t:Set):Set = x => s(x) && (!t(x))
- //E: elem, spelniaja predykat
- def filter(s:Set,p:Int=>Boolean):Set = x => s(x)&&p(x)
- def predEven (x:Int) = x%2 == 0
- val s1 = singletonSet(1) //s1 = Set(1)
- val s2 = singletonSet(2) //s2 = Set(2)
- val s3 = singletonSet(3) //s3 = Set(3)
- val s5 = singletonSet(5) //s5 = Set(5)
- val s6 = singletonSet(6) //s5 = Set(6)
- val united123 = union(s1,union(s2,s3))
- val united156 = union(s1,union(s5,s6))
- val united12356 = union(united123,united156)
- val intersected1 = intersect(s1,united123)
- val diff23 = diff(united123,united156)
- val evenFiltered26 = filter (united12356,predEven)
- //pomocnicza funkcja zamieniajaca set na liste (od 0 do max)
- def toList(s:Set,max:Int):List[Int] = {
- var xs:List[Int] = List()
- for( i <- 0 to max){
- //if element i is in set s add it to the list
- if(s(i))
- xs= i::xs
- }
- xs.reverse
- }
- //lista 6 zad 3
- def print_set (s:Set)={
- //drukuje zbior, arg: set i funkcja zmieniajaca go na liste
- val xs=toList(s,100)
- println("Elementy zbioru: ")
- println()
- for(i <-0 to (xs.length-1)){
- print(xs(i)+", ")
- }
- println()
- }
- //lista 6 zad 4
- def input_one(s:Set):Set={//metoda dodajaca jeden element do istniejacego juz zbioru
- println("Podaj liczbe ktora chcesz dodac do zbioru: ")
- union(s,singletonSet(StdIn.readInt()))
- }
- def input_multiple (s:Set):Set = {//dodajaca wiele elementow do istniejacego juz zbioru
- var newSet: Set =input_one(s)
- println("Kolejny element? true/false")
- while (StdIn.readBoolean()) {
- newSet =input_one(newSet)
- println("Kolejny element? true/false")
- }
- newSet
- }
- //tworzenie nowego zbioru
- def create_set:Set = {
- println("Podaj pierwszy element nowego zbioru: ")
- var newSet: Set =singletonSet(StdIn.readInt())
- println("Czy chcesz dodac wiecej elementow? true/false")
- if(StdIn.readBoolean()){
- newSet=input_multiple(newSet)
- }
- newSet
- }
- def working_on_sets = {
- println("Zbior A: ")
- var setA=create_set
- println("Zbior B: ")
- var setB=create_set
- print_set(setA)
- print_set(setB)
- //wybor funkcji
- print_menu
- var answer= StdIn.readInt()
- answer match {
- case 1 => print_set(union(setA,setB))
- case 2 => print_set(intersect(setA,setB))
- case 3 => print_set(diff(setA,setB))
- case 4 => {
- println("Zbior A: ")
- setA=create_set
- println("Zbior B: ")
- setB=create_set
- print_set(setA)
- print_set(setB)
- }
- case 5 => {
- println("Parzyste z setu A")
- print_set( filter (setA,predEven))
- println("Parzyste z setu B")
- print_set (filter(setB,predEven))
- }
- }
- println("Cos jeszcze? true/false")
- while(StdIn.readBoolean()) {
- print_menu
- answer= StdIn.readInt()
- answer match {
- case 1 => print_set(union(setA,setB))
- case 2 => print_set(intersect(setA,setB))
- case 3 => print_set(diff(setA,setB))
- case 4 => {
- println("Zbior A: ")
- setA=create_set
- println("Zbior B: ")
- setB=create_set
- print_set(setA)
- print_set(setB)
- }
- case 5 => {
- println("Parzyste z setu A")
- print_set( filter (setA,predEven))
- println("Parzyste z setu B")
- print_set (filter(setB,predEven))
- }
- }
- println("Cos jeszcze? true/false")
- }
- }
- def print_menu ={
- println("1. Suma zbiorow\n" +
- "2. Iloczyn\n" +
- "3. Roznica\n" +
- "4. Nadpisz obydwa zbiory\n" +
- "5. Filtrowanie parzystosci")
- }
- def main(args: Array[String] ): Unit = {
- println("Hello World")
- //zad 3
- print_set(united12356)
- //zad 4 b
- working_on_sets
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement