【发布时间】:2015-04-07 18:19:35
【问题描述】:
基本上我有一个存储 Country 类实例的 AVL 树。当我对树进行中序遍历时,我能够正确查看国家/地区详细信息,但是我希望在 GUI 中查看和修改国家/地区类的实例。我遇到的问题是我不知道如何访问类数据并将其显示在列表框之类的东西中。这是我的 Country 课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace International_Trading_Data
{
class Country : IComparable
{
public string countryName { get; set; }
public double gdp { get; set; }
public double inflation { get; set; }
public double tradeBalance { get; set; }
public int hdiRanking { get; set; }
public LinkedList<string> tradePartners { get; set; }
public string f;
public Country (){
}
public Country(string cname, double g, double i, double t, int h, LinkedList<string> tp)
{
this.countryName = cname;
this.gdp = g;
this.inflation = i;
this.tradeBalance = t;
this.hdiRanking = h;
this.tradePartners = tp;
}
public int CompareTo(object obj)
{
Country temp = (Country)obj;
return countryName.CompareTo(temp.countryName);
}
public override string ToString()
{
foreach (string i in tradePartners)
f += i+",";
return countryName+" "+gdp+" "+" "+inflation+" "+tradeBalance+" "+ hdiRanking+ " "+f;
}
}
}
`
这是我创建国家类实例的地方:
public void loadFile()
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "CSV Files (*.csv)|*.csv";
open.FilterIndex = 1;
open.Multiselect = true;
if (open.ShowDialog() == DialogResult.OK)
{
string selectedFilePath = open.FileName;
const int MAX_SIZE = 5000;
string[] allLines = new string[MAX_SIZE];
allLines = File.ReadAllLines(selectedFilePath);
foreach (string line in allLines)
{
if (line.StartsWith("Country"))
{
headers = line.Split(',');
}
else
{
string[] columns = line.Split(',');
LinkedList<string> tradePartners = new LinkedList<string>();
string[] partners = columns[5].Split('[', ']', ';');
foreach (string i in partners)
{
if (i != "")
{
tradePartners.AddLast(i);
}
}
countries.InsertItem(new Country(columns[0], Double.Parse(columns[1]),Double.Parse(columns[2]), Double.Parse(columns[3]) ,int.Parse(columns[4]),tradePartners));
}
}
这是我的中序遍历的代码:
public void InOrder()
{
inOrder(root);
}
private void inOrder(Node<T> tree)
{
if (tree != null)
{
inOrder(tree.Left);
System.Diagnostics.Debug.WriteLine(tree.Data.ToString());
inOrder(tree.Right);
}
此代码为一些测试国家/地区生成以下输出:
阿根廷 3 22.7 0.6 45 巴西、智利、
澳大利亚 3.3 2.2 -5 2 中国、日本、新西兰、
巴西 3 5.2 -2.2 84 智利、阿根廷、美国、
所以我知道我的类正确存储在 avl 树中。
【问题讨论】:
-
你是用文本编辑器写的吗?您需要一个带有表单或窗口的项目来在屏幕上显示某些内容。这是 WPF 还是 WinForms?
-
我在 Visual Studio 中有一个带有表单的项目。在表单上我有一个列表框。我的问题是我不知道如何从 avl 树中检索国家名称并将其显示在列表框中。
-
你必须给我们更多。你有构建树的代码吗?你有国家类的实例吗?你有没有试图从树上得到任何东西?
-
当我从 csv 文件中读取行时,我创建了 country 类的实例并将它们添加到 teee。我已经修改了我的帖子以显示我从 avl 树输出的内容。
标签: c# user-interface avl-tree