有用なデザインパターンをサンプルコードとして羅列したページ
switch_case文とcontinue;との関係
switch_case文の内部でbreak;はよく使われるがcontinue;を利用する場合はちょっと特殊な考え方になる
switch文の外側にあるネスト内の反復処理、for、while、do 、foreachに対して次のループに移る
特殊な検索をかける場合に憶えておくと便利なやり方になる
visualStudioではカーソルをbreak;やcontinue;の上に持っていくと関連している構文をマーカー表示する
わからなくなったら、まずこれで位置を確認すると良い
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
-
-
!
-
-
!
|
|
|
|
|
-
!
-
|
-
|
|
|
|
|
|
!
|
-
!
-
|
|
!
!
|
|
!
!
| using UnityEngine;
using System.Collections;
public class SwitchContinue : MonoBehaviour
{
void Start()
{
string str ="B(AC(1A))CB(57A6)";
int pos = 0;
int nest = 0;
int priority = 0;
int lowPriority = 3;
for (int i = 0; i < str.Length; i++)
{
switch (str[i])
{
case 'A': priority = 1; break; case 'B': priority = 2; break;
case 'C': priority = 3; break;
case '(': nest++; continue;
case ')': nest--; continue;
default: continue; }
if (nest==0 && priority <= lowPriority)
{
lowPriority = priority;
pos = i;
}
}
print(string.Format("配列位置:{0} の文字:{1}", pos,str[pos]));
}
}
|
帰納関数のサンプル(順列の出力)
基本的な帰納コードのサンプル
サンプルとして順列をテキストファイルで出力する
<帰納処理を書くコツ>
- 基底部、帰納部の二段階で関数を作成する(ドミノの一つ目、それに続く無限のドミノ、とふたつに分ける)
- 基底の呼び出しの際は出来るだけ余計なデーターを渡さないように設計する
- 派生した関数(帰納部)で扱う「差が生じるデータ」部が参照渡しの値の場合、希望する動作により呼び出し元が書き換えられると困る場合もあるので値の動きに注意する。必要であれば複製する
- 帰納関数の入口に終了条件を設定する
<このような形式のコードのなにが利点か?>
例えば10行目11行目を
int length = 5;
char[] set = { 'H', 'T' };
と書き換えても動く。forループでよく似た動きをするコードを書いた場合、変更はコード全体に及んでしまうが帰納では、このフォーマットを指定する部分のみで済む
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
-
|
-
|
|
|
|
|
|
|
!
|
-
!
-
|
|
|
|
-
|
|
|
!
|
|
!
|
-
!
-
|
|
-
|
-
|
|
|
!
!
-
|
!
!
|
-
|
!
-
!
-
|
-
|
-
|
!
!
!
!
| using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class Perm : MonoBehaviour
{
void Start()
{
int length = 3;
char[] set = { 'A', 'B', 'C' };
List<string> strList = Root(set, length);
string folder = Application.dataPath; SaveText(folder, @"\test6.txt", strList.ToArray());
}
List<string> Root(char[] set, int length)
{
List<string> strList = new List<string>();
int level = 0;
for (int i = 0; i < set.Length; i++)
{
char[] dat = new char[length];
dat[0] = set[i];
Loop(dat, level, set, strList);
}
return strList;
}
void Loop(char[] dat, int level, char[] set, List<string> strList)
{
level++;
if (level < dat.Length) {
for (int i = 0; i < set.Length; i++)
{
char[] cloneDat = (char[])dat.Clone(); cloneDat[level] = set[i];
Loop(cloneDat, level, set, strList); }
}
else {
strList.Add(new string(dat));
}
}
public void SaveText(string fileFolder, string filename, string[] dataStr)
{
using (StreamWriter w = new StreamWriter(fileFolder + filename, false, System.Text.Encoding.GetEncoding("shift_jis")))
{
foreach (var item in dataStr)
{
w.WriteLine(item);
}
}
}
}
|
補足:値渡し、参照渡しとは?
以下のコードの出力は
123
0
789
となる
変数valListの内容が関数内で書き換えられている点に注目。int型は値型だが、int[]型は参照型になるので関数は参照渡しを受け取り、呼び出し元を書き換える
つまり帰納関数で同様の事をした場合、派生先の関数内の書き換えで呼び出し元を書き変える事になり希望していた効果が得られない事がある
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
-
|
-
|
|
|
|
|
|
|
|
|
!
|
-
|
|
!
!
| using UnityEngine;
using System.Collections;
public class TestMetho : MonoBehaviour {
void Start () {
int val = 123;
int[] valList = { 456, 789 };
Method(val, valList);
print(val);
print(valList[0]);
print(valList[1]);
}
void Method(int val, int[] valList) {
val = 000;
valList[0] = 000;
}
}
|
データーをwiki用に変換したりCSVで出力等
資料:虚数の情緒P522
\({ a}^{ { \left( \frac { 1 }{ 2 } \right) }^{ n } }\) の値をwiki表形式で出力
a=10 | a=200 | a=1000 | a=5000 | a=10000 | 3.162278 | 14.14214 | 31.62278 | 70.71068 | 100 | 1.778279 | 3.760603 | 5.623413 | 8.408964 | 10 | 1.333521 | 1.939227 | 2.371374 | 2.899821 | 3.162278 | 1.154782 | 1.392561 | 1.539927 | 1.702886 | 1.778279 | 1.074608 | 1.180068 | 1.240938 | 1.304947 | 1.333521 | 1.036633 | 1.08631 | 1.113974 | 1.142343 | 1.154782 | 1.018152 | 1.042262 | 1.05545 | 1.068804 | 1.074608 | 1.009035 | 1.020912 | 1.027351 | 1.03383 | 1.036633 | 1.004507 | 1.010402 | 1.013583 | 1.016774 | 1.018152 | 1.002251 | 1.005188 | 1.006769 | 1.008352 | 1.009035 | 1.001125 | 1.00259 | 1.003379 | 1.004167 | 1.004507 | 1.000562 | 1.001294 | 1.001688 | 1.002082 | 1.002251 | 1.000281 | 1.000647 | 1.000844 | 1.00104 | 1.001125 | 1.000141 | 1.000323 | 1.000422 | 1.00052 | 1.000562 | 1.00007 | 1.000162 | 1.000211 | 1.00026 | 1.000281 | 1.000035 | 1.000081 | 1.000105 | 1.00013 | 1.000141 | 1.000018 | 1.00004 | 1.000053 | 1.000065 | 1.00007 | 1.000009 | 1.00002 | 1.000026 | 1.000033 | 1.000035 | 1.000004 | 1.00001 | 1.000013 | 1.000016 | 1.000018 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
-
|
-
|
|
|
|
|
|
|
|
!
|
-
!
-
|
-
|
-
|
!
!
!
|
-
!
-
|
|
|
|
-
|
|
-
|
!
|
!
|
!
|
-
!
-
|
|
-
!
-
|
!
|
|
!
|
-
|
!
-
!
-
|
-
|
-
|
!
!
!
|
-
!
-
|
|
-
|
!
|
!
!
| using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
public class Pow10 : MonoBehaviour {
void Start () {
int length = 20;
float[] bottomList = { 10f, 200f, 1000f, 5000f, 10000f };
float[] powNumber = GeneratorNumPow(bottomList, length).ToArray<float>();
string[] str = CSV2Wiki( PowNumToCSV(powNumber, length));
string folder = Application.dataPath; ClassSaveText(folder, @"\PowList.txt", str);
}
private IEnumerable<float> GeneratorNumPow(float[] bottomList,int length)
{
for (int j = 0; j < bottomList.Length; j++)
{
for (float i = 0; i < length; i++)
{
yield return Mathf.Pow(bottomList[j], Mathf.Pow(1f / 2f, i)); }
}
}
private string[] PowNumToCSV(float[] powNumber,int length)
{
int strLength = powNumber.Length / length;
string[] str = new string[length];
for (int j = 0; j < length; j++)
{
string temp=powNumber[j].ToString();
for (int i = 1; i < strLength; i++)
{
temp += string.Format(",{0}",powNumber[i * length + j]);
}
str[j] = temp;
}
return str;
}
private string[] CSV2Wiki(IEnumerable<string> data)
{
List<string> strList = new List<string>();
foreach (var item in data)
{
strList.Add("|" + item.ToString().Replace(',', '|') + "|");
}
return strList.ToArray();
}
public void SaveText(string fileFolder, string filename, string[] dataStr)
{
using (StreamWriter w = new StreamWriter(fileFolder + filename, false, System.Text.Encoding.GetEncoding("shift_jis")))
{
foreach (var item in dataStr)
{
w.WriteLine(item);
}
}
}
private void ClassSaveText(string folder, string name, IEnumerable data)
{
List<string> strList = new List<string>();
foreach (var item in data)
{
strList.Add(item.ToString());
}
SaveText(folder, name, strList.ToArray());
}
}
|
C#における累乗根計算
例えば以下のような累乗根計算をC#で行う場合は以下のようなコーディングになる
\({ 10 }^{ { \left( \frac { 1 }{ 2 } \right) }^{ n } }\quad =\quad \sqrt [ { 2 }^{ n } ]{ 10 } \)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
-
|
-
|
|
|
-
|
!
|
|
|
!
|
|
-
|
|
-
|
!
!
-
|
!
-
!
-
|
-
|
-
|
!
!
!
|
-
!
-
|
|
-
|
!
|
!
!
| using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
public class Pow10 : MonoBehaviour {
void Start () {
IEnumerable<float> powNumber = GeneratorNumPow();
foreach (var item in powNumber)
{
print(item);
}
string folder = Application.dataPath; ClassSaveText(folder, @"\PowList.txt", powNumber);
}
private IEnumerable<float> GeneratorNumPow()
{
int length = 10;
for (float i = 0; i < length; i++)
{
yield return Mathf.Pow(10, Mathf.Pow(1f / 2f, i)); }
}
public void SaveText(string fileFolder, string filename, string[] dataStr)
{
using (StreamWriter w = new StreamWriter(fileFolder + filename, false, System.Text.Encoding.GetEncoding("shift_jis")))
{
foreach (var item in dataStr)
{
w.WriteLine(item);
}
}
}
private void ClassSaveText(string folder, string name, IEnumerable data)
{
List<string> strList = new List<string>();
foreach (var item in data)
{
strList.Add(item.ToString());
}
SaveText(folder, name, strList.ToArray());
}
}
|
出力結果
10
3.162278
1.778279
1.333521
1.154782
1.074608
1.036633
1.018152
1.009035
1.004507
組み合わせ計算
プログラムコードではforループが利用できるので数学的ではないがシンプルな計算を行った
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| -
!
-
|
|
|
|
|
-
|
|
!
|
!
| public int nCr(int n, int r)
{
if (n == r || r == 0) return 1;
int deno = n;
int nume = 1;
for (int i = 2; i <= r; n--, i++)
{
deno *= (n - 1);
nume *= i;
}
return deno / nume;
}
|
値から特定桁の数字を抽出
値を\({ 10 }^{ n-1 }\)で割って、もう一度\(10\)で割った余りを拾えば狙った桁の数字を抽出できる(\(n\):桁数)
他のn進数でも同様の考え方で狙った桁を抽出できる
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-
|
-
|
!
|
-
|
|
|
|
!
!
| using UnityEngine;
using System.Collections;
public class test1 : MonoBehaviour
{
void Start()
{
print(Digit(123456789, 6)); }
public int Digit(int value, int dig) {
int n=1;
for (int i = 0; i < dig; i++) { n *= 10; }
int temp = value / n;
return temp%10;
}
}
|
デレゲートセレクタ
デレゲートを利用する事で関数を選択する事を可能とする
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
-
|
-
|
|
!
|
|
|
|
|
-
|
|
!
|
|
-
|
|
!
|
|
-
|
!
|
|
-
|
!
!
| using UnityEngine;
using System.Collections;
public class Sum1 : MonoBehaviour
{
public enum Function
{
Linear,
Gravity,
}
public Function function;
delegate float FunctionDelegate (float x);
FunctionDelegate[] FunctionDelegates = {
Linear,
Gravity,
};
void Start ()
{
FunctionDelegate f = FunctionDelegates [(int)function];
print (f (2));
}
static float Linear (float dt)
{
return 1f * dt;
}
static float Gravity (float dt)
{
return 9.8f * dt;
}
}
|
Enumerableを利用した数列の作成
等差数列、等比数列を作成する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
-
|
-
|
-
-
|
!
!
|
-
!
-
|
|
!
|
-
!
-
|
|
!
!
| using UnityEngine;
using System.Collections;
using System.Linq;
public class Numbers2 : MonoBehaviour
{
void Start ()
{
float[] quary = SequenceDiff (1, 2, 5);
foreach (var item in quary) {
print (item);
}
}
float[] SequenceDiff (float a, float d, int count)
{
float[] quary = Enumerable.Range (1, count).Select (n => a + (n - 1) * d).ToArray ();
return quary;
}
float[] SequenceRatio (float a, float r, int count)
{
float[] quary = Enumerable.Range (1, count).Select (n => a * Mathf.Pow (r, n)).ToArray ();
return quary;
}
}
|
IEnumerableを利用した数列の作成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
-
|
|
-
|
-
|
!
!
|
|
-
-
|
!
!
!
| using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Pattern : MonoBehaviour
{
void Start ()
{
IEnumerable<float> number = GeneratorNum (5, 20);
foreach (var item in number) {
print (item);
}
}
IEnumerable<float> GeneratorNum (int x, int y)
{
for (int i = x; i <= y; i++) {
yield return i;
}
}
}
|
|