Skip to content

Commit 1de31cf

Browse files
committed
C#: Implemented Observer.ToString().
1 parent 2fcaf27 commit 1de31cf

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

Diff for: generate/dotnet/csharp_test/csharp_test.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -4516,12 +4516,32 @@ static int StateVectorStringTest()
45164516
return 0;
45174517
}
45184518

4519+
static int ObserverStringCase(double latitude, double longitude, double height, string correct)
4520+
{
4521+
var observer = new Observer(latitude, longitude, height);
4522+
var text = observer.ToString();
4523+
if (text != correct)
4524+
return Fail($"{nameof(ObserverStringCase)}: expected [{correct}] but generated [{text}]");
4525+
return 0;
4526+
}
4527+
4528+
static int ObserverStringTest()
4529+
{
4530+
return (
4531+
0 == ObserverStringCase(+26.728965, -93.157562, 1234.567, "(N 26.728965, W 093.157562, 1234.567 m)") &&
4532+
0 == ObserverStringCase(-26.728965, -93.157562, 1234.567, "(S 26.728965, W 093.157562, 1234.567 m)") &&
4533+
0 == ObserverStringCase(+26.728965, +93.157562, 1234.567, "(N 26.728965, E 093.157562, 1234.567 m)") &&
4534+
0 == ObserverStringCase(+26.728965, +171.157562, 0.0, "(N 26.728965, E 171.157562, 0.000 m)")
4535+
) ? 0 : 1;
4536+
}
4537+
45194538
static int StringsTest()
45204539
{
45214540
return (
45224541
0 == TimeStringTest() &&
45234542
0 == VectorStringTest() &&
4524-
0 == StateVectorStringTest()
4543+
0 == StateVectorStringTest() &&
4544+
0 == ObserverStringTest()
45254545
) ? Pass(nameof(StringsTest)) : 1;
45264546
}
45274547

Diff for: generate/template/astronomy.cs

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2929
using System.Collections.Generic;
3030
using System.Globalization;
3131
using System.Linq;
32+
using System.Text;
3233
using System.Text.RegularExpressions;
3334

3435
namespace CosineKitty
@@ -1004,6 +1005,24 @@ public Observer(double latitude, double longitude, double height)
10041005
this.longitude = longitude;
10051006
this.height = height;
10061007
}
1008+
1009+
/// <summary>
1010+
/// Converts an `Observer` to a string representation like `(N 26.728965, W 093.157562, 1234.567 m)`.
1011+
/// </summary>
1012+
public override string ToString()
1013+
{
1014+
var sb = new StringBuilder();
1015+
sb.Append("(");
1016+
sb.Append(latitude < 0.0 ? "S " : "N ");
1017+
sb.Append(Math.Abs(latitude).ToString("00.000000"));
1018+
sb.Append(", ");
1019+
sb.Append(longitude < 0.0 ? "W " : "E ");
1020+
sb.Append(Math.Abs(longitude).ToString("000.000000"));
1021+
sb.Append(", ");
1022+
sb.Append(height.ToString("0.000"));
1023+
sb.Append(" m)");
1024+
return sb.ToString();
1025+
}
10071026
}
10081027

10091028
/// <summary>

Diff for: source/csharp/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,13 @@ from a particular place on the Earth.
33963396
| `double` | `longitude` | Geographic longitude in degrees east (positive) or west (negative) of the prime meridian at Greenwich, England. |
33973397
| `double` | `height` | The height above (positive) or below (negative) sea level, expressed in meters. |
33983398

3399+
### member functions
3400+
3401+
<a name="Observer.ToString"></a>
3402+
### Observer.ToString() &#8658; `string`
3403+
3404+
**Converts an `Observer` to a string representation like `(N 26.728965, W 093.157562, 1234.567 m)`.**
3405+
33993406
---
34003407

34013408
<a name="Refraction"></a>

Diff for: source/csharp/astronomy.cs

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2929
using System.Collections.Generic;
3030
using System.Globalization;
3131
using System.Linq;
32+
using System.Text;
3233
using System.Text.RegularExpressions;
3334

3435
namespace CosineKitty
@@ -1004,6 +1005,24 @@ public Observer(double latitude, double longitude, double height)
10041005
this.longitude = longitude;
10051006
this.height = height;
10061007
}
1008+
1009+
/// <summary>
1010+
/// Converts an `Observer` to a string representation like `(N 26.728965, W 093.157562, 1234.567 m)`.
1011+
/// </summary>
1012+
public override string ToString()
1013+
{
1014+
var sb = new StringBuilder();
1015+
sb.Append("(");
1016+
sb.Append(latitude < 0.0 ? "S " : "N ");
1017+
sb.Append(Math.Abs(latitude).ToString("00.000000"));
1018+
sb.Append(", ");
1019+
sb.Append(longitude < 0.0 ? "W " : "E ");
1020+
sb.Append(Math.Abs(longitude).ToString("000.000000"));
1021+
sb.Append(", ");
1022+
sb.Append(height.ToString("0.000"));
1023+
sb.Append(" m)");
1024+
return sb.ToString();
1025+
}
10071026
}
10081027

10091028
/// <summary>

0 commit comments

Comments
 (0)