コーディング のバックアップ差分(No.9)

Unity学習帳2冊目コーディング のバックアップ差分(No.9)
« Prev  Next »
8: 2016-01-30 (土) 03:10:18 osinko ソース 9: 2016-05-13 (金) 01:56:42 osinko ソース
Line 3: Line 3:
#jsmath #jsmath
有用なデザインパターンをサンプルコードとして羅列したページ 有用なデザインパターンをサンプルコードとして羅列したページ
 +
 +
 +**データーをwiki用に変換したりCSVで出力等 [#afbd5d39]
 +資料:虚数の情緒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|
 +
 +#code(csharp){{
 +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;    //これだけでunityの実行ファイルがあるフォルダがわかる
 +       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));  //指数計算部
 +           }
 +       }
 +   }
 +
 +   //CSV形式に
 +   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;
 +   }
 +
 +   //csv形式をPukiWiki表形式にする
 +   private string[] CSV2Wiki(IEnumerable<string> data)
 +   {
 +       List<string> strList = new List<string>();
 +
 +       //wiki用置換
 +       foreach (var item in data)
 +       {
 +           strList.Add("|" + item.ToString().Replace(',', '|') + "|");
 +       }
 +
 +       return strList.ToArray();
 +   }
 +
 +   //資料:StreamWriter クラス (System.IO)
 +   //http://msdn.microsoft.com/ja-jp/library/system.io.streamwriter(v=vs.110).aspx
 +
 +   //テキストファイルとしてセーブ
 +   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);
 +           }
 +       }
 +   }
 +
 +   //Classセーバー
 +   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#における累乗根計算 [#l34e77d6]
 +
 +例えば以下のような累乗根計算をC#で行う場合は以下のようなコーディングになる
 +
 +&font(150%){\({ 10 }^{ { \left( \frac { 1 }{ 2 }  \right)  }^{ n } }\quad =\quad \sqrt [ { 2 }^{ n } ]{ 10 } \)};
 +
 +#code(csharp){{
 +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;    //これだけでunityの実行ファイルがあるフォルダがわかる
 +       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));  //10^(1/2)^n
 +       }
 +   }
 +   //資料:StreamWriter クラス (System.IO)
 +   //http://msdn.microsoft.com/ja-jp/library/system.io.streamwriter(v=vs.110).aspx
 +
 +   //テキストファイルとしてセーブ
 +   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);
 +           }
 +       }
 +   }
 +
 +   //Classセーバー
 +   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
**組み合わせ計算 [#md88b46e] **組み合わせ計算 [#md88b46e]
« Prev  Next »


トップ   差分 バックアップ 複製 名前変更 リロード   ページ新規作成 全ページ一覧 単語検索 最新ページの一覧   ヘルプ   最新ページのRSS 1.0 最新ページのRSS 2.0 最新ページのRSS Atom