Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type ulamek = int * int
- exception DzieleniePrzezZero
- //let ( ++ ) (a, b) (c, d) = (a*d + b*c, b*d)
- //let ( -- ) (a, b) (c, d) = (a*d - b*c, b*d)
- //let ( ** ) (a, b) (c, d) = (a*c, b*d)
- //let ( /./ ) (a, b) (c, d) = (a,b) ** (d,c)
- //let ( == ) (a, b) (c, d) = (a*d = b*c)
- let rec NWD = function
- | (0, n) -> n
- | (m, n) -> NWD(n % m, m)
- let skroc (l,m) =
- let znak = if l*m<0 then -1 else 1
- let al = abs l
- let am = abs m
- let d = NWD(al,am)
- in (znak * (al / d), am / d)
- let naString (l, m) =
- let znak = if l*m<0 then "-" else "" (* deklaracje wewnetrzne *)
- let al = abs l (* wart.bezw. = liczba nat. *)
- let am = abs m
- let d = NWD(al, am)
- in znak + (System.Convert.ToString(al / d))
- + "/" + (System.Convert.ToString(am / d))
- // ZADANIE 2
- type samochod = {marka : string; rocznik : int; pojemnosc : float}
- let siena = { marka = "FIAT"; rocznik = 1997; pojemnosc = 1586.0}
- let maluch = { marka = "FIAT"; rocznik = 1987; pojemnosc = 648.0}
- let malolitrazowy(x:samochod) = x.pojemnosc < 900.0;
- type osoba = {imie : string; urodzony : int; adres : string}
- let kier1 = {imie="Adam"; adres = "Rzeszów"; urodzony = 1960};
- let kier2 = {imie="Olek"; adres = "Jasło"; urodzony = 1986};
- let starszy(k1:osoba,k2:osoba) = k1.urodzony < k2.urodzony
- type rej = {pojazd : samochod; posiadacz: osoba}
- let sam1:rej = {pojazd = siena; posiadacz = kier1};
- let sam2:rej = {pojazd = maluch; posiadacz = kier2};
- let wiekszapojemnosc (s1: rej, s2 :rej) =
- s1.pojazd.pojemnosc > s2.pojazd.pojemnosc;
- // Zadania samodzielne
- // 1
- let checkIfZero (b, d) =
- if b = 0 || d = 0 then raise DzieleniePrzezZero
- //let ( ++ ) (a, b) (c, d) =
- // checkIfZero (b, d)
- // (a*d + b*c, b*d)
- //let ( -- ) (a, b) (c, d) =
- // checkIfZero (b, d)
- // (a*d - b*c, b*d)
- //let ( ** ) (a, b) (c, d) =
- // checkIfZero (b, d)
- // (a*c, b*d)
- //let ( /./ ) (a, b) (c, d) =
- // checkIfZero (b, d)
- // checkIfZero (b, c)
- // (a,b) ** (d,c)
- //let ( == ) (a, b) (c, d) =
- // checkIfZero (b, d)
- // (a*d = b*c)
- // 2
- type ulamekR = { l: int; m: int }
- let ( ++++ ) a b =
- checkIfZero(a.m, b.m)
- { l = a.l * b.m; m = a.m * b.l }
- let ( ---- ) a b =
- checkIfZero(a.m, b.m)
- { l = a.l * b.m - a.m * b.l; m = a.m * b.l }
- let ( **** ) a b =
- checkIfZero(a.m, b.m)
- { l = a.l * b.l; m = a.m * b.m }
- let ( /.../ ) a b =
- checkIfZero(a.m, b.m)
- checkIfZero(a.m, b.l)
- a **** { l = b.m; m = b.l }
- let ( ==== ) a b =
- checkIfZero(a.m, b.m)
- a.l * b.m = a.m * b.l
- let skrocR a =
- let znak = if a.l*a.m<0 then -1 else 1
- let al = abs a.l
- let am = abs a.m
- let d = NWD(al,am)
- { l = znak * (al / d); m = am / d }
- let naStringR a =
- let znak = if a.l*a.m<0 then "-" else "" (* deklaracje wewnetrzne *)
- let al = abs a.l (* wart.bezw. = liczba nat. *)
- let am = abs a.m
- let d = NWD(al, am)
- in znak + (System.Convert.ToString(al / d))
- + "/" + (System.Convert.ToString(am / d))
- // 3
- let ( ++ ) (a, b) (c, d) =
- checkIfZero (b, d)
- skroc (a*d + b*c, b*d)
- let ( -- ) (a, b) (c, d) =
- checkIfZero (b, d)
- skroc (a*d - b*c, b*d)
- let ( ** ) (a, b) (c, d) =
- checkIfZero (b, d)
- skroc (a*c, b*d)
- let ( /./ ) (a, b) (c, d) =
- checkIfZero (b, d)
- checkIfZero (b, c)
- skroc (a,b) ** (d,c)
- let ( == ) (a, b) (c, d) =
- checkIfZero (b, d)
- (a*d = b*c)
- // 4
- let naString4 (l, m) =
- let znak = if l*m<0 then "-" else "" (* deklaracje wewnetrzne *)
- let al = abs l (* wart.bezw. = liczba nat. *)
- let am = abs m
- let d = NWD(al, am)
- let almin = al / d
- let ammin = am / d
- let calk = almin / ammin
- let rest = almin % ammin
- in znak + (System.Convert.ToString(calk))
- + " "
- + (System.Convert.ToString(rest))
- + "/"
- + (System.Convert.ToString(ammin))
- // 5
- let ( +++ ) (a, b) (c, d) =
- (a + c, b + d)
- let ( --- ) (a, b) (c, d) =
- (a - c, b - d)
- let ( *** ) (a, b) (c, d) =
- (a * c - b * d, a * d - b * c)
- let odwrotnosc (a, b) =
- let c = a*a + b*b
- if c = 0 then raise DzieleniePrzezZero
- else (a / c, -b / c)
- let ( /../ ) (a, b) (c, d) =
- (a, b) ** odwrotnosc (c, d)
- let sqrtComplex (a, b) =
- 0
- // 6
- let rec powerToUlamek (a, b) n =
- if n = 0 then (1, 1)
- elif n < 0 then skroc (a, b)
- elif n = 1 then skroc (a * a, b * b)
- else powerToUlamek (a * a, b * b) (n - 1)
- // 7
- let rec pierwsza' (n, o) =
- if o < 1 then false
- elif o = 1 then true
- elif n % o = 0 then false
- else pierwsza' (n, o - 1)
- let pierwsza n = pierwsza' (n, n - 1)
- // 8
- let rec podzielnaOdDo (o, d, dzielnik) =
- if o > d then -1
- elif o % dzielnik = 0 then o
- else podzielnaOdDo (o + 1, d, dzielnik)
- // 9
- let multiplyUlamekLiczba (a, b) c =
- checkIfZero(b, b)
- skroc (a * c, b)
- let divideUlamekLiczba (a, b) c =
- checkIfZero(b, c)
- skroc ((a, b) /./ (c, 1))
- let sqrtUlamek (a, b) =
- (System.Math.Sqrt(a), System.Math.Sqrt(b))
- let odwrUlamek (a, b) =
- checkIfZero(a, b)
- (b, a)
- // 10
- type osobaX = {imie : string; nazwisko : string; telefon : string}
- let osoba1 = { imie = "Jan"; nazwisko = "Kowalski"; telefon = "111000222" }
- let osoba2 = { imie = "Roman"; nazwisko = "Nowak"; telefon = "222000111" }
- let checkIfSameSurname a b =
- a.nazwisko = b.nazwisko
- // 11
- type ksiazka = { title: string; author: string; year: int; price: float }
- let ksiazka1 = { title = "Tytuł"; author = "me"}
- // main
- [<EntryPoint>]
- let main _ =
- //let q1 = (-2, 0)
- //let q2 = (5, -10)
- //let q3 = skroc (q1 ++ q2)
- //let q4 = skroc (q2 ** q3)
- //let q5 = skroc (q4 /./ q3)
- printf "%d" (podzielnaOdDo (11, 17, 5))
- //printf "%A\n" (skroc q3)
- //printf "%A\n" (q1 -- q2)
- //printf "%A\n" (q4)
- //printf "%A\n" (q5)
- //printf "%A\n" (q1 == q2)
- //printf "%s\n" (naString q4)
- //printf "%s\n" (naString q5)
- //printf "%d\n" (NWD (22, 24))
- //printf "%d\n" (NWD (0, 12))
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement