在好例子网,分享、交流、成长!
您当前所在位置:首页Pascal 开发实例Pascal编程基础 → 切管软件

切管软件

Pascal编程基础

下载此实例
  • 开发语言:Pascal
  • 实例大小:0.84M
  • 下载次数:1
  • 浏览次数:0
  • 发布时间:2025-06-16
  • 实例类别:Pascal编程基础
  • 发 布 人:terry_tj
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 切管 计算

实例介绍

【实例简介】
切管软件,最大减少费材
【实例截图】
from clipboard
【核心代码】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace PipeCuttingDLL
{
    public interface IPipeCutting
    {
        int addTest(int x, int y);
        string myPipeCutting(string standardLens,string requiredLens);
    }
    [ClassInterface(ClassInterfaceType.None)]
    public class PipeCuttingClass : IPipeCutting
    {
        public class PipeCuttingOptimizer
        {
            // 精度控制,避免浮点数比较问题
            private const double Tolerance = 0.0001;

            public List<CuttingPlan> OptimizeCutting(List<double> standardLengths, List<double> requiredLengths)
            {
                // 验证输入
                if (standardLengths == null || !standardLengths.Any())
                    throw new ArgumentException("标准管件长度列表不能为空");
                if (requiredLengths == null || !requiredLengths.Any())
                    throw new ArgumentException("需求管件长度列表不能为空");
                if (requiredLengths.Any(r => r <= 0))
                    throw new ArgumentException("需求管件长度必须大于0");
                if (standardLengths.Any(s => s <= 0))
                    throw new ArgumentException("标准管件长度必须大于0");

                // 先处理需求长度,按从大到小排序
                requiredLengths = requiredLengths.OrderByDescending(l => l).ToList();
                standardLengths = standardLengths.OrderBy(l => l).ToList();

                List<CuttingPlan> result = new List<CuttingPlan>();
                List<double> remainingRequirements = new List<double>(requiredLengths);

                while (remainingRequirements.Count > 0)
                {
                    // 找出最适合当前剩余需求的管材
                    var bestFit = FindBestFit(standardLengths, remainingRequirements);
                    result.Add(bestFit);

                    // 从剩余需求中移除已满足的部分
                    foreach (var cut in bestFit.Cuts)
                    {
                        // 使用近似比较来处理浮点数精度问题
                        var index = remainingRequirements.FindIndex(r => Math.Abs(r - cut) < Tolerance);
                        if (index >= 0)
                        {
                            remainingRequirements.RemoveAt(index);
                        }
                    }
                }

                return result;
            }

            private CuttingPlan FindBestFit(List<double> standardLengths, List<double> requiredLengths)
            {
                // 找出所有可能的切割组合
                var allCombinations = GenerateCombinations(standardLengths.Max(), requiredLengths);

                if (!allCombinations.Any())
                {
                    throw new InvalidOperationException("无法找到合适的切割方案,可能有需求长度超过最大标准管件长度");
                }

                // 找出浪费最少的组合
                var bestCombination = allCombinations.OrderBy(c => c.Waste).First();

                // 找出最适合的标准管材长度
                double bestPipeLength = standardLengths
                    .Where(l => l >= bestCombination.TotalLength || Math.Abs(l - bestCombination.TotalLength) < Tolerance)
                    .OrderBy(l => l - bestCombination.TotalLength)
                    .First();

                return new CuttingPlan
                {
                    PipeLength = bestPipeLength,
                    Cuts = bestCombination.Cuts,
                    Waste = bestPipeLength - bestCombination.TotalLength
                };
            }

            private List<CuttingCombination> GenerateCombinations(double maxPipeLength, List<double> requiredLengths)
            {
                List<CuttingCombination> result = new List<CuttingCombination>();
                GenerateCombinationsHelper(requiredLengths, maxPipeLength, new List<double>(), 0, result);

                // 如果没有找到任何组合(可能因为单个需求就超过最大管长)
                if (!result.Any() && requiredLengths.Any())
                {
                    // 尝试只取第一个需求(虽然会浪费很多)
                    if (requiredLengths[0] <= maxPipeLength)
                    {
                        result.Add(new CuttingCombination
                        {
                            Cuts = new List<double> { requiredLengths[0] },
                            TotalLength = requiredLengths[0],
                            Waste = maxPipeLength - requiredLengths[0]
                        });
                    }
                }

                return result;
            }

            private void GenerateCombinationsHelper(List<double> requiredLengths, double maxLength,
                List<double> currentCombination, int startIndex, List<CuttingCombination> result)
            {
                double currentTotal = currentCombination.Sum();

                // 如果当前组合的总长度超过了管材最大长度(考虑浮点精度),则返回
                if (currentTotal > maxLength && Math.Abs(currentTotal - maxLength) > Tolerance)
                {
                    return;
                }

                // 如果当前组合不为空,添加到结果中
                if (currentCombination.Count > 0)
                {
                    result.Add(new CuttingCombination
                    {
                        Cuts = new List<double>(currentCombination),
                        TotalLength = currentTotal,
                        Waste = maxLength - currentTotal
                    });
                }

                // 递归生成所有可能的组合
                for (int i = startIndex; i < requiredLengths.Count; i )
                {
                    // 跳过会导致总长度超过管长的需求
                    if (currentTotal requiredLengths[i] > maxLength &&
                        Math.Abs(currentTotal requiredLengths[i] - maxLength) > Tolerance)
                    {
                        continue;
                    }

                    currentCombination.Add(requiredLengths[i]);
                    GenerateCombinationsHelper(requiredLengths, maxLength, currentCombination, i 1, result);
                    currentCombination.RemoveAt(currentCombination.Count - 1);
                }
            }
        }

        public class CuttingPlan
        {
            public double PipeLength { get; set; }
            public List<double> Cuts { get; set; }
            public double Waste { get; set; }

           // public override string ToString()
           // {
           //     return $"使用{PipeLength:F2}管材切割: {string.Join(", ", Cuts.Select(c => c.ToString("F2")))} (浪费: {Waste:F2})";
           // }

            public override string ToString()
            {
                return $"使用 {PipeLength} 切割: {string.Join(", ", Cuts.Select(c => c.ToString()))} (浪费: {Waste:F2})";
            }
        }

        public class CuttingCombination
        {
            public List<double> Cuts { get; set; }
            public double TotalLength { get; set; }
            public double Waste { get; set; }
        }

        public int addTest(int x, int y)
        {
            return x y;
        }
        public string myPipeCutting(string standardLens, string requiredLens)
        {
            var optimizer = new PipeCuttingOptimizer();

            try
            {
                // 标准管材长度(带小数)
                List<double> standardLengths = new List<double>(
                    Array.ConvertAll(standardLens.Split(','), s => double.Parse(s.Trim()))
                );
                // 客户需求长度(带小数)
                List<double> requiredLengths = new List<double>(
                    Array.ConvertAll(requiredLens.Split(','), s => double.Parse(s.Trim()))
                );

                //List<double> standardLengths = new List<double> { 107, 113, 103, 76, 117, 120, 112 };

                // 客户需求长度(带小数)
                // List<double> requiredLengths = new List<double> { 36, 36, 36, 36, 36, 39, 39, 43, 43, 37, 45, 45, 42, 42, 42, 42, 42, 45.5, 45.5, 49.5, 49.5, 33, 33, 27.5, 27.5, 27.5, 32, 32, 35.5, 35.5 };

                var plans = optimizer.OptimizeCutting(standardLengths, requiredLengths);

                String str = "" ;
               // Console.WriteLine("最优切割方案:");
                foreach (var plan in plans)
                {
                    // Console.WriteLine(plan);
                    str = str plan Environment.NewLine;
                }
                str = str Environment.NewLine;
                str = str $"总浪费: {plans.Sum(p => p.Waste):F2}" Environment.NewLine;
                str = str $"使用管材数量: {plans.Count}根" Environment.NewLine;

                // Console.WriteLine($"总浪费: {plans.Sum(p => p.Waste):F2}");
                // Console.WriteLine($"使用管材数量: {plans.Count}根");
                // Console.ReadKey();
                return str;
            }
            catch (Exception ex)
            {
               // Console.WriteLine($"发生错误: {ex.Message}");
                return ex.Message;
            }

        }
    }



}


标签: 切管 计算

实例下载地址

切管软件

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警