2

很久以前我上过几节学校的课,老实说,我从来没有真正理解过课堂的概念。我最近“重新上马”,一直在尝试寻找一些真实世界的应用程序来创建一个类。

您可能已经看到我正在尝试解析大量的家谱数据,这些数据采用非常古老且过时的格式,称为 gedcom

我创建了一个 Gedcom Reader 类来读取文件,对其进行处理并将其作为两个列表提供,其中包含我认为需要使用的数据

对我来说更重要的是我创建了一个类来做到这一点,所以我非常想让这里的专家告诉我我做对了什么以及我可以做得更好(我不会说错,因为这件事有效而且已经足够好了为了我)

班级:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GedcomReader
{

    class Gedcom
    {
        private string GedcomText = "";


        public struct INDI
        {
            public string ID;
            public string Name;
            public string Sex;
            public string BDay;
            public bool Dead;
        }
        public struct FAM
        {
            public string FamID;
            public string Type;
            public string IndiID;
        }

        public List<INDI> Individuals = new List<INDI>();
        public List<FAM> Families = new List<FAM>();


        public Gedcom(string fileName)
        {
            using (StreamReader SR = new StreamReader(fileName))
            {
                GedcomText = SR.ReadToEnd();
            }
            ReadGedcom();

        }

        private void ReadGedcom()
        {
            string[] Nodes = GedcomText.Replace("0 @", "\u0646").Split('\u0646');
            foreach (string Node in Nodes)
            {
                string[] SubNode = Node.Replace("\r\n", "\r").Split('\r');
                if (SubNode[0].Contains("INDI"))
                {
                    Individuals.Add(ExtractINDI(SubNode));

                }
                else if (SubNode[0].Contains("FAM"))
                {
                    Families.Add(ExtractFAM(SubNode));
                }
            }
        }

        private FAM ExtractFAM(string[] Node)
        {
            string sFID = Node[0].Replace("@ FAM", "");
            string sID = "";
            string sType = "";
            foreach (string Line in Node)
            {
                // If node is HUSB
                if (Line.Contains("1 HUSB "))
                {
                    sType = "PAR";
                    sID = Line.Replace("1 HUSB ", "").Replace("@", "").Trim();
                }
                //If node for Wife
                else if (Line.Contains("1 WIFE "))
                {
                    sType = "PAR";
                    sID = Line.Replace("1 WIFE ", "").Replace("@", "").Trim();
                }
                //if node for multi children
                else if (Line.Contains("1 CHIL "))
                {
                    sType = "CHIL";
                    sID = Line.Replace("1 CHIL ", "").Replace("@", "");
                }
            }
            FAM Fam = new FAM();
            Fam.FamID = sFID;
            Fam.Type = sType;
            Fam.IndiID = sID;

            return Fam;
        }
        private INDI ExtractINDI(string[] Node)
        {

            //If a individual is found
            INDI I = new INDI();
            if (Node[0].Contains("INDI"))
            {
                //Create new Structure

                //Add the ID number and remove extra formating
                I.ID = Node[0].Replace("@", "").Replace(" INDI", "").Trim();
                //Find the name remove extra formating for last name
                I.Name = Node[FindIndexinArray(Node, "NAME")].Replace("1 NAME", "").Replace("/", "").Trim();
                //Find Sex and remove extra formating
                I.Sex = Node[FindIndexinArray(Node, "SEX")].Replace("1 SEX ", "").Trim();

                //Deterine if there is a brithday -1 means no
                if (FindIndexinArray(Node, "1 BIRT ") != -1)
                {
                    // add birthday to Struct 
                    I.BDay = Node[FindIndexinArray(Node, "1 BIRT ") + 1].Replace("2 DATE ", "").Trim();
                }

                // deterimin if there is a death tag will return -1 if not found
                if (FindIndexinArray(Node, "1 DEAT ") != -1)
                {
                    //convert Y or N to true or false ( defaults to False so no need to change unless Y is found.
                    if (Node[FindIndexinArray(Node, "1 DEAT ")].Replace("1 DEAT ", "").Trim() == "Y")
                    {
                        //set death
                        I.Dead = true;
                    }
                }

            }
            return I;
        }
        private int FindIndexinArray(string[] Arr, string search)
        {
            int Val = -1;
            for (int i = 0; i < Arr.Length; i++)
            {
                if (Arr[i].Contains(search))
                {
                    Val = i;
                }
            }
            return Val;
        }
    }
}

执行:

using System;
using System.Windows.Forms;
using GedcomReader;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\mostrecent.ged";
            string outpath = @"C:\gedcom.txt";
            Gedcom GD = new Gedcom(path);

            GraphvizWriter GVW = new GraphvizWriter("Family Tree");
            foreach(Gedcom.INDI I in GD.Individuals)
            {
                string color = "pink";
                if (I.Sex == "M")
                {
                    color = "blue";
                }
                GVW.ListNode(I.ID, I.Name, "filled", color, "circle");

                if (I.ID == "ind23800")
                {MessageBox.Show("stop");}
                //"ind23800" [ label="Sarah Mandley",shape="circle",style="filled",color="pink" ];
            }
            foreach (Gedcom.FAM F in GD.Families)
            {

                if (F.Type == "par")
                {
                    GVW.ConnNode(F.FamID, F.IndiID);
                }
                else if (F.Type =="chil")
                {
                    GVW.ConnNode(F.IndiID, F.FamID);
                }
            }
           string x =  GVW.SB.ToString();
            GVW.SaveFile(outpath);
            MessageBox.Show("done");
        }
    }

我特别感兴趣的是是否可以对我不知道我如何在实现中使用它们的结构做任何事情是最好的,但它再次起作用

非常感谢

4

3 回答 3

4

它可能更具可读性。很难阅读和理解。

您可以学习 SOLID 原则 ( http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod )

Robert C. Martin 在 Oredev 2008 上就清洁代码做了很好的介绍(http://www.oredev.org/topmenu/video/agile/robertcmartincleancodeiiifunctions.4.5a2d30d411ee6ffd2888000779.html

一些关于代码可读性的推荐书籍:

  • 肯特贝克“实施模式”

  • 罗伯特 C 马丁“清洁代码”罗伯特 C

  • Martin “C# 中的敏捷原则、模式和实践”

于 2009-05-13T13:51:33.487 回答
4

快速思考:

  • 嵌套类型不应该是可见的。
  • ValueTypes(结构)应该是不可变的。
  • 字段(类变量)不应公开。而是通过属性公开它们。
  • 检查传递的参数是否存在无效值,例如 null。
于 2009-05-13T13:41:37.107 回答
3

我建议你看看这个地方:http ://refactormycode.com/ 。

对于一些快速的事情,你的命名是我开始改变的最大的事情。无需使用全大写或缩写词。

此外,FxCop将帮助进行许多建议的更改。例如,FindIndexinArray将被命名为FindIndexInArray.

编辑:

我不知道这是您的代码中的错误还是设计使然,但是在中FindIndexinArray,一旦找到匹配项,您就不会中断循环。您想要数组中的第一个(中断)还是最后一个(无中断)匹配?

于 2009-05-13T13:43:32.063 回答