0

我收到一个产品列表(listS)和一个数量列表(listI),我想要的是从我的数据库中删除一些项目,这取决于我在 listI 上收到的数量。它就像一种魅力,只要我没有在列表中收到重复的产品,这就是我要解决的问题。

    public void remove(string[] listS, int[] listI)
    {
        int check = 0;
        int quantidadeDB;
        string ingredientDB;
        string ingredient;
        int quantity;
        string newLine;
        string[] linesSplit;
        string[] lines = System.IO.File.ReadAllLines("c:\\z.txt");
        System.IO.StreamWriter fileTemp = new System.IO.StreamWriter("c:\\z2.txt");
        for(int i = 0; i < lines.Length;i++)
        {
            linesSplit = lines[i].Split(' ');
            ingredientDB = linesSplit[0];
            quantityeDB = int.Parse(linesSplit[1]);
            check = 0;
            for(int j=0;j<listS.Length;j++)
            {
                ingredient = listS[j];
                quantity = listI[j];
                if (ingredientDB.ToLower().Equals(ingredient.ToLower()))
                {
                    newLine = ingredientDB + " " + (quantityDB - quantity);
                    fileTemp.WriteLine(newLine);
                    check++;
                }
            }
            if(check.Equals(0))
            fileB.WriteLine(ingredientDB + " " + quantityDB);
        }
        fileTemp.Close();
        string text = System.IO.File.ReadAllText("c:\\z2.txt");
        System.IO.StreamWriter fileL = new System.IO.StreamWriter("c:\\z.txt");
        fileL.Write(text);
        fileL.Close();
    }

示例:如果我的数据库是这个:芬达 100 百事可乐 2 可口可乐 3 冰茶 23 番茄酱 23

收到此数组后:

listS= {"Pepsi", "Pepsi", "Ketchup", "Pepsi", "Ketchup"}; 
listI = {2, 3, 1, 3, 2};

这就是它应该如何输出:

Fanta 100
Pepsi -6
Coca-Cola 3
IcedTea
Ketchup 20

代替:

Fanta 100
Pepsi 0
Pepsi -1
Pepsi -1
Coca-Cola 3
IcedTea 23
Ketchup 22
Ketchup 21
4

2 回答 2

1

如果您有一个包含名称和数量的产品结构,那会更好地实现。

struct Product
{
    public string Name;
    public int Quantity;
}

有一种方法可以通过嵌套的 for 循环来解决您的问题,并将您的数组转换为列表。

List<Product> products = new List<Product>();
for(int i = 0; i < listS.Length; i++)
{
    bool duplicate = false;
    foreach(Product p in products)
    {
        if(listS[i].ToLower().Equals(p.Name.ToLower()))
        {
            p.Quantity += listI[i];
            duplicate = true;
            break;
        }
    }
    if(!duplicate)
    {
        Product p;
        p.Name = listS[i];
        p.Quantity = listI[i];
        products.Add(a)
    }
}

那么产品将没有重复

编辑:如果您真的想保留您的代码,认为将所有内容转换为结构会导致性能损失,那么使用您的代码实施的修复实际上效率非常低。

整个方法可以这样实现。

public void remove(string[] listS, int[] listI)
{
    List<Product> products = new List<Product>();
    for(int i = 0; i < listS.Length; i++)
    {
        bool duplicate = false;
        foreach(Product p in products)
        {
            if(listS[i].ToLower().Equals(p.Name.ToLower()))
            {
                p.Quantity += listI[i];
                duplicate = true;
                break;
            }
        }
        if(!duplicate)
        {
            Product p;
            p.Name = listS[i];
            p.Quantity = listI[i];
            products.Add(a)
        }
    }
    string[] lines = System.IO.File.ReadAllLines("c:\\z.txt");
    for(int i = 0; i < lines.Length; i++)
    {
        string[] linesSplit = lines[i].Split(' ');
        foreach(Product p in products)
        {
            if (linesSplit[0].ToLower().Equals(p.Name.ToLower()))
            {
                lines[i] = linesSplit[0] + " " + (int.Parse(linesSplit[1]) - p.Quantity);
                products.Remove(p)
                break;
            }
        }
    }
    System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\z.txt");
    file.Write(lines);
    file.Close();
}
于 2013-07-06T05:44:59.810 回答
0

更改此行

newLine = ingredientDB + " " + (quantityDB - quantity);

对此:

newLine = ingredientDB + " " + (Int32.Parse(quantityDB) - Int32.Parse(quantity)).ToString();

您不能隐式转换stringint. 所以有一些不寻常的结果。

于 2013-07-06T05:29:36.700 回答