Advertisement
Guest User

Untitled

a guest
Apr 10th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.67 KB | None | 0 0
  1.  
  2. let parseIntC(c: char) = (int c) - 48;
  3.  
  4. let maximum(a: int, b:int) = if a>b then a else b;
  5.  
  6. let parseInt(s: string) =
  7.     let rec parse(position: int, value: int, multiply: int) =
  8.         if position<0 then value else parse(position-1, value+(multiply*parseIntC(s.[position])), multiply*10)
  9.     parse(s.Length-1, 0, 1);
  10.  
  11. let toBigNumberArray(str: string) =
  12.     let rec converse(lista: list<int>, pos: int, sign: int) =
  13.         if pos=str.Length then lista else converse((sign*parseIntC(str.[pos]))::lista, pos+1, sign);
  14.     if (int str.[0])=45 then
  15.         List.rev(converse([], 1, -1))
  16.     else
  17.         List.rev(converse([], 0, 1));
  18.                    
  19. let parserInteger(str: string) =
  20.     let splitted = str.Split([|'.'; 'e'|]);
  21.     toBigNumberArray(splitted.[0])
  22.  
  23. let parserFloat(str: string) =
  24.     let splitted = str.Split([|'.'|]);
  25.     if splitted.Length<=1 then []
  26.     else
  27.        let ndsplitted = splitted.[1].Split([|'e'|]);
  28.        if (int str.[0])=45 then toBigNumberArray("-"+ndsplitted.[0]) else toBigNumberArray(ndsplitted.[0])
  29.  
  30. let parserE(str: string) =
  31.     let integerList = parserInteger(str);
  32.     let floatList = parserFloat(str);
  33.     let splitted = str.Split([|'e'|]);
  34.     let rec backwards(listI: list<int>, listF: list<int>, count: int) =
  35.         if count=parseInt(splitted.[1].[1..(splitted.[1].Length-1)]) then [|listI; listF|]
  36.         elif listI.IsEmpty then backwards([], 0::listF, count+1)
  37.         else backwards(List.rev(List.rev(listI).Tail), List.rev(listI).Head::listF, count+1)
  38.     let rec forwards(listI: list<int>, listF: list<int>, count: int) =
  39.         if count=parseInt(splitted.[1]) then [|listI; listF|]
  40.         elif listF.IsEmpty then forwards(List.rev(0::List.rev(listI)), [], count+1)
  41.         else forwards(List.rev(listF.Head::List.rev(listI)), listF.Tail, count+1)
  42.     if splitted.Length>1 then
  43.         if splitted.[1].Contains("-") then
  44.             backwards(integerList, floatList, 0);
  45.         else
  46.             forwards(integerList, floatList, 0);
  47.     else [|integerList; floatList|]
  48.  
  49. let asString(listI: list<int>, listF: list<int>) =
  50.     let rec convRec(lista: list<int>, str: string, count:int) =
  51.         if count=lista.Length then str else convRec(lista, str+(string lista.[count]), count+1)
  52.     if listI.IsEmpty&&listF.IsEmpty then "0"
  53.     elif listI.IsEmpty then "0." + convRec(listF, "", 0)
  54.     elif listF.IsEmpty then convRec(listI, "", 0)
  55.     else convRec(listI, "", 0) + "." + convRec(listF, "", 0)
  56.  
  57. let add(numFirst: string, numSecond: string) =
  58.     let first = parserE(numFirst);
  59.     let second = parserE(numSecond);
  60.     let rec addiR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
  61.         if pos=too then result
  62.         elif listF.IsEmpty&&not(listS.IsEmpty) then addiR([], List.rev(List.rev(listS).Tail), pos+1, List.rev(listS).Head::result, too)
  63.         elif listS.IsEmpty&&not(listF.IsEmpty) then addiR(List.rev(List.rev(listF).Tail), [], pos+1, List.rev(listF).Head::result, too)
  64.         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)
  65.     let rec addfR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
  66.         if pos=too then List.rev(result)
  67.         elif listF.IsEmpty&&not(listS.IsEmpty) then addfR([], listS.Tail, pos+1, listS.Head::result, too)
  68.         elif listS.IsEmpty&&not(listF.IsEmpty) then addfR(listF.Tail, [], pos+1, listF.Head::result, too)
  69.         else addfR(listF.Tail, listS.Tail, pos+1, (listF.Head+listS.Head)::result, too)
  70.     let rec checkI (lista: list<int>, count: int, result: list<int>, next: bool) =
  71.         if count<0 then result
  72.         else
  73.             let mutable elem = if lista.IsEmpty then 0 else lista.[count]
  74.             if next then elem <- elem+1
  75.             if lista.[count] >9 then checkI(lista, count-1, (elem-10)::result,true) else  checkI(lista, count-1, elem::result,false)  
  76.              
  77.     let mutable integerPart = addiR(first.[0], second.[0],0,[], maximum(first.[0].Length,second.[0].Length));
  78.     let mutable floatPart =  addfR(first.[1], second.[1],0,[], maximum(first.[1].Length,second.[1].Length));
  79.     integerPart <- checkI(integerPart, integerPart.Length-1, [], false);
  80.     floatPart <- checkI(floatPart, floatPart.Length-1, [], false);
  81.     if not(floatPart.IsEmpty)&&floatPart.[0]>10
  82.     then
  83.         floatPart <- floatPart.Head-10::floatPart.Tail
  84.         integerPart <- checkI(List.rev(List.rev(integerPart).Head+1::List.rev(integerPart).Tail), integerPart.Length-1, [], false)
  85.     asString(integerPart, floatPart)
  86.  
  87.  
  88.  
  89. let test = add("1912.13e5", "6161e5");
  90.  
  91. printf "%s" test;
  92.  
  93. System.Console.ReadKey(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement