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 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)
- printfn "%A\n%A" first.[0] second.[0]
- addiR(first.[0], second.[0],0,[], maximum(first.[0].Length,second.[0].Length));
- (*
- printfn "%A\n%A" first.[1] second.[1]
- addfR(first.[1], second.[1],0,[], maximum(first.[1].Length,second.[1].Length));
- *)
- let test = add("6161e5", "1212.13e5");
- printf "%A" test;
- System.Console.ReadKey(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement