⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scalesbalancingproblem.cs

📁 Data Structures and Algorithms with Object-Oriented Design Patterns in C# 这本书的范例代码dll自己反编译的source
💻 CS
字号:
namespace Opus6
{
    using System;
    using System.Collections;
    using System.Text;

    [Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng."), Version("$Id: ScalesBalancingProblem.cs,v 1.5 2001/10/28 19:50:09 brpreiss Exp $")]
    public class ScalesBalancingProblem
    {
        public ScalesBalancingProblem(int[] weight)
        {
            this.weight = weight;
            this.numberOfWeights = weight.Length;
        }

        public Solution Solve(Solver solver)
        {
            return solver.Solve(new Opus6.ScalesBalancingProblem.Node(this));
        }


        protected int numberOfWeights;
        protected int[] weight;


        protected class Node : Solution
        {
            public Node(ScalesBalancingProblem problem)
            {
                this.problem = problem;
                this.diff = 0;
                this.unplacedTotal = 0;
                this.numberPlaced = 0;
                this.pan = new int[problem.numberOfWeights];
                for (int num1 = 0; num1 < problem.numberOfWeights; num1++)
                {
                    this.unplacedTotal += problem.weight[num1];
                }
            }

            public ScalesBalancingProblem.Node Copy()
            {
                ScalesBalancingProblem.Node node1 = new ScalesBalancingProblem.Node(this.problem);
                node1.diff = this.diff;
                node1.numberPlaced = this.numberPlaced;
                for (int num1 = 0; num1 < this.problem.numberOfWeights; num1++)
                {
                    node1.pan[num1] = this.pan[num1];
                }
                node1.unplacedTotal = this.unplacedTotal;
                return node1;
            }

            public void PlaceNext(int p)
            {
                this.pan[this.numberPlaced] = p;
                if (p == 0)
                {
                    this.diff += this.problem.weight[this.numberPlaced];
                }
                else
                {
                    this.diff -= this.problem.weight[this.numberPlaced];
                }
                this.unplacedTotal -= this.problem.weight[this.numberPlaced];
                this.numberPlaced++;
            }

            public override string ToString()
            {
                StringBuilder builder1 = new StringBuilder();
                bool flag1 = false;
                for (int num1 = 0; num1 < this.numberPlaced; num1++)
                {
                    if (flag1)
                    {
                        builder1.Append(", ");
                    }
                    builder1.Append(this.pan[num1]);
                    flag1 = true;
                }
                builder1.Append(" diff = " + this.diff);
                return builder1.ToString();
            }


            public int Bound
            {
                get
                {
                    if (Math.Abs(this.diff) > this.unplacedTotal)
                    {
                        return (Math.Abs(this.diff) - this.unplacedTotal);
                    }
                    return 0;
                }
            }

            public bool IsComplete
            {
                get
                {
                    return (this.numberPlaced == this.problem.numberOfWeights);
                }
            }

            public bool IsFeasible
            {
                get
                {
                    return true;
                }
            }

            public int Objective
            {
                get
                {
                    return Math.Abs(this.diff);
                }
            }

            public IEnumerable Successors
            {
                get
                {
                    return new Enumerable(new Opus6.ScalesBalancingProblem.Node.SuccessorEnumerator(this));
                }
            }


            protected int diff;
            protected int numberPlaced;
            protected int[] pan;
            private ScalesBalancingProblem problem;
            protected int unplacedTotal;


            public class SuccessorEnumerator : IEnumerator
            {
                internal SuccessorEnumerator(ScalesBalancingProblem.Node node)
                {
                    this.pan = -1;
                    this.node = node;
                }

                public bool MoveNext()
                {
                    this.pan++;
                    if (this.pan == 2)
                    {
                        this.pan = -1;
                    }
                    return (this.pan >= 0);
                }

                public void Reset()
                {
                    this.pan = -1;
                }


                public object Current
                {
                    get
                    {
                        if (this.pan < 0)
                        {
                            throw new InvalidOperationException();
                        }
                        ScalesBalancingProblem.Node node1 = this.node.Copy();
                        node1.PlaceNext(this.pan);
                        return node1;
                    }
                }


                private ScalesBalancingProblem.Node node;
                protected int pan;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -