From 8660ad8c8a187deef6f9f7fdb61d9d3351457e7d Mon Sep 17 00:00:00 2001 From: Scott Baldwin Date: Fri, 7 Jul 2017 13:11:08 +1000 Subject: [PATCH 1/6] Added Build Script --- build.ps1 | 18 ++++++++++++++++++ .../ComplexCalculator.Tests.csproj | 2 ++ src/ComplexCalculator.Tests/packages.config | 1 + 3 files changed, 21 insertions(+) create mode 100644 build.ps1 diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..e349ce2 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,18 @@ +<# +.SYNOPSIS + +Builds and runs tests for the Complex Calculator project +#> +if (-not (Get-Command MsBuild -ErrorAction SilentlyContinue)) +{ + Get-VsVars32 +} + +MSBuild .\src\ComplexCalculator\ComplexCalculator.sln + +if ($LASTEXITCODE -eq 0) +{ + .\src\ComplexCalculator\packages\xunit.runner.console.2.3.0-beta3-build3705\tools\net452\xunit.console.exe .\src\ComplexCalculator.Tests\bin\Debug\ComplexCalculator.Tests.dll +} + +exit $LASTEXITCODE diff --git a/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj b/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj index e81e4ec..8b2e73d 100644 --- a/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj +++ b/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj @@ -1,5 +1,6 @@  + @@ -76,6 +77,7 @@ + \ No newline at end of file diff --git a/src/ComplexCalculator.Tests/packages.config b/src/ComplexCalculator.Tests/packages.config index 21c654a..918f657 100644 --- a/src/ComplexCalculator.Tests/packages.config +++ b/src/ComplexCalculator.Tests/packages.config @@ -7,5 +7,6 @@ + \ No newline at end of file From ad00c0bd0967f5e6fbeac0307d4a71493c070588 Mon Sep 17 00:00:00 2001 From: Scott Baldwin Date: Fri, 7 Jul 2017 12:47:49 +1000 Subject: [PATCH 2/6] Created ComplexNumber class Also deleted the initial test class --- .../ComplexCalculator.Tests.csproj | 8 ++++- .../ComplexNumberTestClass.cs | 33 +++++++++++++++++++ src/ComplexCalculator.Tests/TestClass.cs | 18 ---------- .../ComplexCalculator.csproj | 1 + src/ComplexCalculator/ComplexNumber.cs | 11 +++++++ src/ComplexCalculator/Program.cs | 4 --- 6 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/ComplexCalculator.Tests/ComplexNumberTestClass.cs delete mode 100644 src/ComplexCalculator.Tests/TestClass.cs create mode 100644 src/ComplexCalculator/ComplexNumber.cs diff --git a/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj b/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj index 8b2e73d..4a81871 100644 --- a/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj +++ b/src/ComplexCalculator.Tests/ComplexCalculator.Tests.csproj @@ -57,7 +57,7 @@ - + @@ -69,6 +69,12 @@ + + + {75d70f12-d503-450e-9ec8-d32be6491aef} + ComplexCalculator + + diff --git a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs new file mode 100644 index 0000000..1aee287 --- /dev/null +++ b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs @@ -0,0 +1,33 @@ +using Xunit; + +namespace ComplexCalculator.Tests +{ + public class ComplexNumberTestClass + { + [Fact] + public void WhenRealPartSet_ItCanBeReturned() + { + // Arrange + var num = new ComplexNumber(); + + // Act + num.Real = 42; + + // Assert + Assert.Equal(42, num.Real); + } + + [Fact] + public void WhenImaginaryPartSet_ItCanBeReturned() + { + // Arrange + var num = new ComplexNumber(); + + // Act + num.Imaginary = 13; + + // Assert + Assert.Equal(13, num.Imaginary); + } + } +} diff --git a/src/ComplexCalculator.Tests/TestClass.cs b/src/ComplexCalculator.Tests/TestClass.cs deleted file mode 100644 index fadd9a0..0000000 --- a/src/ComplexCalculator.Tests/TestClass.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; - -namespace ComplexCalculator.Tests -{ - public class TestClass - { - [Fact] - public void FirstTest() - { - Assert.True(true); - } - } -} diff --git a/src/ComplexCalculator/ComplexCalculator.csproj b/src/ComplexCalculator/ComplexCalculator.csproj index 0e5c7c8..89d86b3 100644 --- a/src/ComplexCalculator/ComplexCalculator.csproj +++ b/src/ComplexCalculator/ComplexCalculator.csproj @@ -42,6 +42,7 @@ + diff --git a/src/ComplexCalculator/ComplexNumber.cs b/src/ComplexCalculator/ComplexNumber.cs new file mode 100644 index 0000000..ef4a07a --- /dev/null +++ b/src/ComplexCalculator/ComplexNumber.cs @@ -0,0 +1,11 @@ +using System; + +namespace ComplexCalculator +{ + public class ComplexNumber + { + public double Real { get; set; } + + public double Imaginary { get; set; } + } +} diff --git a/src/ComplexCalculator/Program.cs b/src/ComplexCalculator/Program.cs index 0fc8f5c..a51bde7 100644 --- a/src/ComplexCalculator/Program.cs +++ b/src/ComplexCalculator/Program.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ComplexCalculator { From a74d5821eabcf59e6692632372c87591ffb6be0f Mon Sep 17 00:00:00 2001 From: Scott Baldwin Date: Fri, 7 Jul 2017 12:51:45 +1000 Subject: [PATCH 3/6] Implement modulous --- .../ComplexNumberTestClass.cs | 13 +++++++++++++ src/ComplexCalculator/ComplexNumber.cs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs index 1aee287..f4c8f77 100644 --- a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs +++ b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs @@ -29,5 +29,18 @@ public void WhenImaginaryPartSet_ItCanBeReturned() // Assert Assert.Equal(13, num.Imaginary); } + + [Fact] + public void WhenModulousIsCalled_ItIsCalculatedAndReturned() + { + // Arrange + var num = new ComplexNumber { Real = 3, Imaginary = 4 }; + + // Act + var mod = num.Modulous(); + + // Assert + Assert.Equal(5, mod); + } } } diff --git a/src/ComplexCalculator/ComplexNumber.cs b/src/ComplexCalculator/ComplexNumber.cs index ef4a07a..a5a920a 100644 --- a/src/ComplexCalculator/ComplexNumber.cs +++ b/src/ComplexCalculator/ComplexNumber.cs @@ -7,5 +7,10 @@ public class ComplexNumber public double Real { get; set; } public double Imaginary { get; set; } + + public double Modulous() + { + return Math.Sqrt(Real * Real + Imaginary * Imaginary); + } } } From 834a4e7ebe9c555cfe1cf1d01f75fca0601611b1 Mon Sep 17 00:00:00 2001 From: Scott Baldwin Date: Fri, 7 Jul 2017 15:00:55 +1000 Subject: [PATCH 4/6] Implement ToString --- .../ComplexNumberTestClass.cs | 15 +++++++++++++++ src/ComplexCalculator/ComplexNumber.cs | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs index f4c8f77..d06b0fc 100644 --- a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs +++ b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs @@ -42,5 +42,20 @@ public void WhenModulousIsCalled_ItIsCalculatedAndReturned() // Assert Assert.Equal(5, mod); } + + [Theory()] + [InlineData(3.5, -2.8, "3.5-2.8i")] + [InlineData(7.2, 4.1, "7.2+4.1i")] + public void WhenToStringCalled_ReturnsStringREpresentationOfComplexNumber(double real, double imaginary, string result) + { + // Arrange + var num = new ComplexNumber { Real = real, Imaginary = imaginary }; + + // Act + var str = num.ToString(); + + // Assert + Assert.Equal(result, str); + } } } diff --git a/src/ComplexCalculator/ComplexNumber.cs b/src/ComplexCalculator/ComplexNumber.cs index a5a920a..15e8dd1 100644 --- a/src/ComplexCalculator/ComplexNumber.cs +++ b/src/ComplexCalculator/ComplexNumber.cs @@ -12,5 +12,11 @@ public double Modulous() { return Math.Sqrt(Real * Real + Imaginary * Imaginary); } + + public override string ToString() + { + var sign = Imaginary > 0 ? "+" : ""; + return $"{Real}{sign}{Imaginary}i"; + } } } From 0b50979d578704ed407c8f6f5eeb28ea01d4d93f Mon Sep 17 00:00:00 2001 From: Scott Baldwin Date: Sun, 16 Jul 2017 16:49:22 +1000 Subject: [PATCH 5/6] Implement Add Method on ComplexNumer --- .../ComplexNumberTestClass.cs | 18 ++++++++++++++++++ src/ComplexCalculator/ComplexNumber.cs | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs index d06b0fc..e26e68d 100644 --- a/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs +++ b/src/ComplexCalculator.Tests/ComplexNumberTestClass.cs @@ -57,5 +57,23 @@ public void WhenToStringCalled_ReturnsStringREpresentationOfComplexNumber(double // Assert Assert.Equal(result, str); } + + [Theory()] + [InlineData(1, 2, 1, 2, 2, 4)] + [InlineData(1, 1, -1, -1, 0, 0)] + [InlineData(1, 3, -2, -5, -1, -2)] + void WhenComplexNumbersAdded_TheirRealPartsAndImaginaryPartsAreAdded(double r1, double i1, double r2, double i2, double r3, double i3) + { + // Arrange + var n1 = new ComplexNumber { Real = r1, Imaginary = i1 }; + var n2 = new ComplexNumber { Real = r2, Imaginary = i2 }; + + // Act + var n3 = n1.Add(n2); + + // Assert + Assert.Equal(r3, n3.Real); + Assert.Equal(i3, n3.Imaginary); + } } } diff --git a/src/ComplexCalculator/ComplexNumber.cs b/src/ComplexCalculator/ComplexNumber.cs index 15e8dd1..4c5119c 100644 --- a/src/ComplexCalculator/ComplexNumber.cs +++ b/src/ComplexCalculator/ComplexNumber.cs @@ -18,5 +18,14 @@ public override string ToString() var sign = Imaginary > 0 ? "+" : ""; return $"{Real}{sign}{Imaginary}i"; } + + public ComplexNumber Add(ComplexNumber num) + { + return new ComplexNumber + { + Real = this.Real + num.Real, + Imaginary = this.Imaginary + num.Imaginary + }; + } } } From 3faf417dc716fa70a37466d262838eccf6a7834f Mon Sep 17 00:00:00 2001 From: Scott Baldwin Date: Sun, 16 Jul 2017 16:48:15 +1000 Subject: [PATCH 6/6] Add User Input Allows user to input 2 complex numbers and it adds them --- src/ComplexCalculator/Program.cs | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/ComplexCalculator/Program.cs b/src/ComplexCalculator/Program.cs index a51bde7..e2253aa 100644 --- a/src/ComplexCalculator/Program.cs +++ b/src/ComplexCalculator/Program.cs @@ -6,6 +6,41 @@ class Program { static void Main(string[] args) { + Console.WriteLine("This program adds 2 complex numbers"); + ComplexNumber c1 = new ComplexNumber + { + Real = ReadDouble("Enter Real Part of first complex number"), + Imaginary = ReadDouble("Enter Imaginary part of first complex number") + }; + ComplexNumber c2 = new ComplexNumber + { + Real = ReadDouble("Enter Real Part of second complex number"), + Imaginary = ReadDouble("Enter Imaginary part of second complex number") + }; + + var c3 = c1.Add(c2); + + Console.WriteLine("{0} + {1} = {2} with modulous {3}", c1, c2, c3, c3.Modulous()); + } + + private static double ReadDouble(string message) + { + double val; + string s; + bool success = false; + do + { + Console.Write(message + ":"); + s = Console.ReadLine(); + success = double.TryParse(s, out val); + if (!success) + { + Console.WriteLine("ERROR please try again."); + } + } while (!success); + + return val; + } } }