bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

code

サンプルをぱくったサンプル

元ネタはここ。 http://opcdiary.net/?p=3438マルぱくり。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace LinqToXML { class Program { static void Main(string[] args) { var…

C++を学ぶ

知っていることより知らないことの方が多いC++ 処理の一部だけを特殊化したい場合に、派生クラスでオーバーライドしたものを使う。 こんな感じで良いのだろうか。 #include <iostream> using namespace::std; // 基底 class cBase { public: cBase(){} virtual ~cBase(</iostream>…

perlとCでバイナリ

頻繁に使うテーブルのバイナリ化の簡単なメモを残しておくことにした。perlでテキストのリストをバイナリにして、 Cでそのバイナリを読み込み、プリントしてみる。下記を実行するとlist.binができる。 ./txt2bin.pl list.txt下記でlist.binの内容を表示する…

三角形の走査変換のミス

ミスがあった。 色々面倒くさい。

三角形の走査変換もどき

三角形を塗ることができた。 とにかく実装してみたかった。今は反省している。 後処理が怪しい。 // main.cpp #include <stdlib.h> #include <stdio.h> #include <float.h> #include <math.h> #include <SDL/SDL.h> //----------------------------------------------- // 構造体の定義 //------------------</sdl/sdl.h></math.h></float.h></stdio.h></stdlib.h>…

参照カウンタ付きのキャッシュ

大雑把に実装してみた。 もっと抽象化できると思う。 まずは、main.cpp // main.cpp #include<iostream> #include "CacheManager.h" using namespace std; // シングルトンお試し用 void init(); void run(); void end(); void disp(int num, InterfaceResource **ress</iostream>…

ポインタの参照渡し

#include<iostream> #include<string> using namespace std; // 引数のポインタを0(NULL)にする void Ref(int* (&a)) { a = 0; } // 引数のポインタを0(NULL)にする void Ptr(int** a) { *a = 0; } int main() { int num = 5; int *i = &num; cout << "i=" << i << " -> "; Ref</string></iostream>…

やっとこさPython

そろそろPythonを触らないと興味が霧散してしまいそうなので、触った。 演算子をオーバーロードできるのがいい。 class Fraction: def __init__(self, num, den): self.numerator = num self.denominator = den # self.sign def __add__(self, _t): tnum = s…

C#で要素が分数の行列

これまでに書いたコード。Class1.cs using System; using MyMath; namespace ConsoleApplication2 { /// <summary> /// Class1 の概要の説明です。 /// </summary> class Class1 { /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Mai…

C#とFormアプリでGUI部品を動的に生成

どんなサイズの行列にも対応するためのはじめの一歩。 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace FormSample1 { /// <summary> /// Form1 の概要の説</summary>…

C++/CLIでやったことをC#で2

行列クラスも書いた。 ただし、逆行列はまだ。 using System; namespace MyMath { /// <summary> /// 行列クラスです。 /// </summary> public class Matrix { private Fraction[,] m_elements; // コンストラクタ public Matrix( int row, int column ) { this.m_elements = ne…

C++/CLIでやったことをC#で1

分数のクラスを書いた。 コメント書けない病の克服に至らず。 using System; using System.Collections; namespace MyMath { /// <summary> /// 分数クラスです。 /// </summary> public class Fraction { private int m_numerator; private int m_denominator; private int m_s…

勘違い

msdnのコードを鵜呑みにしたのが失敗だった。 public override string ToString() { return( String.Format( "{0}/{1}", this.m_numerator, this.m_denominator ) ); } 上記をみて、string型を返すのにSystem.String型になっている。 こいつを改めてやれば動…

VS2003.NETのC#

下記が上手く動いた。 // ToStringのオーバライド public override string ToString() { return ( this.m_numerator + "/" + this.m_denominator ); } 下記では上手く動かなかった。 String.Formatで色々エラーが出た。 バージョンの問題だろうか。 public o…

原因半分解明

前の分数テンプレートクラスのoperator=が下記のもの。 // TFractionクラスの一部 // 演算子 TFraction<T>& operator=( TFraction<T> t ) { m_numerator = t.GetNumerator(); m_denominator = t.GetDenominator(); m_sign = t.GetSign(); return *this; } 分子が0</t></t>…

重いけど我慢

前作った行列のアプリをC++/CLIのWindowsFormアプリで作り直している。 LabelやTextBoxをリストに入れることを覚えた。 Form1.hより // Form1.hの一部 #pragma endregion private: // コレクション System::Collections::Generic::List<System::Windows::Forms::TextBox^>^ _TextBoxCollections</system::windows::forms::textbox^>…

unsigned int を unsigned char 4つに

#include <stdio.h> #include <stdlib.h> int main() { unsigned int input = 0; unsigned char bits[4] = {0}; printf( "%u\n", 0xffffffff ); //input = 4294967294; //input = 16777215; input = 4278255614; for( int i = 0; i < 4; ++i ) { for( int j = 0; j < 8; ++j ) {</stdlib.h></stdio.h>…

やることそっちのけで高校野球

高校球児の眩しさに立ち眩みをした。 甲子園は暑そうだ。 頑張ってる彼らもまた労働市場に入ってくる。 勝ち目なさ過ぎる。 ハフマン符号で手詰まり感。馬鹿には難しいのかな。 ビット列の基礎からやり直し。 #include <stdio.h> #include <stdlib.h> int main() { int input = </stdlib.h></stdio.h>…

抽象的に考えられない

具体例をゴリゴリして帰納法的に抽象化を試みるのは、能力的に悲しい気がする。 ハフマンもどきで満足気味なのもアレ。 配列サイズを自分で計算していないところが悲しさ3倍増し。 #include <stdio.h> #include <stdlib.h> int main() { unsigned int input[3] = { 0, 2, 3 }; </stdlib.h></stdio.h>…

SHIFT-JIS

下のやつはlength = 2になる。 star[0]に上位、star[1]に下位みたいに分けて入れられているみたいです。 #include<stdio.h> #include<string.h> int main() { char *star = "☆"; printf( star ); printf( " : length = %d", (int)strlen( star ) ); printf( "\n" ); if( 0 == st</string.h></stdio.h>…

こっちの方が分かり易いかな

#include <iostream> using namespace std; template< typename T > class TMatrix { private: T** m_elements; size_t m_row; size_t m_column; public: TMatrix() { } TMatrix( size_t row, size_t column ) : m_elements(0), m_row( row ), m_column( column ) { m_</iostream>…

C++で2次元配列を動的に(その2)

段々違和感なく見ていられるようになったかな。 #include <iostream> using namespace std; // 2次元配列を動的に確保 template< typename T > T **newDim2Array( size_t row, size_t column ) { T **array = new T*[row]; for( int i = 0; i < (int)row; ++i ) array</iostream>…

C++で2次元配列を動的に

下記のようになるが、納得し切れていない。 #include <iostream> using namespace std; int main() { // 配列の行:rowと列:column int row=0, column=0; cout << "行の数を入力 >"; cin >> row; cout << "列の数を入力 >"; cin >> column; // 生成 int **array = new i</iostream>…

共用体2

個人的には、こっちの方が良いかな。 他の予約語にも、アッと驚く使い方があるのかしらん。 #include <iostream> using namespace std; /* union Elements { float m[4][4]; struct { float m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m3</iostream>…

共用体

id:malibu-bulldogさんに共用体を使うことを薦められたので、行列の要素として使うサンプルを作ったら良い感じだ。 こういう感じになるのでしょうか。 #include <iostream> using namespace std; union Elements { float m[4][4]; struct { float m00, m01, m02, m03, </iostream>…

動いているAABBとBSP-Treeに入れてある複数のAABBの接触判定

CAABBは軸平行境界ボックス(Axis-Aligned Bounding Box)構造体をメンバ変数に持つクラス。ゲームプログラミングのためのリアルタイム衝突判定作者: Christer Ericson,中村達也出版社/メーカー: ボーンデジタル発売日: 2005/10メディア: 単行本購入: 7人 クリ…

再び読む気になれないコードに

以前作った行列クラスはこんな感じ。 class Matrix44 { public: float m_11, m_12, m_13, m_14, m_21, m_22, m_23, m_24, m_31, m_32, m_33, m_34, m_41, m_42, m_43, m_44; Matrix44( float in11=1, float in12=0, float in13=0, float in14=0, float in21=…

reduction関数を簡潔にしてみた

今までの奴は下記のもの。 // 約分 template<class T> TFriction<T> TFriction<T>::reduction() { Exec(); // 負の符号の時は、分子から負を取り因数分解する if( false == m_sign ) m_numerator *= -1; std::vector<T> n_factor = Factorization<T>( m_numerator ); std::vector<T> </t></t></t></t></t></class>…

operator/

除算演算子を追加。 先ほどと同じようにFriction.hにある分数のテンプレートに下記を追加。 TFriction<T> operator/( TFriction<T> t ) { this->Exec(); t.Exec(); TFriction<T> temp( this->m_numerator*t.GetDenominator(), // 分子 this->m_denominator*t.GetNumer</t></t></t>…

operator*

乗算演算子を追加 Friction.hにある分数のテンプレートクラスに下記を追加。 TFriction<T> operator*( TFriction<T> t ) { this->Exec(); t.Exec(); TFriction<T> temp( t.GetNumerator()*this->m_numerator, // 分子 t.GetDenominator()*this->m_denominator ); // </t></t></t>…