All pastes #457128 Raw Edit

Stuff

public text v1 · immutable
#457128 ·published 2007-04-25 05:57 UTC
rendered paste body
using System;
using System.Collections;

// Operator overloading
namespace OperatorOverload
{
	class Program
	{
		static void Main(string[] args)
		{
			int i = 5;
			int j = 3;
			int k = i + j;

			string fn = "per";
			string ln = "Larsen";
			string fullname = fn + " " + ln ;

			Set A = new Set(); // A =            
			A.Add(3);
			A.Add(5);
			A.Add(13);
			A.Add(8);
			Console.WriteLine ("A = " + A);

			Set B = new Set();
			B.Add(10);
			B.Add(11);
			B.Add(8);
			B.Add(5);
			Console.WriteLine("B = " +B);

			Set c = A + B;  //union Set (foreningsmngde)
					//A+B operator overloading

			Set D = A * B;  // Intersection (fllesmngde)

			Console.WriteLine("c=" +c);
			Console.WriteLine("D=" +D);
			/*
			 * C={3,8,5,10,13,11}
			 * D={5,8} 
			 */
		}
	}

	class Set {

		private ArrayList list = new ArrayList();

		public Set() {
		}
		public void Add(Object obj) {
			if (obj != null && !list.Contains(obj)) {
				list.Add(obj);
			}
		}
		public static Set operator*(Set a, Set b) {
			Set c = new Set();
			for (int i = 0; i < a.list.Count; i++) {
				if (b.list.Contains(a.list[i])) {
					c.Add(a.list[i]);
				}
			}
			return c;
		}
		public static Set operator+(Set a, Set b) {
			Set c = new Set();
			for (int i = 0; i < a.list.Count; i++) {
				c.Add(a.list[i]);
			}
			for (int i = 0; i < b.list.Count; i++) {
				c.Add(b.list[i]);
			}
			return c;
		}
		public override string ToString() {
			string tmp = null;
			for (int i = 0; i < list.Count; i++) {
				if (tmp == null)
					tmp = list[i].ToString();
				else
					tmp = tmp + ", " + list[i].ToString();
			}
			return tmp;
		}

	}

}