doudansui6650
2017-11-17 16:03
浏览 352
已采纳

在Go中对数组进行排序和比较

I am a beginner Go student and I've been given the following assignment that needs to be built in Go:
-Read in a data file that contains all 83 counties of Michigan and the coordinates (in decimal) for each county seat.
-Set beginning coordinates to the college I attend. -Prompt user for a direction (n,s,e,w) and the program then finds the closest county seat in that specified direction. The found county seat then becomes the new starting point and the program starts over with the direction prompt.
-the program needs to show an error if the user hits water or is about to leave the state.

I am able to do most of this without issue, but here is where I'm hitting a brick wall: My plan is to use two arrays; one for latitude and one for longitude. I need to sort each array so that the numbers are in order from the eastern most county seat to the western most county seat (for latitude and the same for longtitude). My thinking is that if the user enters that they want to go west, the program will take the latitude coordinate of the starting point, determine where in the array it is supposed to be positioned then the program selects the closest coordinate in the numbers to the right of starting coordinate. The program should then take the determined coordinate and reference the main map of coordinates, find the matching complete set of coordinates then set the starting point to the new coordinates. I'm using float64s for the coordinate data types.

So to break it down: the data file reads in the coordinates of {1, 1.2} {2,1.3} {3, 2.4} {4, 5.4} {5, 6.6}; the starting point is (1,2) and the user wants to go west. So the latitude array holds {1.2, 1.3, 2.4, 5.4, 6.6} and the program needs to take the 2 from the starting, determine that it belongs between the 1.3 and 2.4, the determine that the 2.4 is the closest coordinate to the starting point. Then it takes the 2.4 and compares it to the data file coordinates to match the {3, 2.4}. The starting point is now {3, 2.4}. Then the program starts over from the user choosing a direction.

I apologize for the novel but I wanted to make sure I was as clear (as mud, I know LOL) as possible. How do I code the sort and comparisons? If there is an easier way to complete this assignment, I would be incredibly grateful for any help. Please keep in mind, I've only been working with Go for about a month and a half. Please be gentle with complicated code. LOL Thank you!

图片转代码服务由CSDN问答提供 功能建议

我是Go语言的初学者,我收到了以下需要在Go语言中构建的作业:\ n
-读取一个数据文件,其中包含密歇根州的所有83个县以及每个县城的坐标(十进制)。 -将开始的坐标设置为我参加的大学。 -提示用户输入方向(n,s,e,w),然后程序会在该指定方向上找到最近的县城。 然后,找到的县城将成为新的起点,程序将根据方向提示重新开始。
-如果用户上水或即将离开该州,该程序需要显示一个错误。

我能够完成大多数操作,而不会遇到问题,但这是我遇到的障碍。 我的计划是使用两个阵列; 一种用于纬度,另一种用于经度。 我需要对每个数组进行排序,以使数字从最东部的县城到最西部的县城(对于纬度而言,和对于经度而言,是相同的)排序。 我的想法是,如果用户输入要西行的信息,程序将获取起点的纬度坐标,确定应在数组中定位的位置,然后程序会选择数字中最接近坐标的坐标 起始坐标的权利。 然后,程序应获取确定的坐标并参考主坐标图,找到匹配的完整坐标集,然后将起点设置为新坐标。 我将float64s用于坐标数据类型。

因此要对其进行细分:数据文件读取的坐标为{1,1.2} {2,1.3} {3,2.4 } {4,5.4} {5,6.6}; 起点是(1,2),用户想向西走。 因此,纬度数组保存{1.2,1.3,2.4,5.4,6.6},程序需要从起始处取2,确定它属于1.3和2.4之间,确定2.4是最接近起始处的坐标 点。 然后,它取2.4并将其与数据文件坐标进行比较以匹配{3,2.4}。 现在的起点是{3,2.4}。 然后程序从用户选择方向重新开始。

我对此小说表示歉意,但我想确保自己尽可能的清楚(就像泥泞,我知道大声笑)。 如何编写排序和比较代码? 如果有一种更简单的方法来完成此作业,我将非常感谢您的帮助。 请记住,我与Go在一起仅工作了一个半月左右。 请谨慎处理复杂的代码。 大声笑谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsjbest2014 2017-11-17 17:08
    已采纳

    From a design perspective, consider creating a type that encapsulates the information about each county seat (e.g. a struct with the city/town name, latitude, and longitude) since they are logically related.

    The code example below shows how you could sort these objects by latitude or longitude using sort.Slice(...), making it easier to find which one is closest in the specified direction.

    Hope it helps!

    package main
    
    import "sort"
    
    type CountySeat struct {
      Name     string
      Lat, Lng float64
    }
    
    func main() {
      // Pretend we've read these from the data file.
      countySeats := []CountySeat{
        CountySeat{"Alpha", 3.0, 1.0},
        CountySeat{"Bravo", 2.0, 2.0},
        CountySeat{"Charlie", 1.0, 3.0},
      }
    
      // Sorting by latitude makes it easy to find N/S proximity.
      sort.Slice(countySeats, func(i, j int) bool {
        return countySeats[i].Lat < countySeats[j].Lat;
      })
      // Now countySeats are ordered by increasing latitude...
    
      // Sorting by longitude makes it easy to find E/W proximity.
      sort.Slice(countySeats, func(i, j int) bool {
        return countySeats[i].Lng < countySeats[j].Lng;
      })
      // Now countySeats are ordered by increasing longitude...
    
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题