吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 566|回复: 9
收起左侧

[求助] c# 数字拆分,求个思路

[复制链接]
7R903 发表于 2023-11-24 13:51

有这么两组数据,length不相等,我需要将pushOutRow 中下标的ExchangeQty 给到pullInRow 的New Qty中,如果大于,需要进行拆分到新行,fltDate同时也赋值过去

[C#] 纯文本查看 复制代码
var pullInRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-01",ExchangeQty = 39000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A2", FltDate = "2023-12-22",ExchangeQty = 6000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A3", FltDate = "",ExchangeQty = 15000}
            };

            var pushOutRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 35000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ASD", FltDate = "2023-12-07",ExchangeQty = 25000}
            };
//正确结果
var endRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-01", ExchangeQty = 39000,NewQty=35000,NewFltDate="2023-12-06"},//只有35000,全部分过去
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-01", ExchangeQty = 39000,NewQty=4000,NewFltDate="2023-12-07"},//39000-35000=4000(新增行)使用25000其中的4000
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A2", FltDate = "2023-12-22", ExchangeQty = 6000,NewQty=6000,NewFltDate="2023-12-07"},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A3", FltDate = "", ExchangeQty = 15000,NewQty=15000,NewFltDate="2023-12-07"}
            };

public class POExchangeList
        {
            public Guid ID { get; set; }
            public string CustomerName { get; set; }
            public string FltDate { get; set; }
            public int ExchangeQty { get; set; }
            public string NewFltDate { get; set; }
            public int NewQty { get; set; }
            public string bindID { get; set; }
            public int isExchange { get; set; }
        }


在这段代码中,我只考虑了pushOutRow 第一行的ExchangeQty 是小于的,如果第二行小于就会出现问题
[C#] 纯文本查看 复制代码
int deadQty = 0;
            List<POExchangeList> pullTemp = new List<POExchangeList>();
            foreach (var pushOut in pushOutRow)
            {
                foreach (var pullIn in pullInRow)
                {
                    int pushOutQty = pushOut.ExchangeQty;
                    int pullInQty = pullIn.ExchangeQty;
                    if (pullIn.isExchange != 1)
                    {
                        if (pushOutQty < pullInQty)
                        {
                            pullIn.NewQty = pushOutQty;
                            pullIn.NewFltDate = pushOut.FltDate;
                            pullIn.isExchange = 1;

                            deadQty = pullInQty - pushOutQty;//4000
                            break;
                        }
                        else
                        {
                            pullIn.NewQty = pullIn.ExchangeQty;
                            pullIn.NewFltDate = pushOut.FltDate;
                            
                        }
                    }
                    else
                    {
                        //是否还有剩余数
                        if (deadQty > 0)
                        {
                            pullTemp.Add(new POExchangeList
                            {
                                ID = new Guid(),
                                CustomerName = pullIn.CustomerName,
                                FltDate = pullIn.FltDate,
                                ExchangeQty = pullIn.ExchangeQty,
                                NewQty = deadQty,
                                NewFltDate = pushOut.FltDate,
                                isExchange = 1
                            });

                            deadQty -= pullIn.ExchangeQty;
                        }
                    }
                }
            }
            pullInRow.AddRange(pullTemp);


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

go4399 发表于 2023-11-24 15:47
本帖最后由 go4399 于 2023-11-24 15:50 编辑
public static void POExchange(List<POExchangeList> pullInRow, List<POExchangeList> pushOutRow)
{
    if (pullInRow.Count < 1 || pushOutRow.Count < 1)
    {
        return;
    }

    var i = 0;
    var o = 0;
    var inQty = pullInRow[i].ExchangeQty;
    var outQty = pushOutRow[o].ExchangeQty;
    while (inQty != 0 && outQty != 0)
    {
        if (outQty <= inQty)
        {
            pullInRow[i].NewQty = outQty;
            pullInRow[i].NewFltDate = pushOutRow[o].FltDate;
            pullInRow[i].isExchange = 1;
            inQty -= outQty;

            if (inQty > 0)
            {
                var newExchange = new POExchangeList
                {
                    ID = Guid.NewGuid(),
                    CustomerName = pullInRow[i].CustomerName,
                    FltDate = pullInRow[i].FltDate,
                    ExchangeQty = pullInRow[i].ExchangeQty
                };
                pullInRow.Insert(++i, newExchange);
            }
            else
            {
                ++i;
                inQty = i < pullInRow.Count ? pullInRow[i].ExchangeQty : 0;
            }

            ++o;
            outQty = o < pushOutRow.Count ? pushOutRow[o].ExchangeQty : 0;
        }
        else // if (outQty > inQty)
        {
            pullInRow[i].NewQty = inQty;
            pullInRow[i].NewFltDate = pushOutRow[o].FltDate;
            pullInRow[i].isExchange = 1;
            outQty -= inQty;

            ++i;
            inQty = i < pullInRow.Count ? pullInRow[i].ExchangeQty : 0;
        }
    }
}
simleXL 发表于 2023-11-24 16:12
[C#] 纯文本查看 复制代码
var pullInRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-01",ExchangeQty = 39000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A2", FltDate = "2023-12-22",ExchangeQty = 6000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A3", FltDate = "",ExchangeQty = 15000}
            };

            var pushOutRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 35000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ASD", FltDate = "2023-12-07",ExchangeQty = 25000}
            };

            List<POExchangeList> pushResult=new List<POExchangeList>();

            for (int i = 0; i < pullInRow.Count;i++ )
            {
                if (i < pushOutRow.Count)
                {
                    if (pullInRow[i].ExchangeQty > pushOutRow[i].ExchangeQty)
                    {
                        pushResult.Add(new POExchangeList
                        {
                            ID = new Guid(),
                            CustomerName = pullInRow[i].CustomerName,
                            FltDate = pullInRow[i].FltDate,
                            ExchangeQty = pullInRow[i].ExchangeQty,
                            NewQty = pushOutRow[i].ExchangeQty,
                            NewFltDate = pushOutRow[i].FltDate
                        });
                        pushResult.Add(new POExchangeList
                        {
                            ID = new Guid(),
                            CustomerName = pullInRow[i].CustomerName,
                            FltDate = pullInRow[i].FltDate,
                            ExchangeQty = pullInRow[i].ExchangeQty,
                            NewQty = pullInRow[i] .ExchangeQty- pushOutRow[i].ExchangeQty,
                            NewFltDate = pushOutRow[i].FltDate
                        });                        
                    }
                    else
                    {
                        pushResult.Add(new POExchangeList
                        {
                            ID = new Guid(),
                            CustomerName = pullInRow[i].CustomerName,
                            FltDate = pullInRow[i].FltDate,
                            ExchangeQty = pullInRow[i].ExchangeQty,
                            NewQty = pullInRow[i].ExchangeQty,
                            NewFltDate = pullInRow[i].FltDate
                        });
                    }
                }
                else
                {
                    pushResult.Add(new POExchangeList
                    {
                        ID = new Guid(),
                        CustomerName = pullInRow[i].CustomerName,
                        FltDate = pullInRow[i].FltDate,
                        ExchangeQty = pullInRow[i].ExchangeQty,
                        NewQty = pullInRow[i].ExchangeQty,
                        NewFltDate = pullInRow[i].FltDate
                    });
                }
            }
cc329533757 发表于 2023-11-24 17:04
go4399 发表于 2023-11-24 17:21
simleXL 发表于 2023-11-24 16:12
[mw_shl_code=csharp,true]var pullInRow = new List() {
                new POExchangeList {  ID=Guid ...

20-37行没有考虑pullInRow中1条记录会拆分成3条及以上的情况
36、48、61行NewFltDate赋值错误
 楼主| 7R903 发表于 2023-11-26 10:55
go4399 发表于 2023-11-24 15:47
[md]```csharp
public static void POExchange(List pullInRow, List pushOutRow)
{

大部分都是正常的,但有个疑问,当我的数据是以下内容的时候,endRow 有点异常
[C#] 纯文本查看 复制代码
 var pullInRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-01",ExchangeQty = 50000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-02",ExchangeQty = 4000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-03",ExchangeQty = 6000},

            };

            var pushOutRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 55000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-08",ExchangeQty = 4000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-07",ExchangeQty = 1000}
            };
            var endRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-01",ExchangeQty = 50000,NewQty=50000, NewFltDate="2024-12-06"},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-02",ExchangeQty = 4000,NewQty=4000, NewFltDate="2024-12-06"},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 6000,NewQty = 1000, NewFltDate="2024-12-06"},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 6000,NewQty=4000,NewFltDate="2023-12-08"},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 6000,NewQty=1000,NewFltDate="2023-12-07"}
            };
go4399 发表于 2023-11-26 14:01
cn005897 发表于 2023-11-26 10:55
大部分都是正常的,但有个疑问,当我的数据是以下内容的时候,endRow 有点异常
[mw_shl_code=csharp,tru ...

这个endRow有什么异常
 楼主| 7R903 发表于 2023-11-26 14:46
本帖最后由 cn005897 于 2023-11-26 14:52 编辑
go4399 发表于 2023-11-26 14:01
这个endRow有什么异常

应该是这样有问题,当pullInRow小于PushOutRow的长度,endRow(pullInRow )应该是有三条数据
[C#] 纯文本查看 复制代码
var pullInRow = new List<POExchangeList>() {

new POExchangeList {  ID=Guid.NewGuid(), CustomerName="A1", FltDate = "2024-12-03",ExchangeQty = 6000},

            };

            var pushOutRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 1000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06",ExchangeQty = 3000},
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-07",ExchangeQty = 2000}
            };

var endRow = new List<POExchangeList>() {
                new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2023-12-03",ExchangeQty = 50000,NewQty=0, NewFltDate=""},     
            };
go4399 发表于 2023-11-26 14:58
cn005897 发表于 2023-11-26 14:46
应该是这样有问题,当pullInRow小于PushOutRow的长度,endRow(pullInRow )应该是有三条数据
[mw_shl_cod ...

我测试的结果是
[C#] 纯文本查看 复制代码
 var endRow = new List<POExchangeList>() {
    new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03",ExchangeQty = 6000, NewFltDate = "2023-12-06", NewQty = 1000},
    new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03",ExchangeQty = 6000, NewFltDate = "2023-12-06", NewQty = 3000},
    new POExchangeList {  ID=Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03",ExchangeQty = 6000, NewFltDate = "2023-12-07", NewQty = 2000}
};

刚好拆成3行,你要求的正确结果是什么
go4399 发表于 2023-11-26 15:24
cn005897 发表于 2023-11-26 14:46
应该是这样有问题,当pullInRow小于PushOutRow的长度,endRow(pullInRow )应该是有三条数据
[mw_shl_cod ...

以下是完整测试代码,可以运行看结果

using System;
using System.Collections.Generic;

namespace Test
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            var pullInRow = new List<POExchangeList>()
            {
                new POExchangeList { ID = Guid.NewGuid(), CustomerName = "A1", FltDate = "2024-12-03", ExchangeQty = 6000 }
            };

            var pushOutRow = new List<POExchangeList>()
            {
                new POExchangeList { ID = Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06", ExchangeQty = 1000 },
                new POExchangeList { ID = Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-06", ExchangeQty = 3000 },
                new POExchangeList { ID = Guid.NewGuid(), CustomerName = "ADD", FltDate = "2023-12-07", ExchangeQty = 2000 }
            };
            POExchange(pullInRow, pushOutRow);
            Print(pullInRow);
            Console.ReadKey();
        }

        public class POExchangeList
        {
            public Guid ID { get; set; }
            public string CustomerName { get; set; }
            public string FltDate { get; set; }
            public int ExchangeQty { get; set; }
            public string NewFltDate { get; set; }
            public int NewQty { get; set; }
            public string bindID { get; set; }
            public int isExchange { get; set; }
        }

        public static void Print(List<POExchangeList> list)
        {
            Console.WriteLine("var endRow = new List<POExchangeList>() {");

            foreach (var exchangeList in list)
            {
                Print(exchangeList);
            }

            Console.WriteLine("};");
        }

        public static void Print(POExchangeList exchangeList)
        {
            Console.WriteLine($"    new POExchangeList {{ ID = Guid.NewGuid(), CustomerName = \"{exchangeList.CustomerName}\", FltDate = \"{exchangeList.FltDate}\", ExchangeQty = {exchangeList.ExchangeQty}, NewFltDate = \"{exchangeList.NewFltDate}\", NewQty = {exchangeList.NewQty} }};");
        }

        public static void POExchange(List<POExchangeList> pullInRow, List<POExchangeList> pushOutRow)
        {
            if (pullInRow.Count < 1 || pushOutRow.Count < 1)
            {
                return;
            }

            var i = 0;
            var o = 0;
            var inQty = pullInRow[i].ExchangeQty;
            var outQty = pushOutRow[o].ExchangeQty;
            while (inQty != 0 && outQty != 0)
            {
                if (outQty <= inQty)
                {
                    pullInRow[i].NewQty = outQty;
                    pullInRow[i].NewFltDate = pushOutRow[o].FltDate;
                    pullInRow[i].isExchange = 1;
                    inQty -= outQty;

                    if (inQty > 0)
                    {
                        var newExchange = new POExchangeList
                        {
                            ID = Guid.NewGuid(),
                            CustomerName = pullInRow[i].CustomerName,
                            FltDate = pullInRow[i].FltDate,
                            ExchangeQty = pullInRow[i].ExchangeQty
                        };
                        pullInRow.Insert(++i, newExchange);
                    }
                    else
                    {
                        ++i;
                        inQty = i < pullInRow.Count ? pullInRow[i].ExchangeQty : 0;
                    }

                    ++o;
                    outQty = o < pushOutRow.Count ? pushOutRow[o].ExchangeQty : 0;
                }
                else // if (outQty > inQty)
                {
                    pullInRow[i].NewQty = inQty;
                    pullInRow[i].NewFltDate = pushOutRow[o].FltDate;
                    pullInRow[i].isExchange = 1;
                    outQty -= inQty;

                    ++i;
                    inQty = i < pullInRow.Count ? pullInRow[i].ExchangeQty : 0;
                }
            }
        }
    }
}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 18:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表