城市窗体

题目:在这里插入图片描述

  • 运行效果:在这里插入图片描述
1.思路:
  • 先创建一个满足题目要求的窗体结构。
  • 创建一个线性表接口,以及用链式存储结构实现这个线性表,使其实现插入,删除,更新等操作。
  • 在主窗口完成封装。
  • 由于 刚刚接触线性表,所以参考了别人的代码,敬请谅解。
2.代码(主要):
namespace DataStruct
{
    public interface ILinearList<T> where T : IComparable<T>
    {
        int Length { get; }
        T this[int index] { get; set; }
        void Insert(int index, T data);
        void Remove(int index);
        int Search(T data);
    }
}
 
public class SLinkList<T> : ILinearList<T> where T : IComparable<T>
    {
        private SNode<T> _pHead;
        private int _length;

public T this[int index]
        {
            get
            {
                if (index < 0 || index > _length - 1)
                    throw new ArgumentOutOfRangeException();
                return Locate(index).Data;
            }
 
set
            {
                if (index < 0 || index > _length - 1)
                    throw new ArgumentOutOfRangeException();
                Locate(index).Data = value;
            }
        }
public int Length
        {
            get
            {
                return _length;
            }
        }
        public SLinkList()
        {
            _pHead = null;
            _length = 0;
        }
  public void InsertAtFirst(T data)
        {
            if (_length == 0)
            {
                _pHead = new SNode<T>(data, null);
            }
            else
            {
                _pHead = new SNode<T>(data, _pHead);
            }
  _ length++;
  }
  }
   
Button btn = sender as Button;
            string cityname = cityName.Text.Trim();
            string cityx = cityX.Text.Trim();
            string cityy = cityY.Text.Trim();
            switch (btn.Name)
            {
                    case "first_insert":
                    City city_first = new City(city_name, int.Parse(cityx), int.Parse(cityy));
                    ((SLinkList<City>)_list).InsertAtFirst(city_first);
                    listBox1.Items.Insert(0,city_first);
                    break;
                case "last_insert":
                    City city_last = new City(city_name, int.Parse(cityx), int.Parse(cityy));
((SLinkList<City>)_list).InsertAtRear(city_last);
                    listBox1.Items.Add(city_last);
                    break;
                case "insert":
                    City city = new City(city_name, int.Parse(city_x), int.Parse(city_y));
                    string index = this.insert_index.Text;
                    _list.Insert(int.Parse(index), city);
                    listBox1.Items.Insert(int.Parse(index), city);
                    break;
                case "delete":
                    string index_delete = this.delete_index.Text;
                    _list.Remove(int.Parse(index_delete));
                    listBox1.Items.RemoveAt(int.Parse(index_delete));
                    break;
                case "update":
                    City city_update = new City(city_name, int.Parse(city_x), int.Parse(city_y));
                    _list[int.Parse(this.update_index.Text)] = city_update;
                    listBox1.Items.RemoveAt(int.Parse(this.update_index.Text));
                    listBox1.Items.Insert(int.Parse(this.update_index.Text), city_update);
                    break;
                case "search":
                    SNode<City> temp=((SLinkList<City>)_list).LocateHead();
                    int i;
                    for (i = 0; i < _list.Length; i++)
                    {
                        if (temp.Data.Cityname.CompareTo(search_name.Text.Trim()) == 0)
                        {
                            search_X.Text = temp.Data.X.ToString();
                            search_Y.Text = temp.Data.Y.ToString();
                            break;
                        }
                        else
                        {
                            temp = temp.Next;
                        }
                    }
                    break;
                case "search_city":
                    int x = int.Parse(round_x.Text.Trim());
                    int y = int.Parse(round_y.Text.Trim());
                    double d = double.Parse(distance.Text.Trim());
           SNode<City> temp1 = ((SLinkList<City>)_list).LocateHead();
                    int j;
                    for(j=0;j<_list.Length;j++)
                    {
                        int x1=temp1.Data.X;
                        int y1=temp1.Data.Y;
                        double dist=Math.Sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));
                        if(dist<d)
                        {
                            CityDistance city_distance = new CityDistance(temp1.Data.Cityname, temp1.Data.X, temp1.Data.Y,dist);
                            listBox2.Items.Add(city_distance);
                        }
                        temp1=temp1.Next;
                    }
                    break;
            }
        } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值