Advertisement
Guest User

libery

a guest
Apr 8th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.49 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 add(numFirst: string, numSecond: string) =
  50.     let first = parserE(numFirst);
  51.     let second = parserE(numSecond);
  52.     let rec addiR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
  53.         if pos=too then result
  54.         elif listF.IsEmpty&&not(listS.IsEmpty) then addiR([], List.rev(List.rev(listS).Tail), pos+1, List.rev(listS).Head::result, too)
  55.         elif listS.IsEmpty&&not(listF.IsEmpty) then addiR(List.rev(List.rev(listF).Tail), [], pos+1, List.rev(listF).Head::result, too)
  56.         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)
  57.     let rec addfR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
  58.         if pos=too then List.rev(result)
  59.         elif listF.IsEmpty&&not(listS.IsEmpty) then addfR([], listS.Tail, pos+1, listS.Head::result, too)
  60.         elif listS.IsEmpty&&not(listF.IsEmpty) then addfR(listF.Tail, [], pos+1, listF.Head::result, too)
  61.         else addfR(listF.Tail, listS.Tail, pos+1, (listF.Head+listS.Head)::result, too)
  62.  
  63.     printfn "%A\n%A" first.[0] second.[0]
  64.     addiR(first.[0], second.[0],0,[], maximum(first.[0].Length,second.[0].Length));
  65. (*
  66.     printfn "%A\n%A" first.[1] second.[1]
  67.     addfR(first.[1], second.[1],0,[], maximum(first.[1].Length,second.[1].Length));
  68.     *)
  69.  
  70.  
  71.  
  72. let test = add("6161e5", "1212.13e5");
  73.  
  74. printf "%A" test;
  75.  
  76. System.Console.ReadKey(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement