再帰の基礎:波を再帰モデルで表現する 実行結果とソースコード

実行結果

Memo1
depth= 99, Value= 4.1556
depth= 98, Value= 3.88514
depth= 97, Value= 3.55581
depth= 96, Value= 3.25747
depth= 95, Value= 3.0741
depth= 94, Value= 3.06032
depth= 93, Value= 3.2252
depth= 92, Value= 3.52836
depth= 91, Value= 3.88971
depth= 90, Value= 4.21046
depth= 89, Value= 4.40008
depth= 88, Value= 4.40172
depth= 87, Value= 4.20912
depth= 86, Value= 3.86988
depth= 85, Value= 3.47389
depth= 84, Value= 3.12963
depth= 83, Value= 2.93451
depth= 82, Value= 2.94746
depth= 81, Value= 3.17132
depth= 80, Value= 3.55028
depth= 79, Value= 3.98368
depth= 78, Value= 4.35254
depth= 77, Value= 4.55221
depth= 76, Value= 4.52188
depth= 75, Value= 4.26286
depth= 74, Value= 3.84019
depth= 73, Value= 3.36646
depth= 72, Value= 2.97195
depth= 71, Value= 2.76892
depth= 70, Value= 2.81981
depth= 69, Value= 3.1183
depth= 68, Value= 3.58898
depth= 67, Value= 4.10614
depth= 66, Value= 4.52728
depth= 65, Value= 4.7322
depth= 64, Value= 4.65718
depth= 63, Value= 4.31443
depth= 62, Value= 3.79109
depth= 61, Value= 3.22724
depth= 60, Value= 2.77857
depth= 59, Value= 2.57354
depth= 58, Value= 2.67675
depth= 57, Value= 3.069
depth= 56, Value= 3.65006
depth= 55, Value= 4.26401
depth= 54, Value= 4.741
depth= 53, Value= 4.944
depth= 52, Value= 4.80805
depth= 51, Value= 4.36049
depth= 50, Value= 3.71628
depth= 49, Value= 3.04864
depth= 48, Value= 2.54269
depth= 47, Value= 2.34426
depth= 46, Value= 2.5181
depth= 45, Value= 3.02732
depth= 44, Value= 3.74057
depth= 43, Value= 4.46561
depth= 42, Value= 5.001
depth= 41, Value= 5.19185
depth= 40, Value= 4.97437
depth= 39, Value= 4.39648
depth= 38, Value= 3.60787
depth= 37, Value= 2.82156
depth= 36, Value= 2.25647
depth= 35, Value= 2.07674
depth= 34, Value= 2.34432
depth= 33, Value= 2.99856
depth= 32, Value= 3.86931
depth= 31, Value= 4.7209
depth= 30, Value= 5.31568
depth= 29, Value= 5.48017
depth= 28, Value= 5.15526
depth= 27, Value= 4.41627
depth= 26, Value= 3.45607
depth= 25, Value= 2.53511
depth= 24, Value= 1.91094
depth= 23, Value= 1.76649
depth= 22, Value= 2.15679
depth= 21, Value= 2.98975
depth= 20, Value= 4.0472
depth= 19, Value= 5.04174
depth= 18, Value= 5.6946
depth= 17, Value= 5.81348
depth= 16, Value= 5.34879
depth= 15, Value= 4.41181
depth= 14, Value= 3.24875
depth= 13, Value= 2.17635
depth= 12, Value= 1.49593
depth= 11, Value= 1.409
depth= 10, Value= 1.95811
depth= 9, Value= 3.0101
depth= 8, Value= 4.28768
depth= 7, Value= 5.44222
depth= 6, Value= 6.14856
depth= 5, Value= 6.19621
depth= 4, Value= 5.55155
depth= 3, Value= 4.3726
depth= 2, Value= 2.971
depth= 1, Value= 1.73
depth= 0, Value= 1


C++Builder2007ソースコード

//************************
//***** Unit1.h ********
//************************

//—————————————————————————

#ifndef Unit1H
#define Unit1H
//—————————————————————————
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include “Chart.hpp”
#include “TeEngine.hpp”
#include “TeeProcs.hpp”
#include <ExtCtrls.hpp>
#include “Series.hpp”

#include <vector>
//—————————————————————————
const int max_depth = 100; // 最大階層数

class RecursiveWave
{
public:
int current_depth; // クラスメンバーとして
double Value;
RecursiveWave *child;
RecursiveWave *z_1, *z_2;

RecursiveWave(int n);

void LinkParents(RecursiveWave* parent1, RecursiveWave* parent2); // 後からリンク
void Dump(TMemo *memo);
double GetValue(){ return Value; }
};

std::vector<RecursiveWave*> all_waves;

//—————————————————————————
class TForm1 : public TForm
{
__published: // IDE-managed Components
TChart *Chart1;
TButton *ButtonExit;
TLineSeries *Series1;
TMemo *Memo1;
TLineSeries *Series2;
TButton *Button1;
void __fastcall ButtonExitClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);

RecursiveWave *Top;
void ChartSetting();

};
//—————————————————————————
extern PACKAGE TForm1 *Form1;
//—————————————————————————
#endif

 

//************************
//***** Unit1.cpp ******
//************************

//—————————————————————————

#include <vcl.h>
#pragma hdrstop

#include <math.h>
#include “Unit1.h”
//—————————————————————————
#pragma package(smart_init)
#pragma link “Chart”
#pragma link “TeEngine”
#pragma link “TeeProcs”
#pragma link “Series”
#pragma resource “*.dfm”
TForm1 *Form1;
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//—————————————————————————
void __fastcall TForm1::ButtonExitClick(TObject *Sender)
{
Close();
}
//—————————————————————————

// ChartSetting
void TForm1::ChartSetting()
{
Chart1->SeriesList->ClearValues();

// 軸タイトルだけ設定(SeriesCountチェックはButton1Click側に移動)
Chart1->LeftAxis->Title->Caption = “Value”;
Chart1->LeftAxis->Title->Visible = true;
Chart1->RightAxis->Title->Caption = “Depth”;
Chart1->RightAxis->Title->Visible = true;
Chart1->RightAxis->Visible = true;
Chart1->BottomAxis->Title->Caption = “Count”;
Chart1->BottomAxis->Visible = true;

}

RecursiveWave::RecursiveWave(int n): current_depth(n), Value(0.0), child(NULL),
z_1(NULL), z_2(NULL)
{
if (n >= max_depth) return;

// 子を先に作成(再帰)
child = new RecursiveWave(n + 1);

// all_waves登録
all_waves.push_back(this);

}

// 構築後に親を設定する専用メソッド
void RecursiveWave::LinkParents(RecursiveWave* parent1, RecursiveWave* parent2) {
z_1 = parent1;
z_2 = parent2 ? parent2 : parent1; // z_2がなければz_1で代用

// Valueを親を使って計算
if (z_1) {
Value = z_1->Value * 1.7 – (z_2 ? z_2->Value * 0.97 : 0.0) + 1.0;
} else {
Value = 1.0; // 最上位の初期値
}
}

void RecursiveWave::Dump(TMemo *memo)
{
if (all_waves.empty()) return;

AnsiString str;
int count = 0;

for (std::vector<RecursiveWave*>::const_iterator it = all_waves.begin();
it != all_waves.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, neigh->current_depth, neigh->Value) );
count++;
}
}

/*memo->Lines->Add(“==============”);
for(int i=0; i<all_waves->size(); i++){
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, i, (*all_waves)[i].Value ) );
}*/

}

// 新規ヘルパー関数(トップダウンで親情報を伝播)
void SetupParents(RecursiveWave* node, RecursiveWave* p1, RecursiveWave* p2) {
if (!node) return;

node->LinkParents(p1, p2);

// 子に伝える(z_1 = node自身、z_2 = p1)
SetupParents(node->child, node, p1);
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 既存のオブジェクトをクリア(メモリリーク防止)
for (size_t i = 0; i < all_waves.size(); i++) {
delete all_waves[i];
}
all_waves.clear();

// 正しい初期化
RecursiveWave *WaveTop = new RecursiveWave(0); // 引数不要!

// 再帰的に親リンクを設定(トップダウン)
SetupParents(WaveTop, NULL, NULL); // 新規ヘルパー関数

WaveTop->Dump(Memo1);

ChartSetting();

// Seriesを安全に追加
Chart1->SeriesList->ClearValues(); // 念のため

// Series[0] と Series[1] が確実に存在することを保証
while (Chart1->SeriesCount() < 2) {
TLineSeries* s = new TLineSeries(Chart1);
Chart1->AddSeries(s);
}

int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves.begin();
it != all_waves.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
Chart1->Series[0]->AddY(neigh->Value, “”, clBlue); // Value(左軸)
Chart1->Series[1]->AddY(neigh->current_depth, “”, clRed); // Depth(右軸) ← コメント解除OK
count++;
}
}

// 軸設定を再適用
Chart1->Series[0]->VertAxis = 0; // vaLeft
Chart1->Series[1]->VertAxis = 1; // vaRight

}
//—————————————————————————

カテゴリー: 未分類 パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です