Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let parseIntC(c: char) = (int c) - 48;
- let maximum(a: int, b:int) = if a>b then a else b;
- let parseInt(s: string) =
- let rec parse(position: int, value: int, multiply: int) =
- if position<0 then value else parse(position-1, value+(multiply*parseIntC(s.[position])), multiply*10)
- parse(s.Length-1, 0, 1);
- let toBigNumberArray(str: string) =
- let rec converse(lista: list<int>, pos: int, sign: int) =
- if pos=str.Length then lista else converse((sign*parseIntC(str.[pos]))::lista, pos+1, sign);
- if (int str.[0])=45 then
- List.rev(converse([], 1, -1))
- else
- List.rev(converse([], 0, 1));
- let parserInteger(str: string) =
- let splitted = str.Split([|'.'; 'e'|]);
- toBigNumberArray(splitted.[0])
- let parserFloat(str: string) =
- let splitted = str.Split([|'.'|]);
- if splitted.Length<=1 then []
- else
- let ndsplitted = splitted.[1].Split([|'e'|]);
- if (int str.[0])=45 then toBigNumberArray("-"+ndsplitted.[0]) else toBigNumberArray(ndsplitted.[0])
- let parserE(str: string) =
- let integerList = parserInteger(str);
- let floatList = parserFloat(str);
- let splitted = str.Split([|'e'|]);
- let rec backwards(listI: list<int>, listF: list<int>, count: int) =
- if count=parseInt(splitted.[1].[1..(splitted.[1].Length-1)]) then [|listI; listF|]
- elif listI.IsEmpty then backwards([], 0::listF, count+1)
- else backwards(List.rev(List.rev(listI).Tail), List.rev(listI).Head::listF, count+1)
- let rec forwards(listI: list<int>, listF: list<int>, count: int) =
- if count=parseInt(splitted.[1]) then [|listI; listF|]
- elif listF.IsEmpty then forwards(List.rev(0::List.rev(listI)), [], count+1)
- else forwards(List.rev(listF.Head::List.rev(listI)), listF.Tail, count+1)
- if splitted.Length>1 then
- if splitted.[1].Contains("-") then
- backwards(integerList, floatList, 0);
- else
- forwards(integerList, floatList, 0);
- else [|integerList; floatList|]
- let asString(listI: list<int>, listF: list<int>) =
- let rec convRec(lista: list<int>, str: string, count:int) =
- if count=lista.Length then str else convRec(lista, str+(string lista.[count]), count+1)
- if listI.IsEmpty&&listF.IsEmpty then "0"
- elif listI.IsEmpty then "0." + convRec(listF, "", 0)
- elif listF.IsEmpty then convRec(listI, "", 0)
- else convRec(listI, "", 0) + "." + convRec(listF, "", 0)
- let add(numFirst: string, numSecond: string) =
- let first = parserE(numFirst);
- let second = parserE(numSecond);
- let rec addiR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
- if pos=too then result
- elif listF.IsEmpty&¬(listS.IsEmpty) then addiR([], List.rev(List.rev(listS).Tail), pos+1, List.rev(listS).Head::result, too)
- elif listS.IsEmpty&¬(listF.IsEmpty) then addiR(List.rev(List.rev(listF).Tail), [], pos+1, List.rev(listF).Head::result, too)
- 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)
- let rec addfR(listF: list<int>, listS: list<int>, pos: int, result: list<int>, too: int) =
- if pos=too then List.rev(result)
- elif listF.IsEmpty&¬(listS.IsEmpty) then addfR([], listS.Tail, pos+1, listS.Head::result, too)
- elif listS.IsEmpty&¬(listF.IsEmpty) then addfR(listF.Tail, [], pos+1, listF.Head::result, too)
- else addfR(listF.Tail, listS.Tail, pos+1, (listF.Head+listS.Head)::result, too)
- let rec checkI (lista: list<int>, count: int, result: list<int>, next: bool) =
- if count<0 then result
- else
- let mutable elem = if lista.IsEmpty then 0 else lista.[count]
- if next then elem <- elem+1
- if lista.[count] >9 then checkI(lista, count-1, (elem-10)::result,true) else checkI(lista, count-1, elem::result,false)
- let mutable integerPart = addiR(first.[0], second.[0],0,[], maximum(first.[0].Length,second.[0].Length));
- let mutable floatPart = addfR(first.[1], second.[1],0,[], maximum(first.[1].Length,second.[1].Length));
- integerPart <- checkI(integerPart, integerPart.Length-1, [], false);
- floatPart <- checkI(floatPart, floatPart.Length-1, [], false);
- if not(floatPart.IsEmpty)&&floatPart.[0]>10
- then
- floatPart <- floatPart.Head-10::floatPart.Tail
- integerPart <- checkI(List.rev(List.rev(integerPart).Head+1::List.rev(integerPart).Tail), integerPart.Length-1, [], false)
- asString(integerPart, floatPart)
- let test = add("1912.13e5", "6161e5");
- printf "%s" test;
- System.Console.ReadKey(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement