posted by 써니루루 2007. 3. 27. 12:41
선형 콘그루엔셜 방법(Hull. De bull 제안)

1. 하나의 초기 숫자를 설정(x0)
2. 3개의 상수 a, c, m을 수식에 대입해 난수를 발생시킨다.
수식 : x1 = (a * x0 + c) % m
3. 다음 조건을 만족해야 한다.
a < m, c < m, 0 < m


위 알고리즘을 C#으로 표현한 소스는 다음과 같다.

posted by 써니루루 2007. 3. 26. 10:01
읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>


이번 예제는 NextSibling, PreviousSibling을 이용해서 Element의 앞, 뒤 노드를 탐색하는 예제입니다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p557
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBook = xDoc.DocumentElement;
            XmlElement eFirstBook = (XmlElement)eBook.FirstChild;
            XmlElement eNextBook = (XmlElement)eFirstBook.NextSibling;

            XmlElement eLastBook = (XmlElement)eBook.LastChild;
            XmlElement ePreviousBook = (XmlElement)eLastBook.PreviousSibling;

            if (eNextBook == ePreviousBook)
            {
                Console.WriteLine("동일한 Book 엘리먼트 입니다.");
            }
            else
            {
                Console.WriteLine("다른 Book 엘리먼트 입니다.");
            }
        }
    }
}



위 원본 XML 파일에 book 노드가 4개이고,
첫노드의 다음 노드는 b2 book이고, 마지막노드의 이전 노드는 b3 book이 된다.
따라서 두개의 book 엘리먼트는 다른 엘리먼트가 된다.

ref. .NET 닷넷 개발자를 위한 XML p.557

posted by 써니루루 2007. 3. 26. 09:52
읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>



위 문서를 C#으로 읽어 들이기 위해 XmlDocument 개체를 이용한다.


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p556
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBook = xDoc.DocumentElement;
            XmlElement eFirstBook = (XmlElement)eBook.FirstChild;
            XmlNodeList nlChilds = eFirstBook.ChildNodes;

            for (int i = 0; i < nlChilds.Count; i++)
            {
                XmlElement eChild = (XmlElement)nlChilds[i];
                Console.WriteLine(eChild.Name + ":" + eChild.InnerText);
            }
        }
    }
}


XmlElement를 읽어 들인후 그 하위 노드의 리스트를 생성해서 for문을 이용해 loop를 돌리는 예제이다.

각각의 루프에서는 하위 자식노드의 갯수를 세서 그만큼의 자식 element를 생성하고 그 노드들을 제어한다.

위에보면 Javascript 등에서도 자주 봤던 .InnerText 가 눈에 띈다..

 

posted by 써니루루 2007. 3. 26. 09:42
읽어들일 XML 파일 'booklist.xml'

<?xml version="1.0" encoding="utf-8" ?>
<booklist>
  <book id="b1" kind="컴퓨터">
    <title>기초에서 실무까지 XML</title>
    <author>신민철</author>
    <publisher>프리렉</publisher>
    <price>35000</price>
  </book>
  <book id="b2" kind="소설">
    <title>사랑과 전쟁</title>
    <author>이사랑</author>
    <publisher>전쟁문화사</publisher>
    <price>15000</price>
  </book>
  <book id="b3" kind="잡지">
    <title>마이크로 소프트</title>
    <author>빌 게이츠</author>
    <publisher>마소문화사</publisher>
    <price>20000</price>
  </book>
  <book id="b4" kind="소설">
    <title>액션가면부인 바람났네</title>
    <author>짱구</author>
    <publisher>짱구출판사</publisher>
    <price>12000</price>
  </book>
</booklist>



위 문서를 C#으로 읽어 들이기 위해 XmlDocument 개체를 이용한다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace CH11
{
    class p554
    {
        static void Main()
        {
            string filePath = @"..\..\booklist.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);

            XmlElement eBook = xDoc.DocumentElement;

            XmlElement eFirstBook = (XmlElement)eBook.FirstChild;
            XmlElement eLastBook = (XmlElement)eBook.LastChild;

            Console.Write("첫 번째 책 정보 : ");
            Console.WriteLine("\t" + "id=" + eFirstBook.GetAttribute("id"));

            Console.Write("마지막 책 정보 : ");
            Console.WriteLine("\t" + "id=" + eLastBook.GetAttribute("id"));
        }
    }
}




ref. .NET 닷넷 개발자를 위한 XML p554