Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сделана домашняя №5 полностью #5

Merged
merged 16 commits into from
May 16, 2015
Merged
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
*.fsproj
*.sln
*.dll
*.xml
*.bat
*.pdb
*.userprefs
*.config
*.suo
*.exe
hw*/test
tools/
bin/
obj/
.nuget/
build/
AssemblyInfo.fs
test/
unit_test/
packages/
224 changes: 224 additions & 0 deletions hw05_task37_38/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Tasks 37-38. Calc with file input/output
// by Vladimir Yumatov
// SPBU, group 171

// Expected execution time: 1.5 h
// Real time: 1.5 h

open System.IO
open NUnit.Framework
open FsUnit

type Stack<'A> () =
class
let mutable st : 'A list = []

member this.Length = st.Length
member this.Push elm = st <- elm::st

member this.Pop () =
if st.Length = 0 then failwith "Stack is empty"
else
let res = st.[0]
st <- st.Tail
res

member this.Top () =
if st.Length = 0 then failwith "Stack is empty"
else st.[0]

member this.GetList () = st

override this.ToString () = sprintf "%A" st
end


let prior operator =
match operator with
| '+' -> 1
| '-' -> 1
| '*' -> 2
| '/' -> 2
| '%' -> 2
| '^' -> 3
| _ -> 0



// translates string to an actual expression in postfix notation
// and divides it into tokens
let translate (e : string) =
let exp = "(" + e.Replace(" ", "") + ")"
//printfn "%A\n" exp
let ostc = Stack<char>()
let res = Stack<string>()

let mutable temp = ""
for i in 0..exp.Length - 1 do
let ch = exp.[i]
//printfn "%A %A %A %A" ch temp stc res
if (System.Char.IsLetterOrDigit(ch)) then
temp <- temp + ch.ToString()
else
match ch with
| '-' when (System.Char.IsLetterOrDigit(exp.[i + 1]) &&
exp.[i - 1] = '(') ->
temp <- "-"
| c when prior(c) > 0 ->
if System.Char.IsLetterOrDigit(exp.[i - 1]) then
res.Push(temp)
temp <- ""
while ostc.Length > 0 &&
prior(ostc.Top()) >= prior(ch) && prior(ch) < 3
do res.Push(ostc.Pop().ToString())
ostc.Push(ch)
| '(' -> ostc.Push(ch)
| ')' ->
if temp.Length > 0 then
res.Push(temp)
temp <- ""
while ostc.Length > 0 && ostc.Top() <> '(' do
res.Push(ostc.Pop().ToString())
if ostc.Length > 0 then ostc.Pop() |> ignore
| _ -> ()

res

// returns result of the expression
let calculate (opstack: Stack<string>) =
//let opstack = translate exp
//printfn "\n%A" opstack

let rec apply operator =
let mutable a = 0
let mutable b = 0
let mutable temp = opstack.Pop()
let isOperator (a : string) =
a.Length = 1 && prior(a.[0]) > 0

if isOperator(temp) then a <- apply temp
//elif System.Char.IsLetter(temp.[0]) then
//a <- vals.[List.findIndex ((=) temp) vars]
//elif temp.Length > 1 && temp.[0] = '-' &&
// System.Char.IsLetter(temp.[1]) then
//a <- -vals.[List.findIndex ((=) temp.[1..temp.Length - 1]) vars]
else a <- int temp
temp <- opstack.Pop()
if isOperator(temp) then b <- apply temp
//elif System.Char.IsLetter(temp.[0]) then
//b <- vals.[List.findIndex ((=) temp) vars]
//elif temp.Length > 1 && temp.[0] = '-' &&
// System.Char.IsLetter(temp.[1]) then
//b <- -vals.[List.findIndex ((=) temp.[1..temp.Length - 1]) vars]
else b <- int temp

match operator with
| "+" -> b + a
| "-" -> b - a
| "*" -> b * a
| "/" -> b / a
| "%" -> b % a
| "^" ->
let rec pow elm p =
match p with
| 0 -> 1
| 1 -> elm
| p' -> elm * (pow elm (p' - 1))
if a >= 0 then pow b a
else 1 / (pow b (-a))
| _ -> failwith "Invalid operator"

apply (opstack.Pop())


// Task 37
let writeVert (fin: StreamReader) (fout: StreamWriter) =
let st = translate (fin.ReadLine())
let lst = List.rev (st.GetList())
for i in 0..lst.Length - 1 do
if i = 0 then fout.Write(lst.[i])
else fout.Write("\n" + lst.[i])


// Task 38
let calcVert (fin: StreamReader) (fout: StreamWriter) =
let st = Stack<string>()
let mutable op = ""
while not fin.EndOfStream do
op <- fin.ReadLine()
st.Push(op)
fout.Write(string (calculate st))


[<TestCase ("0 + 1", Result = "0\n1\n+")>]
[<TestCase ("1 - 2 - 3", Result = "1\n2\n-\n3\n-")>]
[<TestCase ("3 ^ 2 ^ 2", Result = "3\n2\n2\n^\n^")>]
[<TestCase ("3 ^ 1 ^ 2", Result = "3\n1\n2\n^\n^")>]
[<TestCase ("2 + 2 * 2", Result = "2\n2\n2\n*\n+")>]
[<TestCase ("7 * (6 + 5)", Result = "7\n6\n5\n+\n*")>]
[<TestCase ("7 * 6 + 5", Result = "7\n6\n*\n5\n+")>]
let ``Task 37`` (e: string) =
let w = new StreamWriter("test.in")
w.Write(e)
w.Dispose()
let sout = new StreamWriter("test.out")
let sin = new StreamReader("test.in")
writeVert sin sout
sin.Dispose()
sout.Dispose()
let t = new StreamReader("test.out")
let res = t.ReadToEnd()
t.Dispose()
res



[<TestCase ("5 + (-5)", Result = "0")>]
[<TestCase ("999999999 + 1", Result = "1000000000")>]
[<TestCase ("1 - 15", Result = "-14")>]
[<TestCase ("1 - 2 - 3", Result = "-4")>]
[<TestCase ("6 / 2", Result = "3")>]
[<TestCase ("123 % 10", Result = "3")>]
[<TestCase ("89 * 3", Result = "267")>]
[<TestCase ("678 ^ 0", Result = "1")>]
[<TestCase ("2 ^ 1 + 3", Result = "5")>]
[<TestCase ("3 ^ 2 ^ 2", Result = "81")>]
[<TestCase ("3 ^ 1 ^ 2", Result = "3")>]
[<TestCase ("2 + 2 * 2", Result = "6")>]
let ``Task 38`` (e: string) =
let w = new StreamWriter("test.in")
w.Write(e)
w.Dispose()
let sout = new StreamWriter("test.out")
let sin = new StreamReader("test.in")
writeVert sin sout
sin.Dispose()
sout.Dispose()

let sin' = new StreamReader("test.out")
let sout' = new StreamWriter("test2.out")
calcVert sin' sout'
sin'.Dispose()
sout'.Dispose()
let t = new StreamReader("test2.out")
let res = t.ReadLine()
t.Dispose()
res




[<EntryPoint>]
let main argv =
let sin = new StreamWriter("test.in")
let sout = new StreamReader("test.out")
//writeVert sout sin
//sin.Dispose()
//sout.Dispose()
//let t = new StreamReader("test.in")
//printfn "%s" (t.ReadToEnd())
//t.Dispose()
(calcVert sout sin)
sin.Dispose()
sout.Dispose()
0
Empty file added hw05_task37_38/test.in
Empty file.
Empty file added hw05_task37_38/test.out
Empty file.
Empty file added hw05_task37_38/test2.out
Empty file.
42 changes: 42 additions & 0 deletions hw08_guicalc/build.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#! tools/FAKE/tools/FAKE.exe
#r "tools/FAKE/tools/FakeLib.dll"

open Fake

RestorePackages()


let buildDir = "./bin/build/"
let testDir = "./bin/test/"


Target "Clean" (fun _ ->
CleanDirs [buildDir; testDir]
)

Target "BuildApp" (fun _ ->
!! "src/app/**/*.fsproj"
|> MSBuildRelease buildDir "Build"
|> Log "AppBuild-Output: "
)

Target "BuildTest" (fun _ ->
!! "src/test/**/*.fsproj"
|> MSBuildDebug testDir "Build"
|> Log "TestBuild-Output: "
)

Target "Test" (fun _ ->
!! (testDir + "/*.dll")
|> NUnit (fun p ->
{p with
DisableShadowCopy = true;
OutputFile = testDir + "TestResults.xml" })
)

"Clean"
==> "BuildApp"
==> "BuildTest"
==> "Test"

RunTargetOrDefault "Test"
9 changes: 9 additions & 0 deletions hw08_guicalc/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
echo "Downloading NuGet"
curl -L https://nuget.org/nuget.exe -o NuGet.exe
chmod +x NuGet.exe
echo "Downloading FAKE..."
mono NuGet.exe Install FAKE -OutputDirectory tools -ExcludeVersion
chmod +x tools/FAKE/tools/FAKE.exe
echo "Downloading NUnit.Runners..."
mono NuGet.exe Install NUnit.Runners -OutputDirectory packages -ExcludeVersion
36 changes: 36 additions & 0 deletions hw08_guicalc/hw08_guicalc.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AE27629E-E4B0-4429-B513-ED16C2B8FCE1}"
EndProject
Project("{f2a71f9b-5d33-465a-a702-920d77279786}") = "app", "src\app\app.fsproj", "{5692BA83-09CC-46D5-A089-C3033B8C1FCB}"
EndProject
Project("{f2a71f9b-5d33-465a-a702-920d77279786}") = "test", "src\test\test.fsproj", "{D5C9E789-8AC8-432E-9BFF-CED6C79E61AF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1B6C5F59-D8AC-40BE-846A-04ED7E65B4FF}"
ProjectSection(SolutionItems) = preProject
configure = configure
build.fsx = build.fsx
build.bat = build.bat
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5692BA83-09CC-46D5-A089-C3033B8C1FCB}.Debug|x86.ActiveCfg = Debug|x86
{5692BA83-09CC-46D5-A089-C3033B8C1FCB}.Debug|x86.Build.0 = Debug|x86
{5692BA83-09CC-46D5-A089-C3033B8C1FCB}.Release|x86.ActiveCfg = Release|x86
{5692BA83-09CC-46D5-A089-C3033B8C1FCB}.Release|x86.Build.0 = Release|x86
{D5C9E789-8AC8-432E-9BFF-CED6C79E61AF}.Debug|x86.ActiveCfg = Debug|Any CPU
{D5C9E789-8AC8-432E-9BFF-CED6C79E61AF}.Debug|x86.Build.0 = Debug|Any CPU
{D5C9E789-8AC8-432E-9BFF-CED6C79E61AF}.Release|x86.ActiveCfg = Release|Any CPU
{D5C9E789-8AC8-432E-9BFF-CED6C79E61AF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5692BA83-09CC-46D5-A089-C3033B8C1FCB} = {AE27629E-E4B0-4429-B513-ED16C2B8FCE1}
{D5C9E789-8AC8-432E-9BFF-CED6C79E61AF} = {AE27629E-E4B0-4429-B513-ED16C2B8FCE1}
EndGlobalSection
EndGlobal
Loading