c# - 在 C# 中显示 2 个集合的并集

标签 c# union

我有一个 C# 代码,我在其中询问用户他想要创建的集合的数量,然后在这些集合中输入元素。从这些集合中,他选择 2 个集合并显示所选集合的并集。

在下面的代码中,集合中的元素没有被添加到 _items 中,也没有显示 Union。

感谢您的帮助。

namespace Union
{

  class Program
  {
    static List<SortedSet<string>> _items = new List<SortedSet<string>>();
    static SortedSet<string> set = new SortedSet<string>();

    static void Main(string[] args)
    {
        int i, j, a, b;
        string k;
        Console.WriteLine("\n Enter the number of set to be used: ");
        i = Convert.ToInt32(Console.ReadLine());
        for ( j = 1; j <= i; j++)
        {
            SortedSet<string> set = new SortedSet<string>();
            do
            {
                Console.WriteLine("Enter first element in set {0}:", j);
                k = Console.ReadLine();
                if (k != "stop")
                    set.Add(k);
            } while (k != "stop");
            _items.Add(set);
        }
        Console.WriteLine("Enter index of 1st set  of union:{0}");
        b = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter index of 2nd set  of union:{0}");
        c = Convert.ToInt32(Console.ReadLine());
        DisplayUnion(a, b);
    }

    public static void DisplayUnion(int a, int b)
    {
        SortedSet<string> set1 = _items[a];
        SortedSet<string> set2 = _items[b];
        set1.UnionWith(set2);
        Console.WriteLine(set1);
    }
  }
}

最佳答案

通过修改 Main()DisplayUnion(int a, int b) 方法完全编辑了我的答案,以实现更好的表示并包括边界情况。 Main() 方法:

static void Main(string[] args)
{
    int i, j, a, b;
    string k;
    Console.WriteLine("Enter the number of sets to be used: ");
    i = Convert.ToInt32(Console.ReadLine());
    for (j = 1; j <= i; j++)
    {
        SortedSet<string> set = new SortedSet<string>();
        var index = 0;
        do
        {
            index++;
            Console.WriteLine($"Enter {index} element in set {j}:");
            k = Console.ReadLine();
            if (k != "stop")
                set.Add(k);
        } while (k != "stop");
        _items.Add(set);
    }

    if (_items.Count == 0)
    {
        Console.WriteLine("You have no sets to union.");
        return;
    }

    if (_items.Count == 1)
    {
        Console.WriteLine("Union of only set is: " + string.Join("", _items[0]));

        return;
    }

    while (true)
    {
        Console.WriteLine("Enter index of 1st set  of union:{0}");
        a = Convert.ToInt32(Console.ReadLine());
        if (a < _items.Count)
        {
            break;
        }

        Console.WriteLine($"Set {a} does not exists.");
    }

    while (true)
    {
        Console.WriteLine("Enter index of 2nd set  of union:{0}");
        b = Convert.ToInt32(Console.ReadLine());
        if (b < _items.Count)
        {
            break;
        }

        Console.WriteLine($"Set {b} does not exists.");
    }

DisplayUnion(int a, int b) 方法:

public static void DisplayUnion(int a, int b)
{

    SortedSet<string> set1 = _items[a];
    SortedSet<string> set2 = _items[b];
    set1.UnionWith(set2);
    Console.WriteLine($"Union of set {a + 1} with set {b + 1} is: " + string.Join("", set1));
}

您收到 outOfRangeException,因为您为集合输入了无效索引,而没有检查它实际上是无效的任何地方。我通过添加两个 while 循环 解决了这个问题。 我还为集合为 0 或 1 的边界情况添加了两个 if 语句

希望对您有所帮助。

关于c# - 在 C# 中显示 2 个集合的并集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40519872/

相关文章:

c# - 将 Asp.net MVC 模型数据绑定(bind)到 kendo TreeView 模板(本地数据)

c# - TransactionScope 最大超时

c# - 在单个处理程序中包装多个方法调用(每个方法都有不同的签名)

mysql - 内部连接与联合所有

sql - Oracle 使用相同的子查询联合多个表

c# - 策略模式的实现如何在运行时选择正确的策略?

c# - 将 [Flags] 枚举序列化为字符串

sql - UNION 与 WHERE 子句

php - MySQL从表中选择数据并内连接来自另外两个相同表的所有选择

if-statement - 联合以及如果值存在于其他表 Power BI 中