Advertisement
Guest User

Libery

a guest
Apr 14th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 11.13 KB | None | 0 0
  1. //Funkcja zmieniająca char na int
  2. let parseIntC(c: char) = (int c) - 48;
  3.  
  4. //Funkcja zwracająca większą z podanych wartości
  5. let maximum(a: int, b:int) = if a>b then a else b;
  6.  
  7. //Funkcja zamieniająca listy miejscami.
  8. let swap (left : 'a byref) (right : 'a byref) =
  9.   let temp = left
  10.   left <- right
  11.   right <- temp    
  12.  
  13. //Metoda zamieniająca liczbę w postaci tablicy na łańcuch znaków
  14. let asString(listI: list<int>, listF: list<int>) =
  15.     let rec convRec(lista: list<int>, str: string, count:int) =
  16.         if count=lista.Length then str else convRec(lista, str+(string lista.[count]), count+1)
  17.     if listI.IsEmpty&&listF.IsEmpty then "0"
  18.     elif listI.IsEmpty then "0." + convRec(listF, "", 0)
  19.     elif listF.IsEmpty then convRec(listI, "", 0)
  20.     else convRec(listI, "", 0) + "." + convRec(listF, "", 0)        
  21.  
  22. //Funkcja zwracająca tablicę stworzoną z podanej listy o długości max
  23. let cut(listA:list<int>, max:int)=
  24.     let rec cuts(listA:list<int>, listB:list<int>,iter:int)=
  25.         if iter = max then [|listA;listB|]
  26.         else cuts(listA.Tail,listA.Head::listB,iter+1)
  27.     let rett = cuts(listA,[],0)
  28.     rett
  29.  
  30. //Funkcja zamieniająca string na int
  31. let parseInt(s: string) =
  32.     let rec parse(position: int, value: int, multiply: int) =
  33.         if position<0 then value else parse(position-1, value+(multiply*parseIntC(s.[position])), multiply*10)
  34.     parse(s.Length-1, 0, 1);
  35.  
  36. //Funkcja zamieniająca liczbe całkowitą w postaci stringa na przedstawiającą go listę cyfr
  37. let toBigNumberArray(str: string) =
  38.     let rec converse(lista: list<int>, pos: int, sign: int) =
  39.         if pos=str.Length then lista else converse((sign*parseIntC(str.[pos]))::lista, pos+1, sign);
  40.     if (int str.[0])=45 then
  41.         List.rev(converse([], 1, -1))
  42.     else
  43.         List.rev(converse([], 0, 1));
  44.  
  45. //Funkcja zmieniająca część całkowitą liczby w postaci string na listę cyfr                  
  46. let parserInteger(str: string) =
  47.     let splitted = str.Split([|'.'; 'e'|]);
  48.     toBigNumberArray(splitted.[0])
  49.  
  50. //Funkcja zmieniająca część dziesiętną liczby w postaci string na listę cyfr        
  51. let parserFloat(str: string) =
  52.     let splitted = str.Split([|'.'|]);
  53.     if splitted.Length<=1 then []
  54.     else
  55.        let ndsplitted = splitted.[1].Split([|'e'|]);
  56.        if (int str.[0])=45 then toBigNumberArray("-"+ndsplitted.[0]) else toBigNumberArray(ndsplitted.[0])
  57.  
  58. //Funkcja zamieniająca liczbę zmiennoprzecinkową w postaci string z obsługą notacji wykładniczej na reprezentujące ją listy cyfr
  59. let parserE(str: string) =
  60.     let integerList = parserInteger(str);
  61.     let floatList = parserFloat(str);
  62.     let splitted = str.Split([|'e'|]);
  63.     let rec backwards(listI: list<int>, listF: list<int>, count: int) =
  64.         if count=parseInt(splitted.[1].[1..(splitted.[1].Length-1)]) then [|listI; listF|]
  65.         elif listI.IsEmpty then backwards([], 0::listF, count+1)
  66.         else backwards(List.rev(List.rev(listI).Tail), List.rev(listI).Head::listF, count+1)
  67.     let rec forwards(listI: list<int>, listF: list<int>, count: int) =
  68.         if count=parseInt(splitted.[1]) then [|listI; listF|]
  69.         elif listF.IsEmpty then forwards(List.rev(0::List.rev(listI)), [], count+1)
  70.         else forwards(List.rev(listF.Head::List.rev(listI)), listF.Tail, count+1)
  71.     if splitted.Length>1 then
  72.         if splitted.[1].Contains("-") then
  73.             backwards(integerList, floatList, 0);
  74.         else
  75.             forwards(integerList, floatList, 0);
  76.     else [|integerList; floatList|]
  77.  
  78. //Funkcja uzupełniająca tablicę o nieznaczące zera
  79. let addZero(listA:list<int>, max:int)=
  80.     let rec recZero(listA:list<int>,listB:list<int>,iter:int,too:int)=
  81.         if iter = too then listB
  82.         else
  83.             if not(listA.IsEmpty) then recZero(listA.Tail,listA.Head::listB,iter+1,too)
  84.             else recZero(listA,0::listB,iter+1,too)
  85.     let ret = recZero(listA,[],0,max)
  86.     List.rev(ret)
  87.        
  88. //Funkcja potrzeban w odejmowaniu. Jeżeli nie można odjąć danej cyfry, to pobiera potrzebne jej wartości z bardziej znaczących miejsc
  89. let tak(listka:list<int>)=
  90.         let rets = List.toArray(listka)
  91.         let rec take(iter:int)=
  92.             if iter<rets.Length then
  93.                  if rets.[iter] > 0 then
  94.                     rets.[iter] <- listka.[iter]-1
  95.                     9
  96.                  else
  97.                     let tmp = take(iter+1)
  98.                     if tmp > 0 then
  99.                         rets.[iter]<-9
  100.                         9
  101.                     else 0
  102.             else 0
  103.         if take(1) > 0 then
  104.             rets.[0]<- listka.[0]+10
  105.         List.ofArray(rets)
  106.  
  107. //Funkcja wykonująca odejmowanie. Przyjmuje 2 listy reprezentujące liczby oraz ilość miejsc dziesiętnych, a zwraca listę z wynikiem.
  108. let less(listA:list<int>,listB:list<int>,flo:int)=
  109.     let rec lessFloat(listF1:list<int>,listF2:list<int>,listR:list<int>,pos:int,too:int)=
  110.         if pos = too then listR
  111.         elif listF2.IsEmpty then lessFloat(listF1.Tail,listF2,listF1.Head::listR,pos+1,too)
  112.         elif listF1.Head < listF2.Head then lessFloat(tak(listF1),listF2,listR,pos,too)                        
  113.         else lessFloat(listF1.Tail,listF2.Tail,listF1.Head-listF2.Head::listR,pos+1,too)
  114.     let tst = lessFloat(List.rev(listA),List.rev(listB),[],0,listA.Length)
  115.     let wynik = cut(List.rev(tst),flo)
  116.     [|List.rev(wynik.[0]);wynik.[1]|]
  117.  
  118. //Funkcja zwracająca prawdę jeżeli pierwsza liczba jest większa od 2giej i fałsz, gdy jest odwrotnie    
  119. let getBigger(listA1:list<int>,listA2:list<int>,listB1:list<int>,listB2:list<int>) =
  120.     let rec check(listA:list<int>, listB:list<int>, iter:int)=
  121.         if iter<listA.Length then
  122.             if listA.Head > listB.Head then false
  123.             elif listA.Head < listB.Head then true
  124.             else check(listA.Tail,listB.Tail,iter+1)
  125.         else false
  126.     if listA1.Length>listB1.Length then false        
  127.     elif listA1.Length<listB1.Length then true        
  128.     else check(listA1@listA2,listB1@listB2,0)
  129.  
  130. //Funkcja przyjmujące 2 liczby w postaci string i zwracająca wynik odejmowania 1szej od 2giej w postaci string                            
  131. let minus(numFirst:string, numSecond:string) =
  132.     let mutable _first = parserE(numFirst);
  133.     let mutable _second = parserE(numSecond);
  134.     if _first.[1].Length < _second.[1].Length then
  135.        _first.[1] <- addZero(_first.[1],_second.[1].Length)
  136.     elif _second.[1].Length < _first.[1].Length then
  137.         _second.[1]<-addZero(_second.[1],_first.[1].Length)
  138.     let _big = getBigger(_first.[0],_first.[1],_second.[0],_second.[1])
  139.     if _big then
  140.         swap(&_first)(&_second)    
  141.     let wyn = less(_first.[0]@_first.[1],_second.[0]@_second.[1],_first.[1].Length)
  142.     if _big then "-" + asString(wyn.[0],wyn.[1]) else asString(wyn.[0],wyn.[1])
  143.  
  144. //Funkcja dodająca 2 liczby przyjęte w postaci string i zwracająca wynik ich dodawania w postaci string
  145. let plus(numFirst: string, numSecond: string) =
  146.     let first = parserE(numFirst);
  147.     let second = parserE(numSecond);
  148.     let rec addiR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
  149.         if pos=too then result
  150.         elif listF.IsEmpty&&not(listS.IsEmpty) then addiR([], List.rev(List.rev(listS).Tail), pos+1, List.rev(listS).Head::result, too)
  151.         elif listS.IsEmpty&&not(listF.IsEmpty) then addiR(List.rev(List.rev(listF).Tail), [], pos+1, List.rev(listF).Head::result, too)
  152.         else addiR(List.rev(List.rev(listF).Tail), List.rev(List.rev(listS).Tail), pos+1, (List.rev(listF).Head+List.rev(listS).Head)::result, too)
  153.     let rec addfR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
  154.         if pos=too then List.rev(result)
  155.         elif listF.IsEmpty&&not(listS.IsEmpty) then addfR([], listS.Tail, pos+1, listS.Head::result, too)
  156.         elif listS.IsEmpty&&not(listF.IsEmpty) then addfR(listF.Tail, [], pos+1, listF.Head::result, too)
  157.         else addfR(listF.Tail, listS.Tail, pos+1, (listF.Head+listS.Head)::result, too)
  158.     let rec checkI (lista: list<int>, count: int, result: list<int>, next: bool) =
  159.         if count<0 then result
  160.         else
  161.             let mutable elem = if lista.IsEmpty then 0 else lista.[count]
  162.             if next then elem <- elem+1
  163.             if lista.[count] >9 then checkI(lista, count-1, (elem-10)::result,true) else  checkI(lista, count-1, elem::result,false)  
  164.              
  165.     let mutable integerPart = addiR(first.[0], second.[0],0,[], maximum(first.[0].Length,second.[0].Length));
  166.     let mutable floatPart =  addfR(first.[1], second.[1],0,[], maximum(first.[1].Length,second.[1].Length));
  167.     integerPart <- checkI(integerPart, integerPart.Length-1, [], false);
  168.     floatPart <- checkI(floatPart, floatPart.Length-1, [], false);
  169.     if not(floatPart.IsEmpty)&&floatPart.[0]>10
  170.     then
  171.         floatPart <- floatPart.Head-10::floatPart.Tail
  172.         integerPart <- checkI(List.rev(List.rev(integerPart).Head+1::List.rev(integerPart).Tail), integerPart.Length-1, [], false)
  173.     asString(integerPart, floatPart)
  174.  
  175. //Funkcja obsługująca odejmowanie wielkich liczb, przyjmuje 2 liczby w postaci string i zwraca wynik odejmowania również w tej postaci
  176. let sub(numFirst:string, numSecond:string)=
  177.     if numFirst.[0] = '-' && numSecond.[0] = '-' then
  178.         minus(numSecond.[1..numFirst.Length-1],numFirst.[1..numSecond.Length-1])
  179.     elif numFirst.[0] = '-' && not(numSecond.[0] = '-') then
  180.         "-"+plus(numSecond,numFirst.[1..numFirst.Length-1])
  181.     elif not(numFirst.[0] = '-') && numSecond.[0] ='-' then
  182.         plus(numFirst,numSecond.[1..numSecond.Length-1])
  183.     else minus(numFirst,numSecond)
  184.  
  185. //Funkcja obsługująca dodawanie wielkich liczb, przyjmuje 2 liczby w postaci string i zwraca wynik odejmowania również w tej postaci
  186. let add(numFirst:string, numSecond:string)=
  187.     if numFirst.[0] = '-' && numSecond.[0] = '-' then
  188.         "-"+plus(numFirst.[1..numFirst.Length-1],numSecond.[1..numSecond.Length-1])
  189.     elif numFirst.[0] = '-' && not(numSecond.[0] = '-') then
  190.         minus(numSecond,numFirst.[1..numFirst.Length-1])
  191.     elif not(numFirst.[0] = '-') && numSecond.[0] ='-' then
  192.         minus(numFirst,numSecond.[1..numSecond.Length-1])
  193.     else plus(numFirst,numSecond)
  194.  
  195.  
  196. //Funckja testująca odejmowanie wielkich liczb
  197. let test = sub("26666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666677777777777777777777777777777777777777777777777777777777777777777777777777777777777778888888888888888888888888888888888888888888888888888888888888888888888888888444444444444444444444444444444444444444444444444444444444444444444444444222222222222222222222222222222222123.213e4200","-3666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666.1238888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888e1880")
  198.  
  199. printf "%s" test;
  200.  
  201. System.Console.ReadKey(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement