開発環境
- Microsoft Windows 7 Home Premium (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
独習C# 第3版 ハーバート・シルト (著) エディフィストラーニング株式会社 矢嶋聡 (監修, 翻訳) の第7章(C演算子のオーバーロード、インデクサー、プロパティ)の理解度チェック10、11、12を解いてみる。
10, 11, 12.
コード
using System;
public class Set
{
char[] members;
public int Length { get; private set; }
public Set()
{
Length = 0;
}
public Set(int size)
{
members = new char[size];
Length = 0;
}
public Set(Set s)
{
members = new char[s.Length];
for (int i = 0; i < s.Length; i++)
{
members[i] = s[i];
}
Length = s.Length;
}
public char this[int idx]
{
get
{
if (idx >= 0 && idx < Length)
{
return members[idx];
}
else
{
return (char)0;
}
}
}
int find(char ch)
{
for (int i = 0; i < Length; i++)
{
if (members[i] == ch)
{
return i;
}
}
return -1;
}
public static Set operator +(Set ob, char ch)
{
if (ob.find(ch) != -1)
{
return new Set(ob);
}
else
{
Set newset = new Set(ob.Length + 1);
for (int i = 0; i < ob.Length; i++)
{
newset.members[i] = ob.members[i];
}
newset.Length = ob.Length + 1;
newset.members[newset.Length - 1] = ch;
return newset;
}
}
public static Set operator -(Set ob, char ch)
{
Set newset = new Set();
int i = ob.find(ch);
for (int j = 0; j < ob.Length; j++)
{
if (j != i)
{
newset = newset + ob.members[j];
}
}
return newset;
}
public static Set operator +(Set ob1, Set ob2)
{
Set newset = new Set(ob1);
for (int i = 0; i < ob2.Length; i++)
{
newset = newset + ob2[i];
}
return newset;
}
public static Set operator -(Set ob1, Set ob2)
{
Set newset = new Set(ob1);
for (int i = 0; i < ob2.Length; i++)
{
newset -= ob2[i];
}
return newset;
}
public static bool operator <(Set ob1, Set ob2)
{
for (int i = 0; i < ob1.Length; i++)
{
if (ob2.find(ob1[i]) == -1)
{
return false;
}
}
return true;
}
public static bool operator >(Set ob1, Set ob2)
{
for (int i = 0; i < ob2.Length; i++)
{
if (ob1.find(ob2[i]) == -1)
{
return false;
}
}
return true;
}
public static Set operator &(Set ob1, Set ob2)
{
Set newset = new Set();
for (int i = 0; i < ob2.Length; i++)
{
if (ob1.find(ob2[i]) != -1)
{
newset += ob2[i];
}
}
return newset;
}
public static Set operator |(Set ob1, Set ob2)
{
return (ob1 + ob2) - (ob1 & ob2);
}
public static void Display(Set set)
{
for (int i = 0; i < set.Length; i++)
{
Console.Write("{0} ", set[i]);
}
Console.WriteLine();
}
}
class Tester
{
public void Run()
{
Set s1 = new Set();
Set s2 = new Set();
Set s3 = new Set();
s1 += 'a';
s1 += 'b';
s1 += 'c';
s2 += 'a';
s2 += 'b';
s3 += 'b';
s3 += 'c';
s3 += 'd';
s3 += 'e';
Set[] sets = { s1, s2, s3 };
for (int i = 0; i < sets.Length; i++)
{
Console.Write("s{0}: ", i + 1);
Set.Display(sets[i]);
}
Console.WriteLine("s1s2:{1}",
s1 < s2, s1 > s2);
Console.Write("s1 & s2: ");
Set.Display(s1 & s2);
Console.Write("s1 & s3: ");
Set.Display(s1 & s3);
Console.Write("s1 | s2: ");
Set.Display(s1 | s2);
Console.Write("s1 | s3: ");
Set.Display(s1 | s3);
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
s1: a b c s2: a b s3: b c d e s1<s2: False, s1>s2:True s1 & s2: a b s1 & s3: b c s1 | s2: c s1 | s3: a d e 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿