Last active
August 29, 2015 14:08
-
-
Save nanase/65d9a820781ac242a5e0 to your computer and use it in GitHub Desktop.
pi2.cにコメントをつけてみた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // mes0〜mes5 は文字列。配列として用意している | |
| // 文字コードはUTF-8 (Windowsだと文字化けするかも) | |
| char mes0[] = { 0xe8, 0xa8, 0x88, 0xe7, 0xae, 0x97, 0, 0, 0, 0, 0, 0 , 0}, | |
| mes1[] = { 0, 0, 0, 0, 0, 0, 0xe5, 0x9b, 0x9e, 0xe6, 0x95, 0xb0, 0xe3, 0x82, 0x92, 0xe5, 0x85, 0xa5, 0xe5, 0x8a, 0x9b, 0x3a, 0x20, 0, 0, 0, 0 } , | |
| mes2[] = { 0x25, 0x6c, 0x6c, 0x64, 0 }, | |
| mes3[] = { 0, 0, 0, 0, 0, 0, 0xe4, 0xb8, 0xad, 0x2e, 0x2e, 0x2e, 0x28, 0x25, 0x36, 0x2e, 0x32, 0x66, 0x25, 0x25, 0x29, 0x20, 0, 0, 0, 0, 0, 0, 0, 0 } , | |
| mes4[] = { 0x50, 0x49, 0x20, 0x3d, 0x20, 0x25, 0x2e, 0x36, 0x66, 0xa, 0, 0 } , | |
| mes5[] = { 0xa, 0, 0, 0, 0, 0, 0, 0xe5, 0xae, 0x8c, 0xe4, 0xba, 0x86, 0xa, 0 }; | |
| // プロトタイプ宣言 | |
| void copy(char* base, char* to) ; | |
| void rndInt(void); | |
| // ダミー変数(AA調整用) | |
| int dummy = 1 + 2 + 3 + 4 + 5 - 2; | |
| // プロトタイプ宣言 | |
| double ge(double max); | |
| // ヘッダファイルインクルード | |
| // ./ とか // は(ここでは)ファイルパス的に意味がないので正しいパスです | |
| # include <././stdlib.h> | |
| # include <.////stdio.h> | |
| int main (void) { | |
| double x = 0.00, y=0.; | |
| // unsigned long long int は64bitの整数型 | |
| unsigned long long int i, c, r; | |
| // mes0の先頭文字列を各文字列に上書き | |
| copy(mes0, mes1); | |
| copy(mes0, mes3); | |
| copy(mes0, mes5 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1); | |
| // 計算回数を入力 | |
| do { | |
| printf(mes1); | |
| scanf(mes2, &i); | |
| // 1未満の数値が入力されたら再度入力させる | |
| } while ( i < 1 ); | |
| // r は 2/π の領域に当たった回数 | |
| r = 0; | |
| for (c = 0; c < i; c++) { | |
| // ge関数で0.0〜1.0までのランダムな数値を取ってくる | |
| x = ge(1.0); | |
| y = ge(1.0); | |
| // 減点までの距離が1.0未満 | |
| // (= 2/πの領域に入っている)ならばrをインクリメント | |
| if (x * x + y * y < 1.0){ | |
| r++; | |
| } | |
| // 進捗表示 | |
| // 計算回数が多いと長いことプログラムが終わらないので | |
| // 1000万回ごとに進捗表示 | |
| if (c >= 10000000 && c % 10000000 == 0) { | |
| // 完了率 | |
| printf(mes3, (double)c * 100.0 / (double)i); | |
| // 現在の途中経過 | |
| printf(mes4, (double)r * 4.0 / (double)c); | |
| } | |
| } | |
| // 結果表示 | |
| printf(mes5); | |
| 1 + 1; | |
| printf(mes4, (double)r * 4.0 / (double)c + 0. + 0); | |
| return (0); | |
| } | |
| // ランダムな数値のシード(種)を作る | |
| void rndInt(void) { | |
| // tmpは未初期化、 | |
| // つまりランダムな数値が入っていると期待 | |
| int tmp; | |
| // ランダムなシードが入るでしょう | |
| srand((unsigned int)&tmp); | |
| } | |
| // 0.0〜maxまでの乱数を生成 | |
| double ge(double max) { | |
| // ダミー処理 | |
| int dummy = 27 * 0 + 781 * 24 + 0xFF / 0xFC - 23 + 2; | |
| // ダミー処理 | |
| int pi2 = 1 + 4 + 9 + 25 + 36 + 49 + 64 + 81 + 100 ; | |
| // 乱数を得るテンプレ | |
| return ((rand() / ((double)RAND_MAX + 1.0)) * max); | |
| } | |
| // 文字列をコピーする関数 | |
| void copy(char* base, char* to) { | |
| //ダミー処理 | |
| // "I think the pi is so crazy" | |
| char *i = "t", *hin = "k", *th = "e", *pi = "i", *s = "s", *o = "cr", *a = "zy"; | |
| // ポインタを使って文字列をコピー | |
| while (*base != 0) { | |
| *to ++= *base++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment