コード例 #1
0
ファイル: AlgTests.cs プロジェクト: aBaTaPbl4/Mini
        public void BellmanAlgTest_With9Goods()
        {
            var goods = Create9GoodsArray();

            var alg = new BellmanAlg(100, 4);
            alg.Calc(goods);
            _printer.PrintPack(alg.BestPriceGoodsPack);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: aBaTaPbl4/Mini
        static void MainBellmanAlg(string[] args)
        {
            int i, j; //просто переменные :)

            Console.WriteLine("Данная программа поможет Вам заполнить Ваш рюкзак максимально ценными товарами\nАвтор: Владимир Рыбалка");

            //Введем количество товаров
            do
            {
                incorrEnter = false;
                Console.Write("\nКакое количество товаров вы рассматриваете для приобретения: ");
                try
                {
                    _goodsCount = Convert.ToInt32(Console.ReadLine());
                }
                catch (FormatException)
                {
                    incorrEnter = true;
                    _goodsCount = 0;
                }
            } while (incorrEnter);

            var goods = new Good[_goodsCount]; //Создаем массив заданного размера

            //Создадим объекты товаров
            for (i = 0, j = i + 1; i < goods.Length; i++, j++)
            {
                Console.Write("\n\nВведите название " + j + "-го товара: ");
                _enteredName = Console.ReadLine();
                do
                {
                    incorrEnter = false;
                    Console.Write("Введите вес товара: ");
                    try
                    {
                        _enteredWeight = Convert.ToInt32(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        incorrEnter = true;
                        _enteredWeight = 0;
                    }
                } while (incorrEnter);
                do
                {
                    incorrEnter = false;
                    Console.Write("Введите цену товара: ");
                    try
                    {
                        _enteredPrice = Convert.ToInt32(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        incorrEnter = true;
                        _enteredPrice = 0;
                    }
                } while (incorrEnter);
                var good = new Good(_enteredName); //Создаем объект
                good.Weight = _enteredWeight;
                good.Price = _enteredPrice;
                goods[i] = good;
            }

            // Введем размер рюкзака
            do
            {
                incorrEnter = false;
                Console.Write("\nВведите размер вашего рюкзака (контейнера): ");
                try
                {
                    _bagMaxWeight = Convert.ToInt32(Console.ReadLine());
                }
                catch (FormatException)
                {
                    incorrEnter = true;
                    _bagMaxWeight = 0;
                }
            } while (incorrEnter);

            BellmanAlg alg = new BellmanAlg(_bagMaxWeight, _goodsCount);
            alg.Calc(goods);

            Print(goods, alg.BestPriceGoodsPack.Price);

            Console.ReadKey();
        }