banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

Golang實現經緯度DMS格式錄入和十進制格式打印功能

標題:Golang 實現經緯度 DMS 格式輸入和十進制格式打印功能
日期:2022-05-30 10:53:00
toc:false
index_img:https://puep.qpic.cn/coral/Q3auHgzwzM4fgQ41VTF2rPjKPe2MMQJnppZjNiaj9zdRZoYNibBRo1Xw/0
類別:

  • Go
    標籤:
  • 格式
  • 打印

題目#

為下方表格中的每個位置聲明相應的 location 結構,並使用十進制格式打印出這些位置。

表格#

探測器或著陸器著陸點緯度經度
勇氣號哥倫比亞紀念站南緯 14°34'6.2"東經 175°28'21.5"
機遇號挑戰者紀念站南緯 1°56'46.3"東經 354°28'24.2"
好奇號布萊德柏利著陸地南緯 4°35'22.2"東經 137°26'30.1"
洞察號埃律西昂平原北緯 4°30'0.0"東經 135°54'0"

代碼#

package main

import (
	"fmt"
	"strings"
)

type location struct {
	lat, long float64
}

func newLocation(lat, long coordinate) location {
	return location{
		lat:  lat.decimal(),
		long: long.decimal(),
	}
}

type coordinate struct {
	d, m, s float64
	h       rune
}

func (c coordinate) decimal() float64 {
	sign := 1.0
	switch strings.ToLower(string(c.h)) {
	case "s", "w":
		sign = -1
	}
	return sign * (c.d + c.m/60 + c.s/3600)
}

func main() {
	// 南緯14°34'6.2",東經175°28'21.5"
	spirit := newLocation(coordinate{d: 14, m: 34, s: 6.2, h: 'S'}, coordinate{d: 175, m: 28, s: 21.5, h: 'E'})
	// 南緯1°56'46.3",東經354°28'24.2"
	opportunity := newLocation(coordinate{d: 1, m: 56, s: 46.3, h: 'S'}, coordinate{d: 354, m: 28, s: 24.2, h: 'E'})
	// 南緯4°35'22.2",東經137°26'30.1"
	curiosity := newLocation(coordinate{d: 4, m: 35, s: 22.2, h: 'S'}, coordinate{d: 137, m: 26, s: 30.1, h: 'E'})
	// 北緯4°30'0.0",東經135°54'0"
	insight := newLocation(coordinate{d: 4, m: 30, s: 0.0, h: 'N'}, coordinate{d: 135, m: 54, s: 0, h: 'E'})

	fmt.Printf("spirit: %+v\n", spirit)
	fmt.Printf("opportunity: %+v\n", opportunity)
	fmt.Printf("curiosity: %+v\n", curiosity)
	fmt.Printf("insight: %+v\n", insight)
}

結果#

spirit: {lat:-14.568388888888888 long:175.4726388888889}
opportunity: {lat:-1.9461944444444446 long:354.47338888888885}
curiosity: {lat:-4.5895 long:137.44169444444444}
insight: {lat:4.5 long:135.9}
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。