对于a,b两个数组
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] a = new int[5]{1,2,3,4,5};
int[] b = new int[5]{3,4,5,6,1};
int[] c = new int[10];
int[] d = new int[10];
private void button1_Click(object sender, EventArgs e)
{
c = jiaoji(a, b);
richTextBox1.AppendText("交集是:");
for (int i = 0; i < c.Length; i++)
if(c[i]!=0)
richTextBox1.AppendText(c[i].ToString() + ' ');
d = chaji(a, b);//此函数实现的差集:属于a,不属于b的集合。如果反之,则调换参数前后顺序
richTextBox1.AppendText("\n差集是:");
for (int i = 0; i < c.Length; i++)
if (d[i] != 0)
richTextBox1.AppendText(d[i].ToString() + ' ');
}
public int[] jiaoji(int[]a,int[]b)
{
int[] c = new int[30];
int k = 0;
for (int i = 0; i < a.Length; i++)
for (int j = 0; j < b.Length; j++)
{
if (a[i] == b[j])
{
c[k] = a[i];
k++;
}
}
return c;
}
public int[] chaji(int[] a, int[] b)
{
int[] c = new int[30];
int k = 0;
for (int i = 0; i < a.Length; i++)
{
bool found = false;
for (int j = 0; j < b.Length; j++)
if (a[i] == b[j])
found = true;
if (!found)
{
c[k] = a[i];
k++;
}
}
return c;
}
}
}